Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ParseDecl.cpp - Declaration Parsing ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 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. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "RAIIObjectsForParser.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 16 | #include "clang/AST/ASTContext.h" |
Larisse Voufo | 7c64ef0 | 2013-06-21 00:08:46 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclTemplate.h" |
Benjamin Kramer | 9852f58 | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 18 | #include "clang/Basic/AddressSpaces.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 19 | #include "clang/Basic/Attributes.h" |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 20 | #include "clang/Basic/CharInfo.h" |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 21 | #include "clang/Basic/TargetInfo.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 22 | #include "clang/Parse/ParseDiagnostic.h" |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 23 | #include "clang/Sema/Lookup.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 24 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 25 | #include "clang/Sema/PrettyDeclStackTrace.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 26 | #include "clang/Sema/Scope.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallSet.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallString.h" |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/StringSwitch.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | using namespace clang; |
| 31 | |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | // C99 6.7: Declarations. |
| 34 | //===----------------------------------------------------------------------===// |
| 35 | |
| 36 | /// ParseTypeName |
| 37 | /// type-name: [C99 6.7.6] |
| 38 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 39 | /// |
| 40 | /// Called type-id in C++. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 41 | TypeResult Parser::ParseTypeName(SourceRange *Range, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 42 | Declarator::TheContext Context, |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 43 | AccessSpecifier AS, |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 44 | Decl **OwnedType, |
| 45 | ParsedAttributes *Attrs) { |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 46 | DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context); |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 47 | if (DSC == DSC_normal) |
| 48 | DSC = DSC_type_specifier; |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 49 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 50 | // Parse the common declaration-specifiers piece. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 51 | DeclSpec DS(AttrFactory); |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 52 | if (Attrs) |
| 53 | DS.addAttributes(Attrs->getList()); |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 54 | ParseSpecifierQualifierList(DS, AS, DSC); |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 55 | if (OwnedType) |
| 56 | *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0; |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 57 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 58 | // Parse the abstract-declarator, if present. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 59 | Declarator DeclaratorInfo(DS, Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 61 | if (Range) |
| 62 | *Range = DeclaratorInfo.getSourceRange(); |
| 63 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 64 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 65 | return true; |
| 66 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 67 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 68 | } |
| 69 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 70 | |
| 71 | /// isAttributeLateParsed - Return true if the attribute has arguments that |
| 72 | /// require late parsing. |
| 73 | static bool isAttributeLateParsed(const IdentifierInfo &II) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 74 | #define CLANG_ATTR_LATE_PARSED_LIST |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 75 | return llvm::StringSwitch<bool>(II.getName()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 76 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 77 | .Default(false); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 78 | #undef CLANG_ATTR_LATE_PARSED_LIST |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 81 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 82 | /// |
| 83 | /// [GNU] attributes: |
| 84 | /// attribute |
| 85 | /// attributes attribute |
| 86 | /// |
| 87 | /// [GNU] attribute: |
| 88 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 89 | /// |
| 90 | /// [GNU] attribute-list: |
| 91 | /// attrib |
| 92 | /// attribute_list ',' attrib |
| 93 | /// |
| 94 | /// [GNU] attrib: |
| 95 | /// empty |
| 96 | /// attrib-name |
| 97 | /// attrib-name '(' identifier ')' |
| 98 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 99 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 100 | /// |
| 101 | /// [GNU] attrib-name: |
| 102 | /// identifier |
| 103 | /// typespec |
| 104 | /// typequal |
| 105 | /// storageclass |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 106 | /// |
Richard Smith | 4c6c411 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 107 | /// Whether an attribute takes an 'identifier' is determined by the |
| 108 | /// attrib-name. GCC's behavior here is not worth imitating: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 109 | /// |
Richard Smith | 4c6c411 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 110 | /// * In C mode, if the attribute argument list starts with an identifier |
| 111 | /// followed by a ',' or an ')', and the identifier doesn't resolve to |
| 112 | /// a type, it is parsed as an identifier. If the attribute actually |
| 113 | /// wanted an expression, it's out of luck (but it turns out that no |
| 114 | /// attributes work that way, because C constant expressions are very |
| 115 | /// limited). |
| 116 | /// * In C++ mode, if the attribute argument list starts with an identifier, |
| 117 | /// and the attribute *wants* an identifier, it is parsed as an identifier. |
| 118 | /// At block scope, any additional tokens between the identifier and the |
| 119 | /// ',' or ')' are ignored, otherwise they produce a parse error. |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 120 | /// |
Richard Smith | 4c6c411 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 121 | /// We follow the C++ model, but don't allow junk after the identifier. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 122 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 123 | SourceLocation *endLoc, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 124 | LateParsedAttrList *LateAttrs, |
| 125 | Declarator *D) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 126 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 128 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 129 | ConsumeToken(); |
| 130 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 131 | "attribute")) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 132 | SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 133 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 134 | } |
| 135 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 136 | SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 137 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 138 | } |
| 139 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 140 | while (true) { |
| 141 | // Allow empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 142 | if (TryConsumeToken(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 143 | continue; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 144 | |
| 145 | // Expect an identifier or declaration specifier (const, int, etc.) |
| 146 | if (Tok.isNot(tok::identifier) && !isDeclarationSpecifier()) |
| 147 | break; |
| 148 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 149 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 150 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 152 | if (Tok.isNot(tok::l_paren)) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 153 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 154 | AttributeList::AS_GNU); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 155 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 156 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 157 | |
| 158 | // Handle "parameterized" attributes |
| 159 | if (!LateAttrs || !isAttributeLateParsed(*AttrName)) { |
| 160 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, 0, |
| 161 | SourceLocation(), AttributeList::AS_GNU, D); |
| 162 | continue; |
| 163 | } |
| 164 | |
| 165 | // Handle attributes with arguments that require late parsing. |
| 166 | LateParsedAttribute *LA = |
| 167 | new LateParsedAttribute(this, *AttrName, AttrNameLoc); |
| 168 | LateAttrs->push_back(LA); |
| 169 | |
| 170 | // Attributes in a class are parsed at the end of the class, along |
| 171 | // with other late-parsed declarations. |
| 172 | if (!ClassStack.empty() && !LateAttrs->parseSoon()) |
| 173 | getCurrentClass().LateParsedDeclarations.push_back(LA); |
| 174 | |
| 175 | // consume everything up to and including the matching right parens |
| 176 | ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false); |
| 177 | |
| 178 | Token Eof; |
| 179 | Eof.startToken(); |
| 180 | Eof.setLocation(Tok.getLocation()); |
| 181 | LA->Toks.push_back(Eof); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 182 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 183 | |
| 184 | if (ExpectAndConsume(tok::r_paren)) |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 185 | SkipUntil(tok::r_paren, StopAtSemi); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 186 | SourceLocation Loc = Tok.getLocation(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 187 | if (ExpectAndConsume(tok::r_paren)) |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 188 | SkipUntil(tok::r_paren, StopAtSemi); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 189 | if (endLoc) |
| 190 | *endLoc = Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 191 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Aaron Ballman | 9feedb8 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 194 | /// \brief Normalizes an attribute name by dropping prefixed and suffixed __. |
| 195 | static StringRef normalizeAttrName(StringRef Name) { |
Richard Smith | d92aa2d | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 196 | if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__")) |
| 197 | Name = Name.drop_front(2).drop_back(2); |
Aaron Ballman | 9feedb8 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 198 | return Name; |
| 199 | } |
| 200 | |
| 201 | /// \brief Determine whether the given attribute has an identifier argument. |
| 202 | static bool attributeHasIdentifierArg(const IdentifierInfo &II) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 203 | #define CLANG_ATTR_IDENTIFIER_ARG_LIST |
Aaron Ballman | 9feedb8 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 204 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 205 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Douglas Gregor | 92eb7d8 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 206 | .Default(false); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 207 | #undef CLANG_ATTR_IDENTIFIER_ARG_LIST |
Douglas Gregor | 92eb7d8 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 208 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 209 | |
Aaron Ballman | 9feedb8 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 210 | /// \brief Determine whether the given attribute parses a type argument. |
| 211 | static bool attributeIsTypeArgAttr(const IdentifierInfo &II) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 212 | #define CLANG_ATTR_TYPE_ARG_LIST |
Aaron Ballman | 9feedb8 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 213 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 214 | #include "clang/Parse/AttrParserStringSwitches.inc" |
Aaron Ballman | 9feedb8 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 215 | .Default(false); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 216 | #undef CLANG_ATTR_TYPE_ARG_LIST |
| 217 | } |
| 218 | |
| 219 | /// \brief Determine whether the given attribute requires parsing its arguments |
| 220 | /// in an unevaluated context or not. |
| 221 | static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II) { |
| 222 | #define CLANG_ATTR_ARG_CONTEXT_LIST |
| 223 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
| 224 | #include "clang/Parse/AttrParserStringSwitches.inc" |
| 225 | .Default(false); |
| 226 | #undef CLANG_ATTR_ARG_CONTEXT_LIST |
Aaron Ballman | 9feedb8 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Richard Smith | 8edabd9 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 229 | IdentifierLoc *Parser::ParseIdentifierLoc() { |
| 230 | assert(Tok.is(tok::identifier) && "expected an identifier"); |
| 231 | IdentifierLoc *IL = IdentifierLoc::create(Actions.Context, |
| 232 | Tok.getLocation(), |
| 233 | Tok.getIdentifierInfo()); |
| 234 | ConsumeToken(); |
| 235 | return IL; |
| 236 | } |
| 237 | |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 238 | void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName, |
| 239 | SourceLocation AttrNameLoc, |
| 240 | ParsedAttributes &Attrs, |
| 241 | SourceLocation *EndLoc) { |
| 242 | BalancedDelimiterTracker Parens(*this, tok::l_paren); |
| 243 | Parens.consumeOpen(); |
| 244 | |
| 245 | TypeResult T; |
| 246 | if (Tok.isNot(tok::r_paren)) |
| 247 | T = ParseTypeName(); |
| 248 | |
| 249 | if (Parens.consumeClose()) |
| 250 | return; |
| 251 | |
| 252 | if (T.isInvalid()) |
| 253 | return; |
| 254 | |
| 255 | if (T.isUsable()) |
| 256 | Attrs.addNewTypeAttr(&AttrName, |
| 257 | SourceRange(AttrNameLoc, Parens.getCloseLocation()), 0, |
| 258 | AttrNameLoc, T.get(), AttributeList::AS_GNU); |
| 259 | else |
| 260 | Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()), |
| 261 | 0, AttrNameLoc, 0, 0, AttributeList::AS_GNU); |
| 262 | } |
| 263 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 264 | void Parser::ParseAttributeArgsCommon( |
| 265 | IdentifierInfo *AttrName, SourceLocation AttrNameLoc, |
| 266 | ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName, |
| 267 | SourceLocation ScopeLoc, AttributeList::Syntax Syntax) { |
Richard Smith | d92aa2d | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 268 | // Ignore the left paren location for now. |
| 269 | ConsumeParen(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 270 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 271 | ArgsVector ArgExprs; |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 272 | if (Tok.is(tok::identifier)) { |
Richard Smith | d92aa2d | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 273 | // If this attribute wants an 'identifier' argument, make it so. |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 274 | bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 275 | AttributeList::Kind AttrKind = |
| 276 | AttributeList::getKind(AttrName, ScopeName, Syntax); |
Richard Smith | d92aa2d | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 277 | |
| 278 | // If we don't know how to parse this attribute, but this is the only |
| 279 | // token in this argument, assume it's meant to be an identifier. |
Bill Wendling | 307c92e | 2013-12-05 18:35:37 +0000 | [diff] [blame] | 280 | if (AttrKind == AttributeList::UnknownAttribute || |
| 281 | AttrKind == AttributeList::IgnoredAttribute) { |
Richard Smith | d92aa2d | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 282 | const Token &Next = NextToken(); |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 283 | IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma); |
Richard Smith | d92aa2d | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 284 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 285 | |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 286 | if (IsIdentifierArg) |
| 287 | ArgExprs.push_back(ParseIdentifierLoc()); |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 288 | } |
| 289 | |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 290 | if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) { |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 291 | // Eat the comma. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 292 | if (!ArgExprs.empty()) |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 293 | ConsumeToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 294 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 295 | // Parse the non-empty comma-separated list of expressions. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 296 | do { |
| 297 | std::unique_ptr<EnterExpressionEvaluationContext> Unevaluated; |
| 298 | if (attributeParsedArgsUnevaluated(*AttrName)) |
| 299 | Unevaluated.reset( |
| 300 | new EnterExpressionEvaluationContext(Actions, Sema::Unevaluated)); |
| 301 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 302 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 303 | if (ArgExpr.isInvalid()) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 304 | SkipUntil(tok::r_paren, StopAtSemi); |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 305 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 306 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 307 | ArgExprs.push_back(ArgExpr.release()); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 308 | // Eat the comma, move to the next argument |
| 309 | } while (TryConsumeToken(tok::comma)); |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 310 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 311 | |
| 312 | SourceLocation RParen = Tok.getLocation(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 313 | if (!ExpectAndConsume(tok::r_paren)) { |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 314 | SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc; |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 315 | Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc, |
| 316 | ArgExprs.data(), ArgExprs.size(), Syntax); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 317 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 318 | |
| 319 | if (EndLoc) |
| 320 | *EndLoc = RParen; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 323 | /// Parse the arguments to a parameterized GNU attribute or |
| 324 | /// a C++11 attribute in "gnu" namespace. |
| 325 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 326 | SourceLocation AttrNameLoc, |
| 327 | ParsedAttributes &Attrs, |
| 328 | SourceLocation *EndLoc, |
| 329 | IdentifierInfo *ScopeName, |
| 330 | SourceLocation ScopeLoc, |
| 331 | AttributeList::Syntax Syntax, |
| 332 | Declarator *D) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 333 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 334 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 335 | |
| 336 | AttributeList::Kind AttrKind = |
| 337 | AttributeList::getKind(AttrName, ScopeName, Syntax); |
| 338 | |
| 339 | // Availability attributes have their own grammar. |
| 340 | // FIXME: All these cases fail to pass in the syntax and scope, and might be |
| 341 | // written as C++11 gnu:: attributes. |
| 342 | if (AttrKind == AttributeList::AT_Availability) { |
| 343 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 344 | return; |
| 345 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 346 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 347 | if (AttrKind == AttributeList::AT_ObjCBridgeRelated) { |
| 348 | ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 349 | return; |
| 350 | } |
| 351 | |
| 352 | // Type safety attributes have their own grammar. |
| 353 | if (AttrKind == AttributeList::AT_TypeTagForDatatype) { |
| 354 | ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 355 | return; |
| 356 | } |
| 357 | |
| 358 | // Some attributes expect solely a type parameter. |
| 359 | if (attributeIsTypeArgAttr(*AttrName)) { |
| 360 | ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 361 | return; |
| 362 | } |
| 363 | |
| 364 | // These may refer to the function arguments, but need to be parsed early to |
| 365 | // participate in determining whether it's a redeclaration. |
| 366 | std::unique_ptr<ParseScope> PrototypeScope; |
| 367 | if (AttrName->isStr("enable_if") && D && D->isFunctionDeclarator()) { |
| 368 | DeclaratorChunk::FunctionTypeInfo FTI = D->getFunctionTypeInfo(); |
| 369 | PrototypeScope.reset(new ParseScope(this, Scope::FunctionPrototypeScope | |
| 370 | Scope::FunctionDeclarationScope | |
| 371 | Scope::DeclScope)); |
| 372 | for (unsigned i = 0; i != FTI.NumParams; ++i) { |
| 373 | ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param); |
| 374 | Actions.ActOnReenterCXXMethodParameter(getCurScope(), Param); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName, |
| 379 | ScopeLoc, Syntax); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 380 | } |
| 381 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 382 | bool Parser::ParseMicrosoftDeclSpecArgs(IdentifierInfo *AttrName, |
| 383 | SourceLocation AttrNameLoc, |
| 384 | ParsedAttributes &Attrs) { |
| 385 | // If the attribute isn't known, we will not attempt to parse any |
| 386 | // arguments. |
| 387 | if (!hasAttribute(AttrSyntax::Declspec, nullptr, AttrName, |
| 388 | getTargetInfo().getTriple(), getLangOpts())) { |
| 389 | // Eat the left paren, then skip to the ending right paren. |
| 390 | ConsumeParen(); |
| 391 | SkipUntil(tok::r_paren); |
| 392 | return false; |
| 393 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 394 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 395 | if (AttrName->getName() == "property") { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 396 | // The property declspec is more complex in that it can take one or two |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 397 | // assignment expressions as a parameter, but the lhs of the assignment |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 398 | // must be named get or put. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 399 | |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 400 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 401 | T.expectAndConsume(diag::err_expected_lparen_after, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 402 | AttrName->getNameStart(), tok::r_paren); |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 403 | |
| 404 | enum AccessorKind { |
| 405 | AK_Invalid = -1, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 406 | AK_Put = 0, |
| 407 | AK_Get = 1 // indices into AccessorNames |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 408 | }; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 409 | IdentifierInfo *AccessorNames[] = {0, 0}; |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 410 | bool HasInvalidAccessor = false; |
| 411 | |
| 412 | // Parse the accessor specifications. |
| 413 | while (true) { |
| 414 | // Stop if this doesn't look like an accessor spec. |
| 415 | if (!Tok.is(tok::identifier)) { |
| 416 | // If the user wrote a completely empty list, use a special diagnostic. |
| 417 | if (Tok.is(tok::r_paren) && !HasInvalidAccessor && |
| 418 | AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 419 | Diag(AttrNameLoc, diag::err_ms_property_no_getter_or_putter); |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 420 | break; |
| 421 | } |
| 422 | |
| 423 | Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor); |
| 424 | break; |
| 425 | } |
| 426 | |
| 427 | AccessorKind Kind; |
| 428 | SourceLocation KindLoc = Tok.getLocation(); |
| 429 | StringRef KindStr = Tok.getIdentifierInfo()->getName(); |
| 430 | if (KindStr == "get") { |
| 431 | Kind = AK_Get; |
| 432 | } else if (KindStr == "put") { |
| 433 | Kind = AK_Put; |
| 434 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 435 | // Recover from the common mistake of using 'set' instead of 'put'. |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 436 | } else if (KindStr == "set") { |
| 437 | Diag(KindLoc, diag::err_ms_property_has_set_accessor) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 438 | << FixItHint::CreateReplacement(KindLoc, "put"); |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 439 | Kind = AK_Put; |
| 440 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 441 | // Handle the mistake of forgetting the accessor kind by skipping |
| 442 | // this accessor. |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 443 | } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) { |
| 444 | Diag(KindLoc, diag::err_ms_property_missing_accessor_kind); |
| 445 | ConsumeToken(); |
| 446 | HasInvalidAccessor = true; |
| 447 | goto next_property_accessor; |
| 448 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 449 | // Otherwise, complain about the unknown accessor kind. |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 450 | } else { |
| 451 | Diag(KindLoc, diag::err_ms_property_unknown_accessor); |
| 452 | HasInvalidAccessor = true; |
| 453 | Kind = AK_Invalid; |
| 454 | |
| 455 | // Try to keep parsing unless it doesn't look like an accessor spec. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 456 | if (!NextToken().is(tok::equal)) |
| 457 | break; |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | // Consume the identifier. |
| 461 | ConsumeToken(); |
| 462 | |
| 463 | // Consume the '='. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 464 | if (!TryConsumeToken(tok::equal)) { |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 465 | Diag(Tok.getLocation(), diag::err_ms_property_expected_equal) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 466 | << KindStr; |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 467 | break; |
| 468 | } |
| 469 | |
| 470 | // Expect the method name. |
| 471 | if (!Tok.is(tok::identifier)) { |
| 472 | Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name); |
| 473 | break; |
| 474 | } |
| 475 | |
| 476 | if (Kind == AK_Invalid) { |
| 477 | // Just drop invalid accessors. |
| 478 | } else if (AccessorNames[Kind] != NULL) { |
| 479 | // Complain about the repeated accessor, ignore it, and keep parsing. |
| 480 | Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr; |
| 481 | } else { |
| 482 | AccessorNames[Kind] = Tok.getIdentifierInfo(); |
| 483 | } |
| 484 | ConsumeToken(); |
| 485 | |
| 486 | next_property_accessor: |
| 487 | // Keep processing accessors until we run out. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 488 | if (TryConsumeToken(tok::comma)) |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 489 | continue; |
| 490 | |
| 491 | // If we run into the ')', stop without consuming it. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 492 | if (Tok.is(tok::r_paren)) |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 493 | break; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 494 | |
| 495 | Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen); |
| 496 | break; |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | // Only add the property attribute if it was well-formed. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 500 | if (!HasInvalidAccessor) |
| 501 | Attrs.addNewPropertyAttr(AttrName, AttrNameLoc, 0, SourceLocation(), |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 502 | AccessorNames[AK_Get], AccessorNames[AK_Put], |
| 503 | AttributeList::AS_Declspec); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 504 | T.skipToEnd(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 505 | return !HasInvalidAccessor; |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 506 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 507 | |
| 508 | ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, nullptr, nullptr, |
| 509 | SourceLocation(), AttributeList::AS_Declspec); |
| 510 | return true; |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 513 | /// [MS] decl-specifier: |
| 514 | /// __declspec ( extended-decl-modifier-seq ) |
| 515 | /// |
| 516 | /// [MS] extended-decl-modifier-seq: |
| 517 | /// extended-decl-modifier[opt] |
| 518 | /// extended-decl-modifier extended-decl-modifier-seq |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 519 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) { |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 520 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 521 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 522 | ConsumeToken(); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 523 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 524 | if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec", |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 525 | tok::r_paren)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 526 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 527 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 528 | // An empty declspec is perfectly legal and should not warn. Additionally, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 529 | // you can specify multiple attributes per declspec. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 530 | while (Tok.isNot(tok::r_paren)) { |
| 531 | // Attribute not present. |
| 532 | if (TryConsumeToken(tok::comma)) |
| 533 | continue; |
| 534 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 535 | // We expect either a well-known identifier or a generic string. Anything |
| 536 | // else is a malformed declspec. |
| 537 | bool IsString = Tok.getKind() == tok::string_literal ? true : false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 538 | if (!IsString && Tok.getKind() != tok::identifier && |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 539 | Tok.getKind() != tok::kw_restrict) { |
| 540 | Diag(Tok, diag::err_ms_declspec_type); |
| 541 | T.skipToEnd(); |
| 542 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 543 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 544 | |
| 545 | IdentifierInfo *AttrName; |
| 546 | SourceLocation AttrNameLoc; |
| 547 | if (IsString) { |
| 548 | SmallString<8> StrBuffer; |
| 549 | bool Invalid = false; |
| 550 | StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid); |
| 551 | if (Invalid) { |
| 552 | T.skipToEnd(); |
| 553 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 554 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 555 | AttrName = PP.getIdentifierInfo(Str); |
| 556 | AttrNameLoc = ConsumeStringToken(); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 557 | } else { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 558 | AttrName = Tok.getIdentifierInfo(); |
| 559 | AttrNameLoc = ConsumeToken(); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 560 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 561 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 562 | bool AttrHandled = false; |
| 563 | |
| 564 | // Parse attribute arguments. |
| 565 | if (Tok.is(tok::l_paren)) |
| 566 | AttrHandled = ParseMicrosoftDeclSpecArgs(AttrName, AttrNameLoc, Attrs); |
| 567 | else if (AttrName->getName() == "property") |
| 568 | // The property attribute must have an argument list. |
| 569 | Diag(Tok.getLocation(), diag::err_expected_lparen_after) |
| 570 | << AttrName->getName(); |
| 571 | |
| 572 | if (!AttrHandled) |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 573 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 574 | AttributeList::AS_Declspec); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 575 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 576 | T.consumeClose(); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 577 | } |
| 578 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 579 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 580 | // Treat these like attributes |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 581 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 582 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 583 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 584 | Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) || |
| 585 | Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 586 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 587 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 588 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 589 | AttributeList::AS_Keyword); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 590 | } |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 591 | } |
| 592 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 593 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 594 | // Treat these like attributes |
| 595 | while (Tok.is(tok::kw___pascal)) { |
| 596 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 597 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 598 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 599 | AttributeList::AS_Keyword); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 600 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 603 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 604 | // Treat these like attributes |
| 605 | while (Tok.is(tok::kw___kernel)) { |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 606 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 607 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 608 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 609 | AttributeList::AS_Keyword); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 610 | } |
| 611 | } |
| 612 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 613 | void Parser::ParseOpenCLQualifiers(ParsedAttributes &Attrs) { |
| 614 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 615 | SourceLocation AttrNameLoc = Tok.getLocation(); |
| 616 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 617 | AttributeList::AS_Keyword); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 618 | } |
| 619 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 620 | /// \brief Parse a version number. |
| 621 | /// |
| 622 | /// version: |
| 623 | /// simple-integer |
| 624 | /// simple-integer ',' simple-integer |
| 625 | /// simple-integer ',' simple-integer ',' simple-integer |
| 626 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 627 | Range = Tok.getLocation(); |
| 628 | |
| 629 | if (!Tok.is(tok::numeric_constant)) { |
| 630 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 631 | SkipUntil(tok::comma, tok::r_paren, |
| 632 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 633 | return VersionTuple(); |
| 634 | } |
| 635 | |
| 636 | // Parse the major (and possibly minor and subminor) versions, which |
| 637 | // are stored in the numeric constant. We utilize a quirk of the |
| 638 | // lexer, which is that it handles something like 1.2.3 as a single |
| 639 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 640 | SmallString<512> Buffer; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 641 | Buffer.resize(Tok.getLength()+1); |
| 642 | const char *ThisTokBegin = &Buffer[0]; |
| 643 | |
| 644 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 645 | bool Invalid = false; |
| 646 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 647 | if (Invalid) |
| 648 | return VersionTuple(); |
| 649 | |
| 650 | // Parse the major version. |
| 651 | unsigned AfterMajor = 0; |
| 652 | unsigned Major = 0; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 653 | while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 654 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 655 | ++AfterMajor; |
| 656 | } |
| 657 | |
| 658 | if (AfterMajor == 0) { |
| 659 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 660 | SkipUntil(tok::comma, tok::r_paren, |
| 661 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 662 | return VersionTuple(); |
| 663 | } |
| 664 | |
| 665 | if (AfterMajor == ActualLength) { |
| 666 | ConsumeToken(); |
| 667 | |
| 668 | // We only had a single version component. |
| 669 | if (Major == 0) { |
| 670 | Diag(Tok, diag::err_zero_version); |
| 671 | return VersionTuple(); |
| 672 | } |
| 673 | |
| 674 | return VersionTuple(Major); |
| 675 | } |
| 676 | |
| 677 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 678 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 679 | SkipUntil(tok::comma, tok::r_paren, |
| 680 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 681 | return VersionTuple(); |
| 682 | } |
| 683 | |
| 684 | // Parse the minor version. |
| 685 | unsigned AfterMinor = AfterMajor + 1; |
| 686 | unsigned Minor = 0; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 687 | while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 688 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 689 | ++AfterMinor; |
| 690 | } |
| 691 | |
| 692 | if (AfterMinor == ActualLength) { |
| 693 | ConsumeToken(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 694 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 695 | // We had major.minor. |
| 696 | if (Major == 0 && Minor == 0) { |
| 697 | Diag(Tok, diag::err_zero_version); |
| 698 | return VersionTuple(); |
| 699 | } |
| 700 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 701 | return VersionTuple(Major, Minor); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | // If what follows is not a '.', we have a problem. |
| 705 | if (ThisTokBegin[AfterMinor] != '.') { |
| 706 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 707 | SkipUntil(tok::comma, tok::r_paren, |
| 708 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 709 | return VersionTuple(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | // Parse the subminor version. |
| 713 | unsigned AfterSubminor = AfterMinor + 1; |
| 714 | unsigned Subminor = 0; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 715 | while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 716 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 717 | ++AfterSubminor; |
| 718 | } |
| 719 | |
| 720 | if (AfterSubminor != ActualLength) { |
| 721 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 722 | SkipUntil(tok::comma, tok::r_paren, |
| 723 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 724 | return VersionTuple(); |
| 725 | } |
| 726 | ConsumeToken(); |
| 727 | return VersionTuple(Major, Minor, Subminor); |
| 728 | } |
| 729 | |
| 730 | /// \brief Parse the contents of the "availability" attribute. |
| 731 | /// |
| 732 | /// availability-attribute: |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 733 | /// 'availability' '(' platform ',' version-arg-list, opt-message')' |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 734 | /// |
| 735 | /// platform: |
| 736 | /// identifier |
| 737 | /// |
| 738 | /// version-arg-list: |
| 739 | /// version-arg |
| 740 | /// version-arg ',' version-arg-list |
| 741 | /// |
| 742 | /// version-arg: |
| 743 | /// 'introduced' '=' version |
| 744 | /// 'deprecated' '=' version |
Douglas Gregor | 93a7067 | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 745 | /// 'obsoleted' = version |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 746 | /// 'unavailable' |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 747 | /// opt-message: |
| 748 | /// 'message' '=' <string> |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 749 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 750 | SourceLocation AvailabilityLoc, |
| 751 | ParsedAttributes &attrs, |
| 752 | SourceLocation *endLoc) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 753 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 754 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 755 | ExprResult MessageExpr; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 756 | |
| 757 | // Opening '('. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 758 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 759 | if (T.consumeOpen()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 760 | Diag(Tok, diag::err_expected) << tok::l_paren; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 761 | return; |
| 762 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 763 | |
| 764 | // Parse the platform name, |
| 765 | if (Tok.isNot(tok::identifier)) { |
| 766 | Diag(Tok, diag::err_availability_expected_platform); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 767 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 768 | return; |
| 769 | } |
Richard Smith | 8edabd9 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 770 | IdentifierLoc *Platform = ParseIdentifierLoc(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 771 | |
| 772 | // Parse the ',' following the platform name. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 773 | if (ExpectAndConsume(tok::comma)) { |
| 774 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 775 | return; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 776 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 777 | |
| 778 | // If we haven't grabbed the pointers for the identifiers |
| 779 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 780 | if (!Ident_introduced) { |
| 781 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 782 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 783 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 784 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 785 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 789 | SourceLocation UnavailableLoc; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 790 | do { |
| 791 | if (Tok.isNot(tok::identifier)) { |
| 792 | Diag(Tok, diag::err_availability_expected_change); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 793 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 794 | return; |
| 795 | } |
| 796 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 797 | SourceLocation KeywordLoc = ConsumeToken(); |
| 798 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 799 | if (Keyword == Ident_unavailable) { |
| 800 | if (UnavailableLoc.isValid()) { |
| 801 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 802 | << Keyword << SourceRange(UnavailableLoc); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 803 | } |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 804 | UnavailableLoc = KeywordLoc; |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 805 | continue; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 808 | if (Tok.isNot(tok::equal)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 809 | Diag(Tok, diag::err_expected_after) << Keyword << tok::equal; |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 810 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 811 | return; |
| 812 | } |
| 813 | ConsumeToken(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 814 | if (Keyword == Ident_message) { |
Benjamin Kramer | c561714 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 815 | if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals. |
Andy Gibbs | 97f8461 | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 816 | Diag(Tok, diag::err_expected_string_literal) |
| 817 | << /*Source='availability attribute'*/2; |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 818 | SkipUntil(tok::r_paren, StopAtSemi); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 819 | return; |
| 820 | } |
| 821 | MessageExpr = ParseStringLiteralExpression(); |
| 822 | break; |
| 823 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 824 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 825 | SourceRange VersionRange; |
| 826 | VersionTuple Version = ParseVersionTuple(VersionRange); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 827 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 828 | if (Version.empty()) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 829 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 830 | return; |
| 831 | } |
| 832 | |
| 833 | unsigned Index; |
| 834 | if (Keyword == Ident_introduced) |
| 835 | Index = Introduced; |
| 836 | else if (Keyword == Ident_deprecated) |
| 837 | Index = Deprecated; |
| 838 | else if (Keyword == Ident_obsoleted) |
| 839 | Index = Obsoleted; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 840 | else |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 841 | Index = Unknown; |
| 842 | |
| 843 | if (Index < Unknown) { |
| 844 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 845 | Diag(KeywordLoc, diag::err_availability_redundant) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 846 | << Keyword |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 847 | << SourceRange(Changes[Index].KeywordLoc, |
| 848 | Changes[Index].VersionRange.getEnd()); |
| 849 | } |
| 850 | |
| 851 | Changes[Index].KeywordLoc = KeywordLoc; |
| 852 | Changes[Index].Version = Version; |
| 853 | Changes[Index].VersionRange = VersionRange; |
| 854 | } else { |
| 855 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 856 | << Keyword << VersionRange; |
| 857 | } |
| 858 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 859 | } while (TryConsumeToken(tok::comma)); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 860 | |
| 861 | // Closing ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 862 | if (T.consumeClose()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 863 | return; |
| 864 | |
| 865 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 866 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 867 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 868 | // The 'unavailable' availability cannot be combined with any other |
| 869 | // availability changes. Make sure that hasn't happened. |
| 870 | if (UnavailableLoc.isValid()) { |
| 871 | bool Complained = false; |
| 872 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 873 | if (Changes[Index].KeywordLoc.isValid()) { |
| 874 | if (!Complained) { |
| 875 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 876 | << SourceRange(Changes[Index].KeywordLoc, |
| 877 | Changes[Index].VersionRange.getEnd()); |
| 878 | Complained = true; |
| 879 | } |
| 880 | |
| 881 | // Clear out the availability. |
| 882 | Changes[Index] = AvailabilityChange(); |
| 883 | } |
| 884 | } |
| 885 | } |
| 886 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 887 | // Record this attribute |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 888 | attrs.addNew(&Availability, |
| 889 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Fariborz Jahanian | f96708d | 2012-01-23 23:38:32 +0000 | [diff] [blame] | 890 | 0, AvailabilityLoc, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 891 | Platform, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 892 | Changes[Introduced], |
| 893 | Changes[Deprecated], |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 894 | Changes[Obsoleted], |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 895 | UnavailableLoc, MessageExpr.take(), |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 896 | AttributeList::AS_GNU); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 899 | /// \brief Parse the contents of the "objc_bridge_related" attribute. |
| 900 | /// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')' |
| 901 | /// related_class: |
| 902 | /// Identifier |
| 903 | /// |
| 904 | /// opt-class_method: |
| 905 | /// Identifier: | <empty> |
| 906 | /// |
| 907 | /// opt-instance_method: |
| 908 | /// Identifier | <empty> |
| 909 | /// |
| 910 | void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated, |
| 911 | SourceLocation ObjCBridgeRelatedLoc, |
| 912 | ParsedAttributes &attrs, |
| 913 | SourceLocation *endLoc) { |
| 914 | // Opening '('. |
| 915 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 916 | if (T.consumeOpen()) { |
| 917 | Diag(Tok, diag::err_expected) << tok::l_paren; |
| 918 | return; |
| 919 | } |
| 920 | |
| 921 | // Parse the related class name. |
| 922 | if (Tok.isNot(tok::identifier)) { |
| 923 | Diag(Tok, diag::err_objcbridge_related_expected_related_class); |
| 924 | SkipUntil(tok::r_paren, StopAtSemi); |
| 925 | return; |
| 926 | } |
| 927 | IdentifierLoc *RelatedClass = ParseIdentifierLoc(); |
| 928 | if (ExpectAndConsume(tok::comma)) { |
| 929 | SkipUntil(tok::r_paren, StopAtSemi); |
| 930 | return; |
| 931 | } |
| 932 | |
| 933 | // Parse optional class method name. |
| 934 | IdentifierLoc *ClassMethod = 0; |
| 935 | if (Tok.is(tok::identifier)) { |
| 936 | ClassMethod = ParseIdentifierLoc(); |
| 937 | if (!TryConsumeToken(tok::colon)) { |
| 938 | Diag(Tok, diag::err_objcbridge_related_selector_name); |
| 939 | SkipUntil(tok::r_paren, StopAtSemi); |
| 940 | return; |
| 941 | } |
| 942 | } |
| 943 | if (!TryConsumeToken(tok::comma)) { |
| 944 | if (Tok.is(tok::colon)) |
| 945 | Diag(Tok, diag::err_objcbridge_related_selector_name); |
| 946 | else |
| 947 | Diag(Tok, diag::err_expected) << tok::comma; |
| 948 | SkipUntil(tok::r_paren, StopAtSemi); |
| 949 | return; |
| 950 | } |
| 951 | |
| 952 | // Parse optional instance method name. |
| 953 | IdentifierLoc *InstanceMethod = 0; |
| 954 | if (Tok.is(tok::identifier)) |
| 955 | InstanceMethod = ParseIdentifierLoc(); |
| 956 | else if (Tok.isNot(tok::r_paren)) { |
| 957 | Diag(Tok, diag::err_expected) << tok::r_paren; |
| 958 | SkipUntil(tok::r_paren, StopAtSemi); |
| 959 | return; |
| 960 | } |
| 961 | |
| 962 | // Closing ')'. |
| 963 | if (T.consumeClose()) |
| 964 | return; |
| 965 | |
| 966 | if (endLoc) |
| 967 | *endLoc = T.getCloseLocation(); |
| 968 | |
| 969 | // Record this attribute |
| 970 | attrs.addNew(&ObjCBridgeRelated, |
| 971 | SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()), |
| 972 | 0, ObjCBridgeRelatedLoc, |
| 973 | RelatedClass, |
| 974 | ClassMethod, |
| 975 | InstanceMethod, |
| 976 | AttributeList::AS_GNU); |
| 977 | |
| 978 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 979 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 980 | // Late Parsed Attributes: |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 981 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 982 | |
| 983 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 984 | |
| 985 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 986 | Self->ParseLexedAttributes(*Class); |
| 987 | } |
| 988 | |
| 989 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 990 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 994 | /// scope appropriately. |
| 995 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 996 | // Deal with templates |
| 997 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 998 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 999 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 1000 | HasTemplateScope); |
| 1001 | if (HasTemplateScope) |
| 1002 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 1003 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1004 | // Set or update the scope flags. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1005 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1006 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1007 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 1008 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 1009 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1010 | // Enter the scope of nested classes |
| 1011 | if (!AlreadyHasClassScope) |
| 1012 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 1013 | Class.TagOrTemplate); |
Benjamin Kramer | 268efba | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 1014 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1015 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 1016 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 1017 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1018 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1019 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1020 | if (!AlreadyHasClassScope) |
| 1021 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 1022 | Class.TagOrTemplate); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1025 | |
| 1026 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 1027 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 1028 | bool EnterScope, bool OnDefinition) { |
DeLesley Hutchins | 161db02 | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1029 | assert(LAs.parseSoon() && |
| 1030 | "Attribute list should be marked for immediate parsing."); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1031 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 95526a4 | 2012-08-15 22:41:04 +0000 | [diff] [blame] | 1032 | if (D) |
| 1033 | LAs[i]->addDecl(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1034 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | d306cf7 | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 1035 | delete LAs[i]; |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1036 | } |
| 1037 | LAs.clear(); |
| 1038 | } |
| 1039 | |
| 1040 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1041 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 1042 | /// This will be called at the end of parsing a class declaration |
| 1043 | /// for each LateParsedAttribute. We consume the saved tokens and |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1044 | /// create an attribute with the arguments filled in. We add this |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1045 | /// to the Attribute list for the decl. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1046 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 1047 | bool EnterScope, bool OnDefinition) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1048 | // Save the current token position. |
| 1049 | SourceLocation OrigLoc = Tok.getLocation(); |
| 1050 | |
| 1051 | // Append the current token at the end of the new token stream so that it |
| 1052 | // doesn't get lost. |
| 1053 | LA.Toks.push_back(Tok); |
| 1054 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 1055 | // Consume the previously pushed token. |
Argyrios Kyrtzidis | ab2d09b | 2013-03-27 23:58:17 +0000 | [diff] [blame] | 1056 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1057 | |
| 1058 | ParsedAttributes Attrs(AttrFactory); |
| 1059 | SourceLocation endLoc; |
| 1060 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1061 | if (LA.Decls.size() > 0) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1062 | Decl *D = LA.Decls[0]; |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1063 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1064 | RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1065 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1066 | // Allow 'this' within late-parsed attributes. |
Richard Smith | cafeb94 | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 1067 | Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0, |
| 1068 | ND && ND->isCXXInstanceMember()); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1069 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1070 | if (LA.Decls.size() == 1) { |
| 1071 | // If the Decl is templatized, add template parameters to scope. |
| 1072 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 1073 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 1074 | if (HasTemplateScope) |
| 1075 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1076 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1077 | // If the Decl is on a function, add function parameters to the scope. |
| 1078 | bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
| 1079 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope); |
| 1080 | if (HasFunScope) |
| 1081 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1082 | |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1083 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 1084 | 0, SourceLocation(), AttributeList::AS_GNU, 0); |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1085 | |
| 1086 | if (HasFunScope) { |
| 1087 | Actions.ActOnExitFunctionContext(); |
| 1088 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 1089 | } |
| 1090 | if (HasTemplateScope) { |
| 1091 | TempScope.Exit(); |
| 1092 | } |
| 1093 | } else { |
| 1094 | // If there are multiple decls, then the decl cannot be within the |
| 1095 | // function scope. |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1096 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 1097 | 0, SourceLocation(), AttributeList::AS_GNU, 0); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1098 | } |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 1099 | } else { |
| 1100 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 1103 | const AttributeList *AL = Attrs.getList(); |
| 1104 | if (OnDefinition && AL && !AL->isCXX11Attribute() && |
| 1105 | AL->isKnownToGCC()) |
| 1106 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 1107 | << &LA.AttrName; |
| 1108 | |
| 1109 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1110 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1111 | |
| 1112 | if (Tok.getLocation() != OrigLoc) { |
| 1113 | // Due to a parsing error, we either went over the cached tokens or |
| 1114 | // there are still cached tokens left, so we skip the leftover tokens. |
| 1115 | // Since this is an uncommon situation that should be avoided, use the |
| 1116 | // expensive isBeforeInTranslationUnit call. |
| 1117 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 1118 | OrigLoc)) |
| 1119 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
Douglas Gregor | d78ef5b | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 1120 | ConsumeAnyToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1121 | } |
| 1122 | } |
| 1123 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1124 | void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName, |
| 1125 | SourceLocation AttrNameLoc, |
| 1126 | ParsedAttributes &Attrs, |
| 1127 | SourceLocation *EndLoc) { |
| 1128 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 1129 | |
| 1130 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1131 | T.consumeOpen(); |
| 1132 | |
| 1133 | if (Tok.isNot(tok::identifier)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 1134 | Diag(Tok, diag::err_expected) << tok::identifier; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1135 | T.skipToEnd(); |
| 1136 | return; |
| 1137 | } |
Richard Smith | 8edabd9 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 1138 | IdentifierLoc *ArgumentKind = ParseIdentifierLoc(); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1139 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 1140 | if (ExpectAndConsume(tok::comma)) { |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1141 | T.skipToEnd(); |
| 1142 | return; |
| 1143 | } |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1144 | |
| 1145 | SourceRange MatchingCTypeRange; |
| 1146 | TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange); |
| 1147 | if (MatchingCType.isInvalid()) { |
| 1148 | T.skipToEnd(); |
| 1149 | return; |
| 1150 | } |
| 1151 | |
| 1152 | bool LayoutCompatible = false; |
| 1153 | bool MustBeNull = false; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 1154 | while (TryConsumeToken(tok::comma)) { |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1155 | if (Tok.isNot(tok::identifier)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 1156 | Diag(Tok, diag::err_expected) << tok::identifier; |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1157 | T.skipToEnd(); |
| 1158 | return; |
| 1159 | } |
| 1160 | IdentifierInfo *Flag = Tok.getIdentifierInfo(); |
| 1161 | if (Flag->isStr("layout_compatible")) |
| 1162 | LayoutCompatible = true; |
| 1163 | else if (Flag->isStr("must_be_null")) |
| 1164 | MustBeNull = true; |
| 1165 | else { |
| 1166 | Diag(Tok, diag::err_type_safety_unknown_flag) << Flag; |
| 1167 | T.skipToEnd(); |
| 1168 | return; |
| 1169 | } |
| 1170 | ConsumeToken(); // consume flag |
| 1171 | } |
| 1172 | |
| 1173 | if (!T.consumeClose()) { |
| 1174 | Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1175 | ArgumentKind, MatchingCType.release(), |
| 1176 | LayoutCompatible, MustBeNull, |
| 1177 | AttributeList::AS_GNU); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | if (EndLoc) |
| 1181 | *EndLoc = T.getCloseLocation(); |
| 1182 | } |
| 1183 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1184 | /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets |
| 1185 | /// of a C++11 attribute-specifier in a location where an attribute is not |
| 1186 | /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this |
| 1187 | /// situation. |
| 1188 | /// |
| 1189 | /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if |
| 1190 | /// this doesn't appear to actually be an attribute-specifier, and the caller |
| 1191 | /// should try to parse it. |
| 1192 | bool Parser::DiagnoseProhibitedCXX11Attribute() { |
| 1193 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)); |
| 1194 | |
| 1195 | switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) { |
| 1196 | case CAK_NotAttributeSpecifier: |
| 1197 | // No diagnostic: we're in Obj-C++11 and this is not actually an attribute. |
| 1198 | return false; |
| 1199 | |
| 1200 | case CAK_InvalidAttributeSpecifier: |
| 1201 | Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute); |
| 1202 | return false; |
| 1203 | |
| 1204 | case CAK_AttributeSpecifier: |
| 1205 | // Parse and discard the attributes. |
| 1206 | SourceLocation BeginLoc = ConsumeBracket(); |
| 1207 | ConsumeBracket(); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1208 | SkipUntil(tok::r_square); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1209 | assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied"); |
| 1210 | SourceLocation EndLoc = ConsumeBracket(); |
| 1211 | Diag(BeginLoc, diag::err_attributes_not_allowed) |
| 1212 | << SourceRange(BeginLoc, EndLoc); |
| 1213 | return true; |
| 1214 | } |
Chandler Carruth | 2c6dbd7 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 1215 | llvm_unreachable("All cases handled above."); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Richard Smith | 975d52c | 2013-02-20 01:17:14 +0000 | [diff] [blame] | 1218 | /// \brief We have found the opening square brackets of a C++11 |
| 1219 | /// attribute-specifier in a location where an attribute is not permitted, but |
| 1220 | /// we know where the attributes ought to be written. Parse them anyway, and |
| 1221 | /// provide a fixit moving them to the right place. |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1222 | void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs, |
| 1223 | SourceLocation CorrectLocation) { |
| 1224 | assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) || |
| 1225 | Tok.is(tok::kw_alignas)); |
| 1226 | |
| 1227 | // Consume the attributes. |
| 1228 | SourceLocation Loc = Tok.getLocation(); |
| 1229 | ParseCXX11Attributes(Attrs); |
| 1230 | CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true); |
| 1231 | |
| 1232 | Diag(Loc, diag::err_attributes_not_allowed) |
| 1233 | << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange) |
| 1234 | << FixItHint::CreateRemoval(AttrRange); |
| 1235 | } |
| 1236 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1237 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 1238 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 1239 | << attrs.Range; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1240 | } |
| 1241 | |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1242 | void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) { |
| 1243 | AttributeList *AttrList = attrs.getList(); |
| 1244 | while (AttrList) { |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1245 | if (AttrList->isCXX11Attribute()) { |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 1246 | Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr) |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1247 | << AttrList->getName(); |
| 1248 | AttrList->setInvalid(); |
| 1249 | } |
| 1250 | AttrList = AttrList->getNext(); |
| 1251 | } |
| 1252 | } |
| 1253 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1254 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 1255 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1256 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 1257 | /// location of the semicolon in DeclEnd. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1258 | /// |
| 1259 | /// declaration: [C99 6.7] |
| 1260 | /// block-declaration -> |
| 1261 | /// simple-declaration |
| 1262 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1263 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1264 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1265 | /// [C++] using-directive |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1266 | /// [C++] using-declaration |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1267 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1268 | /// others... [FIXME] |
| 1269 | /// |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1270 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 1271 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1272 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1273 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 1274 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | e8cff36 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 1275 | // Must temporarily exit the objective-c container scope for |
| 1276 | // parsing c none objective-c decls. |
| 1277 | ObjCDeclContextSwitch ObjCDC(*this); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1278 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1279 | Decl *SingleDecl = 0; |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1280 | Decl *OwnedType = 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1281 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1282 | case tok::kw_template: |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1283 | case tok::kw_export: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1284 | ProhibitAttributes(attrs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1285 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1286 | break; |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1287 | case tok::kw_inline: |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 1288 | // Could be the start of an inline namespace. Allowed as an ext in C++03. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1289 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1290 | ProhibitAttributes(attrs); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1291 | SourceLocation InlineLoc = ConsumeToken(); |
| 1292 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 1293 | break; |
| 1294 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1295 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1296 | true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1297 | case tok::kw_namespace: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1298 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1299 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1300 | break; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1301 | case tok::kw_using: |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1302 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1303 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1304 | break; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1305 | case tok::kw_static_assert: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1306 | case tok::kw__Static_assert: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1307 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1308 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1309 | break; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1310 | default: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1311 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1312 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1313 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1314 | // This routine returns a DeclGroup, if the thing we parsed only contains a |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1315 | // single decl, convert it now. Alias declarations can also declare a type; |
| 1316 | // include that too if it is present. |
| 1317 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1321 | /// declaration-specifiers init-declarator-list[opt] ';' |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1322 | /// [C++11] attribute-specifier-seq decl-specifier-seq[opt] |
| 1323 | /// init-declarator-list ';' |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1324 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 1325 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1326 | /// |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1327 | /// for-range-declaration: [C++11 6.5p1: stmt.ranged] |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1328 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1329 | /// |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1330 | /// If RequireSemi is false, this does not check for a ';' at the end of the |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1331 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1332 | /// |
| 1333 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 1334 | /// of a simple-declaration. If we find that we are, we also parse the |
| 1335 | /// for-range-initializer, and place it here. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1336 | Parser::DeclGroupPtrTy |
| 1337 | Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context, |
| 1338 | SourceLocation &DeclEnd, |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1339 | ParsedAttributesWithRange &Attrs, |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1340 | bool RequireSemi, ForRangeInit *FRI) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1341 | // Parse the common declaration-specifiers piece. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1342 | ParsingDeclSpec DS(*this); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1343 | |
Bill Wendling | f0cc19f | 2013-11-19 22:56:43 +0000 | [diff] [blame] | 1344 | DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context); |
| 1345 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext); |
| 1346 | |
| 1347 | // If we had a free-standing type definition with a missing semicolon, we |
| 1348 | // may get this far before the problem becomes obvious. |
| 1349 | if (DS.hasTagDefinition() && |
| 1350 | DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext)) |
| 1351 | return DeclGroupPtrTy(); |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1352 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1353 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1354 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1355 | if (Tok.is(tok::semi)) { |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1356 | ProhibitAttributes(Attrs); |
Argyrios Kyrtzidis | 5641b0d | 2012-05-16 23:49:15 +0000 | [diff] [blame] | 1357 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1358 | if (RequireSemi) ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1359 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1360 | DS); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1361 | DS.complete(TheDecl); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1362 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1363 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1364 | |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1365 | DS.takeAttributesFrom(Attrs); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1366 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1367 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1368 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1369 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1370 | /// for a declarator. |
| 1371 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1372 | switch (Tok.getKind()) { |
| 1373 | case tok::annot_cxxscope: |
| 1374 | case tok::annot_template_id: |
| 1375 | case tok::caret: |
| 1376 | case tok::code_completion: |
| 1377 | case tok::coloncolon: |
| 1378 | case tok::ellipsis: |
| 1379 | case tok::kw___attribute: |
| 1380 | case tok::kw_operator: |
| 1381 | case tok::l_paren: |
| 1382 | case tok::star: |
| 1383 | return true; |
| 1384 | |
| 1385 | case tok::amp: |
| 1386 | case tok::ampamp: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1387 | return getLangOpts().CPlusPlus; |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1388 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1389 | case tok::l_square: // Might be an attribute on an unnamed bit-field. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1390 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 && |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1391 | NextToken().is(tok::l_square); |
| 1392 | |
| 1393 | case tok::colon: // Might be a typo for '::' or an unnamed bit-field. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1394 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1395 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1396 | case tok::identifier: |
| 1397 | switch (NextToken().getKind()) { |
| 1398 | case tok::code_completion: |
| 1399 | case tok::coloncolon: |
| 1400 | case tok::comma: |
| 1401 | case tok::equal: |
| 1402 | case tok::equalequal: // Might be a typo for '='. |
| 1403 | case tok::kw_alignas: |
| 1404 | case tok::kw_asm: |
| 1405 | case tok::kw___attribute: |
| 1406 | case tok::l_brace: |
| 1407 | case tok::l_paren: |
| 1408 | case tok::l_square: |
| 1409 | case tok::less: |
| 1410 | case tok::r_brace: |
| 1411 | case tok::r_paren: |
| 1412 | case tok::r_square: |
| 1413 | case tok::semi: |
| 1414 | return true; |
| 1415 | |
| 1416 | case tok::colon: |
| 1417 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1418 | // and in block scope it's probably a label. Inside a class definition, |
| 1419 | // this is a bit-field. |
| 1420 | return Context == Declarator::MemberContext || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1421 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1422 | |
| 1423 | case tok::identifier: // Possible virt-specifier. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1424 | return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken()); |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1425 | |
| 1426 | default: |
| 1427 | return false; |
| 1428 | } |
| 1429 | |
| 1430 | default: |
| 1431 | return false; |
| 1432 | } |
| 1433 | } |
| 1434 | |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1435 | /// Skip until we reach something which seems like a sensible place to pick |
| 1436 | /// up parsing after a malformed declaration. This will sometimes stop sooner |
| 1437 | /// than SkipUntil(tok::r_brace) would, but will never stop later. |
| 1438 | void Parser::SkipMalformedDecl() { |
| 1439 | while (true) { |
| 1440 | switch (Tok.getKind()) { |
| 1441 | case tok::l_brace: |
| 1442 | // Skip until matching }, then stop. We've probably skipped over |
| 1443 | // a malformed class or function definition or similar. |
| 1444 | ConsumeBrace(); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1445 | SkipUntil(tok::r_brace); |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1446 | if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) { |
| 1447 | // This declaration isn't over yet. Keep skipping. |
| 1448 | continue; |
| 1449 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 1450 | TryConsumeToken(tok::semi); |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1451 | return; |
| 1452 | |
| 1453 | case tok::l_square: |
| 1454 | ConsumeBracket(); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1455 | SkipUntil(tok::r_square); |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1456 | continue; |
| 1457 | |
| 1458 | case tok::l_paren: |
| 1459 | ConsumeParen(); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1460 | SkipUntil(tok::r_paren); |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1461 | continue; |
| 1462 | |
| 1463 | case tok::r_brace: |
| 1464 | return; |
| 1465 | |
| 1466 | case tok::semi: |
| 1467 | ConsumeToken(); |
| 1468 | return; |
| 1469 | |
| 1470 | case tok::kw_inline: |
| 1471 | // 'inline namespace' at the start of a line is almost certainly |
Jordan Rose | 94f29f4 | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1472 | // a good place to pick back up parsing, except in an Objective-C |
| 1473 | // @interface context. |
| 1474 | if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) && |
| 1475 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1476 | return; |
| 1477 | break; |
| 1478 | |
| 1479 | case tok::kw_namespace: |
| 1480 | // 'namespace' at the start of a line is almost certainly a good |
Jordan Rose | 94f29f4 | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1481 | // place to pick back up parsing, except in an Objective-C |
| 1482 | // @interface context. |
| 1483 | if (Tok.isAtStartOfLine() && |
| 1484 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
| 1485 | return; |
| 1486 | break; |
| 1487 | |
| 1488 | case tok::at: |
| 1489 | // @end is very much like } in Objective-C contexts. |
| 1490 | if (NextToken().isObjCAtKeyword(tok::objc_end) && |
| 1491 | ParsingInObjCContainer) |
| 1492 | return; |
| 1493 | break; |
| 1494 | |
| 1495 | case tok::minus: |
| 1496 | case tok::plus: |
| 1497 | // - and + probably start new method declarations in Objective-C contexts. |
| 1498 | if (Tok.isAtStartOfLine() && ParsingInObjCContainer) |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1499 | return; |
| 1500 | break; |
| 1501 | |
| 1502 | case tok::eof: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 1503 | case tok::annot_module_begin: |
| 1504 | case tok::annot_module_end: |
| 1505 | case tok::annot_module_include: |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1506 | return; |
| 1507 | |
| 1508 | default: |
| 1509 | break; |
| 1510 | } |
| 1511 | |
| 1512 | ConsumeAnyToken(); |
| 1513 | } |
| 1514 | } |
| 1515 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1516 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1517 | /// definition or a group of object declarations, actually parse the |
| 1518 | /// result. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1519 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1520 | unsigned Context, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1521 | bool AllowFunctionDefinitions, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1522 | SourceLocation *DeclEnd, |
| 1523 | ForRangeInit *FRI) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1524 | // Parse the first declarator. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1525 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1526 | ParseDeclarator(D); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1527 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1528 | // Bail out if the first declarator didn't seem well-formed. |
| 1529 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1530 | SkipMalformedDecl(); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1531 | return DeclGroupPtrTy(); |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1532 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1533 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1534 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1535 | // appropriate function scope after the function Decl has been constructed. |
DeLesley Hutchins | 161db02 | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1536 | // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList. |
| 1537 | LateParsedAttrList LateParsedAttrs(true); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1538 | if (D.isFunctionDeclarator()) |
| 1539 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1540 | |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1541 | // Check to see if we have a function *definition* which must have a body. |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1542 | if (D.isFunctionDeclarator() && |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1543 | // Look at the next token to make sure that this isn't a function |
| 1544 | // declaration. We have to check this because __attribute__ might be the |
| 1545 | // start of a function definition in GCC-extended K&R C. |
Fariborz Jahanian | be1d4ec | 2012-08-10 15:54:40 +0000 | [diff] [blame] | 1546 | !isDeclarationAfterDeclarator()) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1547 | |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1548 | if (AllowFunctionDefinitions) { |
| 1549 | if (isStartOfFunctionDefinition(D)) { |
| 1550 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1551 | Diag(Tok, diag::err_function_declared_typedef); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1552 | |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1553 | // Recover by treating the 'typedef' as spurious. |
| 1554 | DS.ClearStorageClassSpecs(); |
| 1555 | } |
| 1556 | |
| 1557 | Decl *TheDecl = |
| 1558 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
| 1559 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1562 | if (isDeclarationSpecifier()) { |
| 1563 | // If there is an invalid declaration specifier right after the function |
| 1564 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1565 | // actually a body. Just fall through into the code that handles it as a |
| 1566 | // prototype, and let the top-level code handle the erroneous declspec |
| 1567 | // where it would otherwise expect a comma or semicolon. |
| 1568 | } else { |
| 1569 | Diag(Tok, diag::err_expected_fn_body); |
| 1570 | SkipUntil(tok::semi); |
| 1571 | return DeclGroupPtrTy(); |
| 1572 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1573 | } else { |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1574 | if (Tok.is(tok::l_brace)) { |
| 1575 | Diag(Tok, diag::err_function_definition_not_allowed); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 1576 | SkipMalformedDecl(); |
| 1577 | return DeclGroupPtrTy(); |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1578 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1579 | } |
| 1580 | } |
| 1581 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1582 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1583 | return DeclGroupPtrTy(); |
| 1584 | |
| 1585 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1586 | // must parse and analyze the for-range-initializer before the declaration is |
| 1587 | // analyzed. |
Douglas Gregor | 12849d0 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1588 | // |
| 1589 | // Handle the Objective-C for-in loop variable similarly, although we |
| 1590 | // don't need to parse the container in advance. |
| 1591 | if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) { |
| 1592 | bool IsForRangeLoop = false; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 1593 | if (TryConsumeToken(tok::colon, FRI->ColonLoc)) { |
Douglas Gregor | 12849d0 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1594 | IsForRangeLoop = true; |
Douglas Gregor | 12849d0 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1595 | if (Tok.is(tok::l_brace)) |
| 1596 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1597 | else |
| 1598 | FRI->RangeExpr = ParseExpression(); |
| 1599 | } |
| 1600 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1601 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | 12849d0 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1602 | if (IsForRangeLoop) |
| 1603 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1604 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | 6895a64 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 1605 | D.complete(ThisDecl); |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1606 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1609 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1610 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1611 | if (LateParsedAttrs.size() > 0) |
| 1612 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1613 | D.complete(FirstDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1614 | if (FirstDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1615 | DeclsInGroup.push_back(FirstDecl); |
| 1616 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1617 | bool ExpectSemi = Context != Declarator::ForContext; |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 1618 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1619 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1620 | // error, bail out. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 1621 | SourceLocation CommaLoc; |
| 1622 | while (TryConsumeToken(tok::comma, CommaLoc)) { |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1623 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1624 | // This comma was followed by a line-break and something which can't be |
| 1625 | // the start of a declarator. The comma was probably a typo for a |
| 1626 | // semicolon. |
| 1627 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1628 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1629 | ExpectSemi = false; |
| 1630 | break; |
| 1631 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1632 | |
| 1633 | // Parse the next declarator. |
| 1634 | D.clear(); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1635 | D.setCommaLoc(CommaLoc); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1636 | |
| 1637 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1638 | // declaration, these would be part of the declspec. In subsequent |
| 1639 | // declarators, they become part of the declarator itself, so that they |
| 1640 | // don't apply to declarators after *this* one. Examples: |
| 1641 | // short __attribute__((common)) var; -> declspec |
| 1642 | // short var __attribute__((common)); -> declarator |
| 1643 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1644 | MaybeParseGNUAttributes(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1645 | |
| 1646 | ParseDeclarator(D); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1647 | if (!D.isInvalidType()) { |
| 1648 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1649 | D.complete(ThisDecl); |
| 1650 | if (ThisDecl) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1651 | DeclsInGroup.push_back(ThisDecl); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1652 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
| 1655 | if (DeclEnd) |
| 1656 | *DeclEnd = Tok.getLocation(); |
| 1657 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1658 | if (ExpectSemi && |
Chris Lattner | 8bb21d3 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 1659 | ExpectAndConsumeSemi(Context == Declarator::FileContext |
| 1660 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1661 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1662 | // Okay, there was no semicolon and one was expected. If we see a |
| 1663 | // declaration specifier, just assume it was missing and continue parsing. |
| 1664 | // Otherwise things are very confused and we skip to recover. |
| 1665 | if (!isDeclarationSpecifier()) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1666 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 1667 | TryConsumeToken(tok::semi); |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1668 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1671 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1674 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1675 | /// declarator. Returns true on an error. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1676 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1677 | // If a simple-asm-expr is present, parse it. |
| 1678 | if (Tok.is(tok::kw_asm)) { |
| 1679 | SourceLocation Loc; |
| 1680 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1681 | if (AsmLabel.isInvalid()) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1682 | SkipUntil(tok::semi, StopBeforeMatch); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1683 | return true; |
| 1684 | } |
| 1685 | |
| 1686 | D.setAsmLabel(AsmLabel.release()); |
| 1687 | D.SetRangeEnd(Loc); |
| 1688 | } |
| 1689 | |
| 1690 | MaybeParseGNUAttributes(D); |
| 1691 | return false; |
| 1692 | } |
| 1693 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1694 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1695 | /// declarator'. This method parses the remainder of the declaration |
| 1696 | /// (including any attributes or initializer, among other things) and |
| 1697 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1698 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1699 | /// init-declarator: [C99 6.7] |
| 1700 | /// declarator |
| 1701 | /// declarator '=' initializer |
| 1702 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1703 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1704 | /// [C++] declarator initializer[opt] |
| 1705 | /// |
| 1706 | /// [C++] initializer: |
| 1707 | /// [C++] '=' initializer-clause |
| 1708 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1709 | /// [C++0x] '=' 'default' [TODO] |
| 1710 | /// [C++0x] '=' 'delete' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1711 | /// [C++0x] braced-init-list |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1712 | /// |
| 1713 | /// According to the standard grammar, =default and =delete are function |
| 1714 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1715 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1716 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1717 | const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1718 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1719 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1720 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1721 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1722 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1723 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1724 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1725 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1726 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1727 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1728 | switch (TemplateInfo.Kind) { |
| 1729 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1730 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1731 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1732 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1733 | case ParsedTemplateInfo::Template: |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1734 | case ParsedTemplateInfo::ExplicitSpecialization: { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1735 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1736 | *TemplateInfo.TemplateParams, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1737 | D); |
Larisse Voufo | 2521813 | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 1738 | if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl)) |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1739 | // Re-direct this decl to refer to the templated decl so that we can |
| 1740 | // initialize it. |
| 1741 | ThisDecl = VT->getTemplatedDecl(); |
| 1742 | break; |
| 1743 | } |
| 1744 | case ParsedTemplateInfo::ExplicitInstantiation: { |
| 1745 | if (Tok.is(tok::semi)) { |
| 1746 | DeclResult ThisRes = Actions.ActOnExplicitInstantiation( |
| 1747 | getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D); |
| 1748 | if (ThisRes.isInvalid()) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1749 | SkipUntil(tok::semi, StopBeforeMatch); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1750 | return 0; |
| 1751 | } |
| 1752 | ThisDecl = ThisRes.get(); |
| 1753 | } else { |
| 1754 | // FIXME: This check should be for a variable template instantiation only. |
| 1755 | |
| 1756 | // Check that this is a valid instantiation |
| 1757 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 1758 | // If the declarator-id is not a template-id, issue a diagnostic and |
| 1759 | // recover by ignoring the 'template' keyword. |
| 1760 | Diag(Tok, diag::err_template_defn_explicit_instantiation) |
| 1761 | << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc); |
| 1762 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1763 | } else { |
| 1764 | SourceLocation LAngleLoc = |
| 1765 | PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
| 1766 | Diag(D.getIdentifierLoc(), |
| 1767 | diag::err_explicit_instantiation_with_definition) |
| 1768 | << SourceRange(TemplateInfo.TemplateLoc) |
| 1769 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
| 1770 | |
| 1771 | // Recover as if it were an explicit specialization. |
| 1772 | TemplateParameterLists FakedParamLists; |
| 1773 | FakedParamLists.push_back(Actions.ActOnTemplateParameterList( |
| 1774 | 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0, |
| 1775 | LAngleLoc)); |
| 1776 | |
| 1777 | ThisDecl = |
| 1778 | Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D); |
| 1779 | } |
| 1780 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1781 | break; |
| 1782 | } |
| 1783 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1784 | |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1785 | bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType(); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1786 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1787 | // Parse declarator '=' initializer. |
Richard Trieu | d6c7c67 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1788 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | fcaf27e | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1789 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1790 | ConsumeToken(); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1791 | |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1792 | if (Tok.is(tok::kw_delete)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1793 | if (D.isFunctionDeclarator()) |
| 1794 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1795 | << 1 /* delete */; |
| 1796 | else |
| 1797 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Sean Hunt | fe2695e | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1798 | } else if (Tok.is(tok::kw_default)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1799 | if (D.isFunctionDeclarator()) |
Sebastian Redl | ecfcd56 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1800 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1801 | << 0 /* default */; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1802 | else |
| 1803 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1804 | } else { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1805 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1806 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1807 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1808 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1809 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1810 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1811 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Peter Collingbourne | ec98f2f | 2012-07-27 12:56:09 +0000 | [diff] [blame] | 1812 | Actions.FinalizeDeclaration(ThisDecl); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1813 | cutOffParsing(); |
| 1814 | return 0; |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1815 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1816 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1817 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1818 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1819 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1820 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1821 | ExitScope(); |
| 1822 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1823 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1824 | if (Init.isInvalid()) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1825 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1826 | Actions.ActOnInitializerError(ThisDecl); |
| 1827 | } else |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1828 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1829 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1830 | } |
| 1831 | } else if (Tok.is(tok::l_paren)) { |
| 1832 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1833 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1834 | T.consumeOpen(); |
| 1835 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1836 | ExprVector Exprs; |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1837 | CommaLocsTy CommaLocs; |
| 1838 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1839 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1840 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1841 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1844 | if (ParseExpressionList(Exprs, CommaLocs)) { |
David Blaikie | 3ea19c8 | 2012-10-10 23:15:05 +0000 | [diff] [blame] | 1845 | Actions.ActOnInitializerError(ThisDecl); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1846 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1847 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1848 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1849 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1850 | ExitScope(); |
| 1851 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1852 | } else { |
| 1853 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1854 | T.consumeClose(); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1855 | |
| 1856 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1857 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1858 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1859 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1860 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1861 | ExitScope(); |
| 1862 | } |
| 1863 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1864 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 1865 | T.getCloseLocation(), |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1866 | Exprs); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1867 | Actions.AddInitializerToDecl(ThisDecl, Initializer.take(), |
| 1868 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1869 | } |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1870 | } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) && |
Fariborz Jahanian | b0ed95c | 2012-07-03 23:22:13 +0000 | [diff] [blame] | 1871 | (!CurParsedObjCImpl || !D.isFunctionDeclarator())) { |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1872 | // Parse C++0x braced-init-list. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1873 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1874 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1875 | if (D.getCXXScopeSpec().isSet()) { |
| 1876 | EnterScope(0); |
| 1877 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1878 | } |
| 1879 | |
| 1880 | ExprResult Init(ParseBraceInitializer()); |
| 1881 | |
| 1882 | if (D.getCXXScopeSpec().isSet()) { |
| 1883 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1884 | ExitScope(); |
| 1885 | } |
| 1886 | |
| 1887 | if (Init.isInvalid()) { |
| 1888 | Actions.ActOnInitializerError(ThisDecl); |
| 1889 | } else |
| 1890 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1891 | /*DirectInit=*/true, TypeContainsAuto); |
| 1892 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1893 | } else { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1894 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1897 | Actions.FinalizeDeclaration(ThisDecl); |
| 1898 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1899 | return ThisDecl; |
| 1900 | } |
| 1901 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1902 | /// ParseSpecifierQualifierList |
| 1903 | /// specifier-qualifier-list: |
| 1904 | /// type-specifier specifier-qualifier-list[opt] |
| 1905 | /// type-qualifier specifier-qualifier-list[opt] |
| 1906 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 1907 | /// |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1908 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 1909 | DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1910 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1911 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1912 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1913 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1914 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1915 | // Validate declspec for type-name. |
| 1916 | unsigned Specs = DS.getParsedSpecifiers(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 1917 | if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) { |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1918 | Diag(Tok, diag::err_expected_type); |
| 1919 | DS.SetTypeSpecError(); |
| 1920 | } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 1921 | !DS.hasAttributes()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1922 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1923 | if (!DS.hasTypeSpecifier()) |
| 1924 | DS.SetTypeSpecError(); |
| 1925 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1926 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1927 | // Issue diagnostic and remove storage class if present. |
| 1928 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 1929 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1930 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 1931 | else |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 1932 | Diag(DS.getThreadStorageClassSpecLoc(), |
| 1933 | diag::err_typename_invalid_storageclass); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1934 | DS.ClearStorageClassSpecs(); |
| 1935 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1936 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1937 | // Issue diagnostic and remove function specfier if present. |
| 1938 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1939 | if (DS.isInlineSpecified()) |
| 1940 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1941 | if (DS.isVirtualSpecified()) |
| 1942 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1943 | if (DS.isExplicitSpecified()) |
| 1944 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1945 | DS.ClearFunctionSpecs(); |
| 1946 | } |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1947 | |
| 1948 | // Issue diagnostic and remove constexpr specfier if present. |
| 1949 | if (DS.isConstexprSpecified()) { |
| 1950 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 1951 | DS.ClearConstexprSpec(); |
| 1952 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1955 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 1956 | /// specified token is valid after the identifier in a declarator which |
| 1957 | /// immediately follows the declspec. For example, these things are valid: |
| 1958 | /// |
| 1959 | /// int x [ 4]; // direct-declarator |
| 1960 | /// int x ( int y); // direct-declarator |
| 1961 | /// int(int x ) // direct-declarator |
| 1962 | /// int x ; // simple-declaration |
| 1963 | /// int x = 17; // init-declarator-list |
| 1964 | /// int x , y; // init-declarator-list |
| 1965 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1966 | /// int x : 4; // struct-declarator |
Chris Lattner | c83c27a | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 1967 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1968 | /// |
| 1969 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 1970 | /// ')' happens to be valid anyway). |
| 1971 | /// int (x) |
| 1972 | /// |
| 1973 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 1974 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 1975 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1976 | T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon); |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1979 | |
| 1980 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 1981 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 1982 | /// the declspec has no type specifier. In this case, the declspec is either |
| 1983 | /// malformed or is "implicit int" (in K&R and C89). |
| 1984 | /// |
| 1985 | /// This method handles diagnosing this prettily and returns false if the |
| 1986 | /// declspec is done being processed. If it recovers and thinks there may be |
| 1987 | /// other pieces of declspec after it, it returns true. |
| 1988 | /// |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1989 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1990 | const ParsedTemplateInfo &TemplateInfo, |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1991 | AccessSpecifier AS, DeclSpecContext DSC, |
| 1992 | ParsedAttributesWithRange &Attrs) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1993 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1994 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1995 | SourceLocation Loc = Tok.getLocation(); |
| 1996 | // If we see an identifier that is not a type name, we normally would |
| 1997 | // parse it as the identifer being declared. However, when a typename |
| 1998 | // is typo'd or the definition is not included, this will incorrectly |
| 1999 | // parse the typename as the identifier name and fall over misparsing |
| 2000 | // later parts of the diagnostic. |
| 2001 | // |
| 2002 | // As such, we try to do some look-ahead in cases where this would |
| 2003 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 2004 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 2005 | // an identifier with implicit int, we'd get a parse error because the |
| 2006 | // next token is obviously invalid for a type. Parse these as a case |
| 2007 | // with an invalid type specifier. |
| 2008 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2010 | // Since we know that this either implicit int (which is rare) or an |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2011 | // error, do lookahead to try to do better recovery. This never applies |
| 2012 | // within a type specifier. Outside of C++, we allow this even if the |
| 2013 | // language doesn't "officially" support implicit int -- we support |
Richard Smith | 58eb370 | 2013-04-30 22:43:51 +0000 | [diff] [blame] | 2014 | // implicit int as an extension in C99 and C11. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2015 | if (!isTypeSpecifier(DSC) && !getLangOpts().CPlusPlus && |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2016 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2017 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 2018 | // we just avoid eating the identifier, so it will be parsed as the |
| 2019 | // identifier in the declarator. |
| 2020 | return false; |
| 2021 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2022 | |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2023 | if (getLangOpts().CPlusPlus && |
| 2024 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 2025 | // Don't require a type specifier if we have the 'auto' storage class |
| 2026 | // specifier in C++98 -- we'll promote it to a type specifier. |
Richard Smith | b79b17b | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2027 | if (SS) |
| 2028 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2029 | return false; |
| 2030 | } |
| 2031 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2032 | // Otherwise, if we don't consume this token, we are going to emit an |
| 2033 | // error anyway. Try to recover from various common problems. Check |
| 2034 | // to see if this was a reference to a tag name without a tag specified. |
| 2035 | // This is a common problem in C (saying 'foo' instead of 'struct foo'). |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2036 | // |
| 2037 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 2038 | if (SS == 0) { |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2039 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2040 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2041 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2042 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2043 | default: break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2044 | case DeclSpec::TST_enum: |
| 2045 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 2046 | case DeclSpec::TST_union: |
| 2047 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 2048 | case DeclSpec::TST_struct: |
| 2049 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2050 | case DeclSpec::TST_interface: |
| 2051 | TagName="__interface"; FixitTagName = "__interface "; |
| 2052 | TagKind=tok::kw___interface;break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2053 | case DeclSpec::TST_class: |
| 2054 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2055 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2056 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2057 | if (TagName) { |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2058 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 2059 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 2060 | Sema::LookupOrdinaryName); |
| 2061 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2062 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2063 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 2064 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 2065 | |
| 2066 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 2067 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 2068 | I != IEnd; ++I) |
Kaelyn Uhrain | 392b3f5 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 2069 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2070 | << TokenName << TagName; |
| 2071 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2072 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2073 | // Parse this as a tag as if the missing tag were present. |
| 2074 | if (TagKind == tok::kw_enum) |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2075 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2076 | else |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2077 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2078 | /*EnteringContext*/ false, DSC_normal, Attrs); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2079 | return true; |
| 2080 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2081 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2082 | |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2083 | // Determine whether this identifier could plausibly be the name of something |
Richard Smith | 7514db2 | 2012-05-15 21:42:17 +0000 | [diff] [blame] | 2084 | // being declared (with a missing type). |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2085 | if (!isTypeSpecifier(DSC) && |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2086 | (!SS || DSC == DSC_top_level || DSC == DSC_class)) { |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2087 | // Look ahead to the next token to try to figure out what this declaration |
| 2088 | // was supposed to be. |
| 2089 | switch (NextToken().getKind()) { |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2090 | case tok::l_paren: { |
| 2091 | // static x(4); // 'x' is not a type |
| 2092 | // x(int n); // 'x' is not a type |
| 2093 | // x (*p)[]; // 'x' is a type |
| 2094 | // |
| 2095 | // Since we're in an error case (or the rare 'implicit int in C++' MS |
| 2096 | // extension), we can afford to perform a tentative parse to determine |
| 2097 | // which case we're in. |
| 2098 | TentativeParsingAction PA(*this); |
| 2099 | ConsumeToken(); |
| 2100 | TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false); |
| 2101 | PA.Revert(); |
Richard Smith | b79b17b | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2102 | |
| 2103 | if (TPR != TPResult::False()) { |
| 2104 | // The identifier is followed by a parenthesized declarator. |
| 2105 | // It's supposed to be a type. |
| 2106 | break; |
| 2107 | } |
| 2108 | |
| 2109 | // If we're in a context where we could be declaring a constructor, |
| 2110 | // check whether this is a constructor declaration with a bogus name. |
| 2111 | if (DSC == DSC_class || (DSC == DSC_top_level && SS)) { |
| 2112 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2113 | if (Actions.isCurrentClassNameTypo(II, SS)) { |
| 2114 | Diag(Loc, diag::err_constructor_bad_name) |
| 2115 | << Tok.getIdentifierInfo() << II |
| 2116 | << FixItHint::CreateReplacement(Tok.getLocation(), II->getName()); |
| 2117 | Tok.setIdentifierInfo(II); |
| 2118 | } |
| 2119 | } |
| 2120 | // Fall through. |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2121 | } |
Richard Smith | b79b17b | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2122 | case tok::comma: |
| 2123 | case tok::equal: |
| 2124 | case tok::kw_asm: |
| 2125 | case tok::l_brace: |
| 2126 | case tok::l_square: |
| 2127 | case tok::semi: |
| 2128 | // This looks like a variable or function declaration. The type is |
| 2129 | // probably missing. We're done parsing decl-specifiers. |
| 2130 | if (SS) |
| 2131 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
| 2132 | return false; |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2133 | |
| 2134 | default: |
| 2135 | // This is probably supposed to be a type. This includes cases like: |
| 2136 | // int f(itn); |
| 2137 | // struct S { unsinged : 4; }; |
| 2138 | break; |
| 2139 | } |
| 2140 | } |
| 2141 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2142 | // This is almost certainly an invalid type name. Let the action emit a |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2143 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2144 | ParsedType T; |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2145 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2146 | if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T, |
| 2147 | getLangOpts().CPlusPlus && |
| 2148 | NextToken().is(tok::less))) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2149 | // The action emitted a diagnostic, so we don't have to. |
| 2150 | if (T) { |
| 2151 | // The action has suggested that the type T could be used. Set that as |
| 2152 | // the type in the declaration specifiers, consume the would-be type |
| 2153 | // name token, and we're done. |
| 2154 | const char *PrevSpec; |
| 2155 | unsigned DiagID; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2156 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T, |
| 2157 | Actions.getASTContext().getPrintingPolicy()); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2158 | DS.SetRangeEnd(Tok.getLocation()); |
| 2159 | ConsumeToken(); |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2160 | // There may be other declaration specifiers after this. |
| 2161 | return true; |
| 2162 | } else if (II != Tok.getIdentifierInfo()) { |
| 2163 | // If no type was suggested, the correction is to a keyword |
| 2164 | Tok.setKind(II->getTokenID()); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2165 | // There may be other declaration specifiers after this. |
| 2166 | return true; |
| 2167 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2168 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2169 | // Fall through; the action had no suggestion for us. |
| 2170 | } else { |
| 2171 | // The action did not emit a diagnostic, so emit one now. |
| 2172 | SourceRange R; |
| 2173 | if (SS) R = SS->getRange(); |
| 2174 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 2175 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2176 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2177 | // Mark this as an error. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2178 | DS.SetTypeSpecError(); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2179 | DS.SetRangeEnd(Tok.getLocation()); |
| 2180 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2181 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2182 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 2183 | // avoid rippling error messages on subsequent uses of the same type, |
| 2184 | // could be useful if #include was forgotten. |
| 2185 | return false; |
| 2186 | } |
| 2187 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2188 | /// \brief Determine the declaration specifier context from the declarator |
| 2189 | /// context. |
| 2190 | /// |
| 2191 | /// \param Context the declarator context, which is one of the |
| 2192 | /// Declarator::TheContext enumerator values. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2193 | Parser::DeclSpecContext |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2194 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 2195 | if (Context == Declarator::MemberContext) |
| 2196 | return DSC_class; |
| 2197 | if (Context == Declarator::FileContext) |
| 2198 | return DSC_top_level; |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 2199 | if (Context == Declarator::TrailingReturnContext) |
| 2200 | return DSC_trailing; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2201 | if (Context == Declarator::AliasDeclContext || |
| 2202 | Context == Declarator::AliasTemplateContext) |
| 2203 | return DSC_alias_declaration; |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2204 | return DSC_normal; |
| 2205 | } |
| 2206 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2207 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 2208 | /// |
| 2209 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 2210 | /// type. Ideally, the type should be propagated directly into Sema. |
| 2211 | /// |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2212 | /// [C11] type-id |
| 2213 | /// [C11] constant-expression |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2214 | /// [C++0x] type-id ...[opt] |
| 2215 | /// [C++0x] assignment-expression ...[opt] |
| 2216 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 2217 | SourceLocation &EllipsisLoc) { |
| 2218 | ExprResult ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2219 | if (isTypeIdInParens()) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2220 | SourceLocation TypeLoc = Tok.getLocation(); |
| 2221 | ParsedType Ty = ParseTypeName().get(); |
| 2222 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2223 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 2224 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2225 | } else |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2226 | ER = ParseConstantExpression(); |
| 2227 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2228 | if (getLangOpts().CPlusPlus11) |
| 2229 | TryConsumeToken(tok::ellipsis, EllipsisLoc); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2230 | |
| 2231 | return ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
| 2234 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 2235 | /// attribute to Attrs. |
| 2236 | /// |
| 2237 | /// alignment-specifier: |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2238 | /// [C11] '_Alignas' '(' type-id ')' |
| 2239 | /// [C11] '_Alignas' '(' constant-expression ')' |
Richard Smith | 33f04a2 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2240 | /// [C++11] 'alignas' '(' type-id ...[opt] ')' |
| 2241 | /// [C++11] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2242 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2243 | SourceLocation *EndLoc) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2244 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 2245 | "Not an alignment-specifier!"); |
| 2246 | |
Richard Smith | 33f04a2 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2247 | IdentifierInfo *KWName = Tok.getIdentifierInfo(); |
| 2248 | SourceLocation KWLoc = ConsumeToken(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2249 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2250 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2251 | if (T.expectAndConsume()) |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2252 | return; |
| 2253 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2254 | SourceLocation EllipsisLoc; |
| 2255 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2256 | if (ArgExpr.isInvalid()) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2257 | T.skipToEnd(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2258 | return; |
| 2259 | } |
| 2260 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2261 | T.consumeClose(); |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2262 | if (EndLoc) |
| 2263 | *EndLoc = T.getCloseLocation(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2264 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2265 | ArgsVector ArgExprs; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2266 | ArgExprs.push_back(ArgExpr.release()); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2267 | Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1, |
| 2268 | AttributeList::AS_Keyword, EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2269 | } |
| 2270 | |
Bill Wendling | f0cc19f | 2013-11-19 22:56:43 +0000 | [diff] [blame] | 2271 | /// Determine whether we're looking at something that might be a declarator |
| 2272 | /// in a simple-declaration. If it can't possibly be a declarator, maybe |
| 2273 | /// diagnose a missing semicolon after a prior tag definition in the decl |
| 2274 | /// specifier. |
| 2275 | /// |
| 2276 | /// \return \c true if an error occurred and this can't be any kind of |
| 2277 | /// declaration. |
| 2278 | bool |
| 2279 | Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS, |
| 2280 | DeclSpecContext DSContext, |
| 2281 | LateParsedAttrList *LateAttrs) { |
| 2282 | assert(DS.hasTagDefinition() && "shouldn't call this"); |
| 2283 | |
| 2284 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Bill Wendling | f0cc19f | 2013-11-19 22:56:43 +0000 | [diff] [blame] | 2285 | |
| 2286 | if (getLangOpts().CPlusPlus && |
| 2287 | (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) || |
| 2288 | Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) && |
| 2289 | TryAnnotateCXXScopeToken(EnteringContext)) { |
| 2290 | SkipMalformedDecl(); |
| 2291 | return true; |
| 2292 | } |
| 2293 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2294 | bool HasScope = Tok.is(tok::annot_cxxscope); |
| 2295 | // Make a copy in case GetLookAheadToken invalidates the result of NextToken. |
| 2296 | Token AfterScope = HasScope ? NextToken() : Tok; |
| 2297 | |
Bill Wendling | f0cc19f | 2013-11-19 22:56:43 +0000 | [diff] [blame] | 2298 | // Determine whether the following tokens could possibly be a |
| 2299 | // declarator. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2300 | bool MightBeDeclarator = true; |
| 2301 | if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) { |
| 2302 | // A declarator-id can't start with 'typename'. |
| 2303 | MightBeDeclarator = false; |
| 2304 | } else if (AfterScope.is(tok::annot_template_id)) { |
| 2305 | // If we have a type expressed as a template-id, this cannot be a |
| 2306 | // declarator-id (such a type cannot be redeclared in a simple-declaration). |
| 2307 | TemplateIdAnnotation *Annot = |
| 2308 | static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue()); |
| 2309 | if (Annot->Kind == TNK_Type_template) |
| 2310 | MightBeDeclarator = false; |
| 2311 | } else if (AfterScope.is(tok::identifier)) { |
| 2312 | const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken(); |
| 2313 | |
Bill Wendling | f0cc19f | 2013-11-19 22:56:43 +0000 | [diff] [blame] | 2314 | // These tokens cannot come after the declarator-id in a |
| 2315 | // simple-declaration, and are likely to come after a type-specifier. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2316 | if (Next.is(tok::star) || Next.is(tok::amp) || Next.is(tok::ampamp) || |
| 2317 | Next.is(tok::identifier) || Next.is(tok::annot_cxxscope) || |
| 2318 | Next.is(tok::coloncolon)) { |
| 2319 | // Missing a semicolon. |
| 2320 | MightBeDeclarator = false; |
| 2321 | } else if (HasScope) { |
| 2322 | // If the declarator-id has a scope specifier, it must redeclare a |
| 2323 | // previously-declared entity. If that's a type (and this is not a |
| 2324 | // typedef), that's an error. |
| 2325 | CXXScopeSpec SS; |
| 2326 | Actions.RestoreNestedNameSpecifierAnnotation( |
| 2327 | Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS); |
| 2328 | IdentifierInfo *Name = AfterScope.getIdentifierInfo(); |
| 2329 | Sema::NameClassification Classification = Actions.ClassifyName( |
| 2330 | getCurScope(), SS, Name, AfterScope.getLocation(), Next, |
| 2331 | /*IsAddressOfOperand*/false); |
| 2332 | switch (Classification.getKind()) { |
| 2333 | case Sema::NC_Error: |
| 2334 | SkipMalformedDecl(); |
| 2335 | return true; |
Bill Wendling | f0cc19f | 2013-11-19 22:56:43 +0000 | [diff] [blame] | 2336 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2337 | case Sema::NC_Keyword: |
| 2338 | case Sema::NC_NestedNameSpecifier: |
| 2339 | llvm_unreachable("typo correction and nested name specifiers not " |
| 2340 | "possible here"); |
Bill Wendling | f0cc19f | 2013-11-19 22:56:43 +0000 | [diff] [blame] | 2341 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2342 | case Sema::NC_Type: |
| 2343 | case Sema::NC_TypeTemplate: |
| 2344 | // Not a previously-declared non-type entity. |
| 2345 | MightBeDeclarator = false; |
| 2346 | break; |
Bill Wendling | f0cc19f | 2013-11-19 22:56:43 +0000 | [diff] [blame] | 2347 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2348 | case Sema::NC_Unknown: |
| 2349 | case Sema::NC_Expression: |
| 2350 | case Sema::NC_VarTemplate: |
| 2351 | case Sema::NC_FunctionTemplate: |
| 2352 | // Might be a redeclaration of a prior entity. |
| 2353 | break; |
| 2354 | } |
Bill Wendling | f0cc19f | 2013-11-19 22:56:43 +0000 | [diff] [blame] | 2355 | } |
Bill Wendling | f0cc19f | 2013-11-19 22:56:43 +0000 | [diff] [blame] | 2356 | } |
| 2357 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2358 | if (MightBeDeclarator) |
Bill Wendling | f0cc19f | 2013-11-19 22:56:43 +0000 | [diff] [blame] | 2359 | return false; |
| 2360 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2361 | const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy(); |
Bill Wendling | f0cc19f | 2013-11-19 22:56:43 +0000 | [diff] [blame] | 2362 | Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2363 | diag::err_expected_after) |
| 2364 | << DeclSpec::getSpecifierName(DS.getTypeSpecType(), PPol) << tok::semi; |
Bill Wendling | f0cc19f | 2013-11-19 22:56:43 +0000 | [diff] [blame] | 2365 | |
| 2366 | // Try to recover from the typo, by dropping the tag definition and parsing |
| 2367 | // the problematic tokens as a type. |
| 2368 | // |
| 2369 | // FIXME: Split the DeclSpec into pieces for the standalone |
| 2370 | // declaration and pieces for the following declaration, instead |
| 2371 | // of assuming that all the other pieces attach to new declaration, |
| 2372 | // and call ParsedFreeStandingDeclSpec as appropriate. |
| 2373 | DS.ClearTypeSpecType(); |
| 2374 | ParsedTemplateInfo NotATemplate; |
| 2375 | ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs); |
| 2376 | return false; |
| 2377 | } |
| 2378 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2379 | /// ParseDeclarationSpecifiers |
| 2380 | /// declaration-specifiers: [C99 6.7] |
| 2381 | /// storage-class-specifier declaration-specifiers[opt] |
| 2382 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2383 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2384 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2385 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2386 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2387 | /// |
| 2388 | /// storage-class-specifier: [C99 6.7.1] |
| 2389 | /// 'typedef' |
| 2390 | /// 'extern' |
| 2391 | /// 'static' |
| 2392 | /// 'auto' |
| 2393 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2394 | /// [C++] 'mutable' |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2395 | /// [C++11] 'thread_local' |
| 2396 | /// [C11] '_Thread_local' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2397 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2398 | /// function-specifier: [C99 6.7.4] |
| 2399 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2400 | /// [C++] 'virtual' |
| 2401 | /// [C++] 'explicit' |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2402 | /// [OpenCL] '__kernel' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2403 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2404 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2405 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2406 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 2407 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2408 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2409 | AccessSpecifier AS, |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2410 | DeclSpecContext DSContext, |
| 2411 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2412 | if (DS.getSourceRange().isInvalid()) { |
| 2413 | DS.SetRangeStart(Tok.getLocation()); |
| 2414 | DS.SetRangeEnd(Tok.getLocation()); |
| 2415 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2416 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2417 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2418 | bool AttrsLastTime = false; |
| 2419 | ParsedAttributesWithRange attrs(AttrFactory); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2420 | const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2421 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2422 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2423 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2424 | unsigned DiagID = 0; |
| 2425 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2426 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2427 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2428 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2429 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2430 | DoneWithDeclSpec: |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2431 | if (!AttrsLastTime) |
| 2432 | ProhibitAttributes(attrs); |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2433 | else { |
| 2434 | // Reject C++11 attributes that appertain to decl specifiers as |
| 2435 | // we don't support any C++11 attributes that appertain to decl |
| 2436 | // specifiers. This also conforms to what g++ 4.8 is doing. |
| 2437 | ProhibitCXX11Attributes(attrs); |
| 2438 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2439 | DS.takeAttributesFrom(attrs); |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2440 | } |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 2441 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2442 | // If this is not a declaration specifier token, we're done reading decl |
| 2443 | // specifiers. First verify that DeclSpec's are consistent. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2444 | DS.Finish(Diags, PP, Policy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2445 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2446 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2447 | case tok::l_square: |
| 2448 | case tok::kw_alignas: |
Richard Smith | 672edb0 | 2013-02-22 09:15:49 +0000 | [diff] [blame] | 2449 | if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier()) |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2450 | goto DoneWithDeclSpec; |
| 2451 | |
| 2452 | ProhibitAttributes(attrs); |
| 2453 | // FIXME: It would be good to recover by accepting the attributes, |
| 2454 | // but attempting to do that now would cause serious |
| 2455 | // madness in terms of diagnostics. |
| 2456 | attrs.clear(); |
| 2457 | attrs.Range = SourceRange(); |
| 2458 | |
| 2459 | ParseCXX11Attributes(attrs); |
| 2460 | AttrsLastTime = true; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2461 | continue; |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2462 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2463 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2464 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2465 | if (DS.hasTypeSpecifier()) { |
| 2466 | bool AllowNonIdentifiers |
| 2467 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 2468 | Scope::BlockScope | |
| 2469 | Scope::TemplateParamScope | |
| 2470 | Scope::FunctionPrototypeScope | |
| 2471 | Scope::AtCatchScope)) == 0; |
| 2472 | bool AllowNestedNameSpecifiers |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2473 | = DSContext == DSC_top_level || |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2474 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 2475 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2476 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2477 | AllowNonIdentifiers, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2478 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2479 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2482 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 2483 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 2484 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2485 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2486 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2487 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2488 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 2489 | else if (CurParsedObjCImpl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2490 | CCC = Sema::PCC_ObjCImplementation; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2491 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2492 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2493 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2494 | } |
| 2495 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2496 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2497 | // C++ scope specifier. Annotate and loop, or bail out on error. |
Eli Friedman | 5a42820 | 2013-08-15 23:59:20 +0000 | [diff] [blame] | 2498 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2499 | if (!DS.hasTypeSpecifier()) |
| 2500 | DS.SetTypeSpecError(); |
| 2501 | goto DoneWithDeclSpec; |
| 2502 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 2503 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 2504 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2505 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2506 | |
| 2507 | case tok::annot_cxxscope: { |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2508 | if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector()) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2509 | goto DoneWithDeclSpec; |
| 2510 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2511 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 2512 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 2513 | Tok.getAnnotationRange(), |
| 2514 | SS); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2515 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2516 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2517 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2518 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2519 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2520 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2521 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2522 | |
| 2523 | // C++ [class.qual]p2: |
| 2524 | // In a lookup in which the constructor is an acceptable lookup |
| 2525 | // result and the nested-name-specifier nominates a class C: |
| 2526 | // |
| 2527 | // - if the name specified after the |
| 2528 | // nested-name-specifier, when looked up in C, is the |
| 2529 | // injected-class-name of C (Clause 9), or |
| 2530 | // |
| 2531 | // - if the name specified after the nested-name-specifier |
| 2532 | // is the same as the identifier or the |
| 2533 | // simple-template-id's template-name in the last |
| 2534 | // component of the nested-name-specifier, |
| 2535 | // |
| 2536 | // the name is instead considered to name the constructor of |
| 2537 | // class C. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2538 | // |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2539 | // Thus, if the template-name is actually the constructor |
| 2540 | // name, then the code is ill-formed; this interpretation is |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2541 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2542 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2543 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2544 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2545 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2546 | if (isConstructorDeclarator(/*Unqualified*/false)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2547 | // The user meant this to be an out-of-line constructor |
| 2548 | // definition, but template arguments are not allowed |
| 2549 | // there. Just allow this as a constructor; we'll |
| 2550 | // complain about it later. |
| 2551 | goto DoneWithDeclSpec; |
| 2552 | } |
| 2553 | |
| 2554 | // The user meant this to name a type, but it actually names |
| 2555 | // a constructor with some extraneous template |
| 2556 | // arguments. Complain, then parse it as a type as the user |
| 2557 | // intended. |
| 2558 | Diag(TemplateId->TemplateNameLoc, |
| 2559 | diag::err_out_of_line_template_id_names_constructor) |
| 2560 | << TemplateId->Name; |
| 2561 | } |
| 2562 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2563 | DS.getTypeSpecScope() = SS; |
| 2564 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2565 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2566 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2567 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2568 | continue; |
| 2569 | } |
| 2570 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2571 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2572 | DS.getTypeSpecScope() = SS; |
| 2573 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2574 | if (Tok.getAnnotationValue()) { |
| 2575 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2576 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2577 | Tok.getAnnotationEndLoc(), |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2578 | PrevSpec, DiagID, T, Policy); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2579 | if (isInvalid) |
| 2580 | break; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2581 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2582 | else |
| 2583 | DS.SetTypeSpecError(); |
| 2584 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2585 | ConsumeToken(); // The typename |
| 2586 | } |
| 2587 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2588 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2589 | goto DoneWithDeclSpec; |
| 2590 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2591 | // If we're in a context where the identifier could be a class name, |
| 2592 | // check whether this is a constructor declaration. |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2593 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2594 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2595 | &SS)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2596 | if (isConstructorDeclarator(/*Unqualified*/false)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2597 | goto DoneWithDeclSpec; |
| 2598 | |
| 2599 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 2600 | // of the class is qualified in a context where it could name |
| 2601 | // a constructor, its a constructor name. However, we've |
| 2602 | // looked at the declarator, and the user probably meant this |
| 2603 | // to be a type. Complain that it isn't supposed to be treated |
| 2604 | // as a type, then proceed to parse it as a type. |
| 2605 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 2606 | << Next.getIdentifierInfo(); |
| 2607 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2608 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2609 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 2610 | Next.getLocation(), |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2611 | getCurScope(), &SS, |
| 2612 | false, false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2613 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2614 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2615 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2616 | // If the referenced identifier is not a type, then this declspec is |
| 2617 | // erroneous: We already checked about that it has no type specifier, and |
| 2618 | // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2619 | // typename. |
David Blaikie | 7247c88 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 2620 | if (!TypeRep) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2621 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2622 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2623 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) { |
| 2624 | if (!Attrs.empty()) { |
| 2625 | AttrsLastTime = true; |
| 2626 | attrs.takeAllFrom(Attrs); |
| 2627 | } |
| 2628 | continue; |
| 2629 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2630 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2631 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2632 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2633 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2634 | ConsumeToken(); // The C++ scope. |
| 2635 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2636 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2637 | DiagID, TypeRep, Policy); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2638 | if (isInvalid) |
| 2639 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2640 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2641 | DS.SetRangeEnd(Tok.getLocation()); |
| 2642 | ConsumeToken(); // The typename. |
| 2643 | |
| 2644 | continue; |
| 2645 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2646 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2647 | case tok::annot_typename: { |
Bill Wendling | f0cc19f | 2013-11-19 22:56:43 +0000 | [diff] [blame] | 2648 | // If we've previously seen a tag definition, we were almost surely |
| 2649 | // missing a semicolon after it. |
| 2650 | if (DS.hasTypeSpecifier() && DS.hasTagDefinition()) |
| 2651 | goto DoneWithDeclSpec; |
| 2652 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2653 | if (Tok.getAnnotationValue()) { |
| 2654 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | c43271e | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 2655 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2656 | DiagID, T, Policy); |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2657 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2658 | DS.SetTypeSpecError(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2659 | |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 2660 | if (isInvalid) |
| 2661 | break; |
| 2662 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2663 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2664 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2665 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2666 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2667 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2668 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2669 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2670 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2671 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2672 | continue; |
| 2673 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2674 | |
Douglas Gregor | bfad915 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2675 | case tok::kw___is_signed: |
| 2676 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 2677 | // typically treats it as a trait. If we see __is_signed as it appears |
| 2678 | // in libstdc++, e.g., |
| 2679 | // |
| 2680 | // static const bool __is_signed; |
| 2681 | // |
| 2682 | // then treat __is_signed as an identifier rather than as a keyword. |
| 2683 | if (DS.getTypeSpecType() == TST_bool && |
| 2684 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
Bill Wendling | 03e463e | 2013-12-16 02:32:55 +0000 | [diff] [blame] | 2685 | DS.getStorageClassSpec() == DeclSpec::SCS_static) |
| 2686 | TryKeywordIdentFallback(true); |
Douglas Gregor | bfad915 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2687 | |
| 2688 | // We're done with the declaration-specifiers. |
| 2689 | goto DoneWithDeclSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2690 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2691 | // typedef-name |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2692 | case tok::kw_decltype: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2693 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2694 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 2695 | // so handle it as such. This is important for ctor parsing. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2696 | if (getLangOpts().CPlusPlus) { |
Eli Friedman | 5a42820 | 2013-08-15 23:59:20 +0000 | [diff] [blame] | 2697 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2698 | if (!DS.hasTypeSpecifier()) |
| 2699 | DS.SetTypeSpecError(); |
| 2700 | goto DoneWithDeclSpec; |
| 2701 | } |
| 2702 | if (!Tok.is(tok::identifier)) |
| 2703 | continue; |
| 2704 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2705 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2706 | // This identifier can only be a typedef name if we haven't already seen |
| 2707 | // a type-specifier. Without this check we misparse: |
| 2708 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 2709 | if (DS.hasTypeSpecifier()) |
| 2710 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2711 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2712 | // Check for need to substitute AltiVec keyword tokens. |
| 2713 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2714 | break; |
| 2715 | |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2716 | // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not |
| 2717 | // allow the use of a typedef name as a type specifier. |
| 2718 | if (DS.isTypeAltiVecVector()) |
| 2719 | goto DoneWithDeclSpec; |
| 2720 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2721 | ParsedType TypeRep = |
| 2722 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 2723 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2724 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2725 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 2726 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2727 | if (!TypeRep) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2728 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2729 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) { |
| 2730 | if (!Attrs.empty()) { |
| 2731 | AttrsLastTime = true; |
| 2732 | attrs.takeAllFrom(Attrs); |
| 2733 | } |
| 2734 | continue; |
| 2735 | } |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2736 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2737 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2738 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2739 | // If we're in a context where the identifier could be a class name, |
| 2740 | // check whether this is a constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2741 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2742 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2743 | isConstructorDeclarator(/*Unqualified*/true)) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2744 | goto DoneWithDeclSpec; |
| 2745 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2746 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2747 | DiagID, TypeRep, Policy); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2748 | if (isInvalid) |
| 2749 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2750 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2751 | DS.SetRangeEnd(Tok.getLocation()); |
| 2752 | ConsumeToken(); // The identifier |
| 2753 | |
| 2754 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2755 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2756 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2757 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2758 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2759 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2760 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2761 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2762 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2763 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2764 | |
| 2765 | // type-name |
| 2766 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2767 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2768 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2769 | // This template-id does not refer to a type name, so we're |
| 2770 | // done with the type-specifiers. |
| 2771 | goto DoneWithDeclSpec; |
| 2772 | } |
| 2773 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2774 | // If we're in a context where the template-id could be a |
| 2775 | // constructor name or specialization, check whether this is a |
| 2776 | // constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2777 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2778 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2779 | isConstructorDeclarator(TemplateId->SS.isEmpty())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2780 | goto DoneWithDeclSpec; |
| 2781 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2782 | // Turn the template-id annotation token into a type annotation |
| 2783 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2784 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2785 | continue; |
| 2786 | } |
| 2787 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2788 | // GNU attributes support. |
| 2789 | case tok::kw___attribute: |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2790 | ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2791 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2792 | |
| 2793 | // Microsoft declspec support. |
| 2794 | case tok::kw___declspec: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2795 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2796 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2797 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2798 | // Microsoft single token adornments. |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2799 | case tok::kw___forceinline: { |
Serge Pavlov | d1fa81c | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2800 | isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID); |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2801 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2802 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2803 | DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2804 | AttributeList::AS_Keyword); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2805 | break; |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2806 | } |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2807 | |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 2808 | case tok::kw___sptr: |
| 2809 | case tok::kw___uptr: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2810 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2811 | case tok::kw___ptr32: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2812 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2813 | case tok::kw___cdecl: |
| 2814 | case tok::kw___stdcall: |
| 2815 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2816 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2817 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2818 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2819 | continue; |
| 2820 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2821 | // Borland single token adornments. |
| 2822 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2823 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2824 | continue; |
| 2825 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2826 | // OpenCL single token adornments. |
| 2827 | case tok::kw___kernel: |
| 2828 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2829 | continue; |
| 2830 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2831 | // storage-class-specifier |
| 2832 | case tok::kw_typedef: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2833 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2834 | PrevSpec, DiagID, Policy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2835 | break; |
| 2836 | case tok::kw_extern: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2837 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2838 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2839 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2840 | PrevSpec, DiagID, Policy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2841 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2842 | case tok::kw___private_extern__: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2843 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2844 | Loc, PrevSpec, DiagID, Policy); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2845 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2846 | case tok::kw_static: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2847 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2848 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2849 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2850 | PrevSpec, DiagID, Policy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2851 | break; |
| 2852 | case tok::kw_auto: |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2853 | if (getLangOpts().CPlusPlus11) { |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2854 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2855 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2856 | PrevSpec, DiagID, Policy); |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2857 | if (!isInvalid) |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2858 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2859 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2860 | } else |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2861 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2862 | DiagID, Policy); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2863 | } else |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2864 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2865 | PrevSpec, DiagID, Policy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2866 | break; |
| 2867 | case tok::kw_register: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2868 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2869 | PrevSpec, DiagID, Policy); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2870 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2871 | case tok::kw_mutable: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2872 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2873 | PrevSpec, DiagID, Policy); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2874 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2875 | case tok::kw___thread: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2876 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc, |
| 2877 | PrevSpec, DiagID); |
| 2878 | break; |
| 2879 | case tok::kw_thread_local: |
| 2880 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc, |
| 2881 | PrevSpec, DiagID); |
| 2882 | break; |
| 2883 | case tok::kw__Thread_local: |
| 2884 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local, |
| 2885 | Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2886 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2887 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2888 | // function-specifier |
| 2889 | case tok::kw_inline: |
Serge Pavlov | d1fa81c | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2890 | isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2891 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2892 | case tok::kw_virtual: |
Serge Pavlov | d1fa81c | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2893 | isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2894 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2895 | case tok::kw_explicit: |
Serge Pavlov | d1fa81c | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2896 | isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2897 | break; |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 2898 | case tok::kw__Noreturn: |
| 2899 | if (!getLangOpts().C11) |
| 2900 | Diag(Loc, diag::ext_c11_noreturn); |
Serge Pavlov | d1fa81c | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2901 | isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID); |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 2902 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2903 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2904 | // alignment-specifier |
| 2905 | case tok::kw__Alignas: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2906 | if (!getLangOpts().C11) |
Jordan Rose | f70a886 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2907 | Diag(Tok, diag::ext_c11_alignment) << Tok.getName(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2908 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2909 | continue; |
| 2910 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2911 | // friend |
| 2912 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2913 | if (DSContext == DSC_class) |
| 2914 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 2915 | else { |
| 2916 | PrevSpec = ""; // not actually used by the diagnostic |
| 2917 | DiagID = diag::err_friend_invalid_in_context; |
| 2918 | isInvalid = true; |
| 2919 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2920 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2921 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2922 | // Modules |
| 2923 | case tok::kw___module_private__: |
| 2924 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 2925 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2926 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2927 | // constexpr |
| 2928 | case tok::kw_constexpr: |
| 2929 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 2930 | break; |
| 2931 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2932 | // type-specifier |
| 2933 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2934 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2935 | DiagID, Policy); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2936 | break; |
| 2937 | case tok::kw_long: |
| 2938 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2939 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2940 | DiagID, Policy); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2941 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2942 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2943 | DiagID, Policy); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2944 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2945 | case tok::kw___int64: |
| 2946 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2947 | DiagID, Policy); |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2948 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2949 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2950 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 2951 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2952 | break; |
| 2953 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2954 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2955 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2956 | break; |
| 2957 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2958 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2959 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2960 | break; |
| 2961 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2962 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2963 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2964 | break; |
| 2965 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2966 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2967 | DiagID, Policy); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2968 | break; |
| 2969 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2970 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2971 | DiagID, Policy); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2972 | break; |
| 2973 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2974 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2975 | DiagID, Policy); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2976 | break; |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 2977 | case tok::kw___int128: |
| 2978 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2979 | DiagID, Policy); |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 2980 | break; |
| 2981 | case tok::kw_half: |
| 2982 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2983 | DiagID, Policy); |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 2984 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2985 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2986 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2987 | DiagID, Policy); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2988 | break; |
| 2989 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2990 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2991 | DiagID, Policy); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2992 | break; |
| 2993 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2994 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2995 | DiagID, Policy); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2996 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2997 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2998 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 2999 | DiagID, Policy); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3000 | break; |
| 3001 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3002 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3003 | DiagID, Policy); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3004 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3005 | case tok::kw_bool: |
| 3006 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3007 | if (Tok.is(tok::kw_bool) && |
| 3008 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 3009 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 3010 | PrevSpec = ""; // Not used by the diagnostic. |
| 3011 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3012 | // For better error recovery. |
| 3013 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3014 | isInvalid = true; |
| 3015 | } else { |
| 3016 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3017 | DiagID, Policy); |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3018 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3019 | break; |
| 3020 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3021 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3022 | DiagID, Policy); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3023 | break; |
| 3024 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3025 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3026 | DiagID, Policy); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3027 | break; |
| 3028 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3029 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3030 | DiagID, Policy); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3031 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3032 | case tok::kw___vector: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3033 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy); |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3034 | break; |
| 3035 | case tok::kw___pixel: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3036 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy); |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3037 | break; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3038 | case tok::kw___unknown_anytype: |
| 3039 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3040 | PrevSpec, DiagID, Policy); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3041 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3042 | |
| 3043 | // class-specifier: |
| 3044 | case tok::kw_class: |
| 3045 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3046 | case tok::kw___interface: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3047 | case tok::kw_union: { |
| 3048 | tok::TokenKind Kind = Tok.getKind(); |
| 3049 | ConsumeToken(); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3050 | |
| 3051 | // These are attributes following class specifiers. |
| 3052 | // To produce better diagnostic, we parse them when |
| 3053 | // parsing class specifier. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3054 | ParsedAttributesWithRange Attributes(AttrFactory); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3055 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3056 | EnteringContext, DSContext, Attributes); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3057 | |
| 3058 | // If there are attributes following class specifier, |
| 3059 | // take them over and handle them here. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3060 | if (!Attributes.empty()) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3061 | AttrsLastTime = true; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3062 | attrs.takeAllFrom(Attributes); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3063 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3064 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3065 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3066 | |
| 3067 | // enum-specifier: |
| 3068 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3069 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3070 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3071 | continue; |
| 3072 | |
| 3073 | // cv-qualifier: |
| 3074 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3075 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3076 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3077 | break; |
| 3078 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3079 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3080 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3081 | break; |
| 3082 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3083 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3084 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3085 | break; |
| 3086 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3087 | // C++ typename-specifier: |
| 3088 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3089 | if (TryAnnotateTypeOrScopeToken()) { |
| 3090 | DS.SetTypeSpecError(); |
| 3091 | goto DoneWithDeclSpec; |
| 3092 | } |
| 3093 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3094 | continue; |
| 3095 | break; |
| 3096 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3097 | // GNU typeof support. |
| 3098 | case tok::kw_typeof: |
| 3099 | ParseTypeofSpecifier(DS); |
| 3100 | continue; |
| 3101 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3102 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 3103 | ParseDecltypeSpecifier(DS); |
| 3104 | continue; |
| 3105 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3106 | case tok::kw___underlying_type: |
| 3107 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3108 | continue; |
| 3109 | |
| 3110 | case tok::kw__Atomic: |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3111 | // C11 6.7.2.4/4: |
| 3112 | // If the _Atomic keyword is immediately followed by a left parenthesis, |
| 3113 | // it is interpreted as a type specifier (with a type name), not as a |
| 3114 | // type qualifier. |
| 3115 | if (NextToken().is(tok::l_paren)) { |
| 3116 | ParseAtomicSpecifier(DS); |
| 3117 | continue; |
| 3118 | } |
| 3119 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 3120 | getLangOpts()); |
| 3121 | break; |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3122 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3123 | // OpenCL qualifiers: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3124 | case tok::kw___private: |
| 3125 | case tok::kw___global: |
| 3126 | case tok::kw___local: |
| 3127 | case tok::kw___constant: |
| 3128 | case tok::kw___read_only: |
| 3129 | case tok::kw___write_only: |
| 3130 | case tok::kw___read_write: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3131 | ParseOpenCLQualifiers(DS.getAttributes()); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3132 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3133 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 3134 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3135 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3136 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 3137 | // but we support it. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3138 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3139 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3140 | |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 3141 | if (!ParseObjCProtocolQualifiers(DS)) |
| 3142 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 3143 | << FixItHint::CreateInsertion(Loc, "id") |
| 3144 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3145 | |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 3146 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 3147 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 3148 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3149 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3150 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3151 | if (isInvalid) { |
| 3152 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3153 | assert(DiagID); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3154 | |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 3155 | if (DiagID == diag::ext_duplicate_declspec) |
| 3156 | Diag(Tok, DiagID) |
| 3157 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 3158 | else |
| 3159 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3160 | } |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3161 | |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 3162 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3163 | if (DiagID != diag::err_bool_redeclaration) |
| 3164 | ConsumeToken(); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3165 | |
| 3166 | AttrsLastTime = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3167 | } |
| 3168 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 3169 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3170 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 3171 | /// semicolon. |
| 3172 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3173 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3174 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3175 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3176 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3177 | /// struct-declarator-list: |
| 3178 | /// struct-declarator |
| 3179 | /// struct-declarator-list ',' struct-declarator |
| 3180 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 3181 | /// struct-declarator: |
| 3182 | /// declarator |
| 3183 | /// [GNU] declarator attributes[opt] |
| 3184 | /// declarator[opt] ':' constant-expression |
| 3185 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 3186 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3187 | void Parser:: |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3188 | ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3189 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3190 | if (Tok.is(tok::kw___extension__)) { |
| 3191 | // __extension__ silences extension warnings in the subexpression. |
| 3192 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3193 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3194 | return ParseStructDeclaration(DS, Fields); |
| 3195 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3196 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3197 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3198 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3199 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3200 | // If there are no declarators, this is a free-standing declaration |
| 3201 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3202 | if (Tok.is(tok::semi)) { |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3203 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
| 3204 | DS); |
| 3205 | DS.complete(TheDecl); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3206 | return; |
| 3207 | } |
| 3208 | |
| 3209 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3210 | bool FirstDeclarator = true; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3211 | SourceLocation CommaLoc; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3212 | while (1) { |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3213 | ParsingFieldDeclarator DeclaratorInfo(*this, DS); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3214 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3215 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3216 | // Attributes are only allowed here on successive declarators. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3217 | if (!FirstDeclarator) |
| 3218 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3219 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3220 | /// struct-declarator: declarator |
| 3221 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3222 | if (Tok.isNot(tok::colon)) { |
| 3223 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 3224 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3225 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3226 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3227 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3228 | if (TryConsumeToken(tok::colon)) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3229 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3230 | if (Res.isInvalid()) |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3231 | SkipUntil(tok::semi, StopBeforeMatch); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 3232 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 3233 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3234 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3235 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3236 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3237 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3238 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3239 | // We're done with this declarator; invoke the callback. |
Eli Friedman | 817a886 | 2012-08-08 23:35:12 +0000 | [diff] [blame] | 3240 | Fields.invoke(DeclaratorInfo); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3241 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3242 | // If we don't have a comma, it is either the end of the list (a ';') |
| 3243 | // or an error, bail out. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3244 | if (!TryConsumeToken(tok::comma, CommaLoc)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3245 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3246 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3247 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3248 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3249 | } |
| 3250 | |
| 3251 | /// ParseStructUnionBody |
| 3252 | /// struct-contents: |
| 3253 | /// struct-declaration-list |
| 3254 | /// [EXT] empty |
| 3255 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 3256 | /// struct-declaration-list: |
| 3257 | /// struct-declaration |
| 3258 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3259 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3260 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3261 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3262 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3263 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 3264 | "parsing struct/union body"); |
Andy Gibbs | f50f3f7 | 2013-04-03 09:31:19 +0000 | [diff] [blame] | 3265 | assert(!getLangOpts().CPlusPlus && "C++ declarations not supported"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3266 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3267 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3268 | if (T.consumeOpen()) |
| 3269 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3270 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3271 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3272 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3273 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3274 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3275 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3276 | // While we still have something to read, read the declarations in the struct. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3277 | while (Tok.isNot(tok::r_brace) && !isEofOrEom()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3278 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3279 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3280 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3281 | if (Tok.is(tok::semi)) { |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3282 | ConsumeExtraSemi(InsideStruct, TagType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3283 | continue; |
| 3284 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3285 | |
Andy Gibbs | 74b9fa1 | 2013-04-03 09:46:04 +0000 | [diff] [blame] | 3286 | // Parse _Static_assert declaration. |
| 3287 | if (Tok.is(tok::kw__Static_assert)) { |
| 3288 | SourceLocation DeclEnd; |
| 3289 | ParseStaticAssertDeclaration(DeclEnd); |
| 3290 | continue; |
| 3291 | } |
| 3292 | |
Argyrios Kyrtzidis | bd95745 | 2013-04-18 01:42:35 +0000 | [diff] [blame] | 3293 | if (Tok.is(tok::annot_pragma_pack)) { |
| 3294 | HandlePragmaPack(); |
| 3295 | continue; |
| 3296 | } |
| 3297 | |
| 3298 | if (Tok.is(tok::annot_pragma_align)) { |
| 3299 | HandlePragmaAlign(); |
| 3300 | continue; |
| 3301 | } |
| 3302 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3303 | if (!Tok.is(tok::at)) { |
| 3304 | struct CFieldCallback : FieldCallback { |
| 3305 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3306 | Decl *TagDecl; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3307 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3308 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3309 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3310 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3311 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 3312 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3313 | void invoke(ParsingFieldDeclarator &FD) override { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3314 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3315 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 3316 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 3317 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3318 | FieldDecls.push_back(Field); |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3319 | FD.complete(Field); |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3320 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3321 | } Callback(*this, TagDecl, FieldDecls); |
| 3322 | |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3323 | // Parse all the comma separated declarators. |
| 3324 | ParsingDeclSpec DS(*this); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3325 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3326 | } else { // Handle @defs |
| 3327 | ConsumeToken(); |
| 3328 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 3329 | Diag(Tok, diag::err_unexpected_at); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3330 | SkipUntil(tok::semi); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3331 | continue; |
| 3332 | } |
| 3333 | ConsumeToken(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3334 | ExpectAndConsume(tok::l_paren); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3335 | if (!Tok.is(tok::identifier)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3336 | Diag(Tok, diag::err_expected) << tok::identifier; |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3337 | SkipUntil(tok::semi); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3338 | continue; |
| 3339 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3340 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3341 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3342 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3343 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 3344 | ConsumeToken(); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3345 | ExpectAndConsume(tok::r_paren); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3346 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3347 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3348 | if (TryConsumeToken(tok::semi)) |
| 3349 | continue; |
| 3350 | |
| 3351 | if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3352 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3353 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3354 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3355 | |
| 3356 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 3357 | // Skip to end of block or statement to avoid ext-warning on extra ';'. |
| 3358 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
| 3359 | // If we stopped at a ';', eat it. |
| 3360 | TryConsumeToken(tok::semi); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3361 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3362 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3363 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3364 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3365 | ParsedAttributes attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3366 | // If attributes exist after struct contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3367 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 3368 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3369 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 3370 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3371 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3372 | attrs.getList()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3373 | StructScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3374 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 3375 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3376 | } |
| 3377 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3378 | /// ParseEnumSpecifier |
| 3379 | /// enum-specifier: [C99 6.7.2.2] |
| 3380 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3381 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3382 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3383 | /// '}' attributes[opt] |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 3384 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3385 | /// '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3386 | /// 'enum' identifier |
| 3387 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3388 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3389 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 3390 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3391 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3392 | /// enum-head: [C++11] |
| 3393 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 3394 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 3395 | /// identifier enum-base[opt] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3396 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3397 | /// enum-key: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3398 | /// 'enum' |
| 3399 | /// 'enum' 'class' |
| 3400 | /// 'enum' 'struct' |
| 3401 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3402 | /// enum-base: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3403 | /// ':' type-specifier-seq |
| 3404 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3405 | /// [C++] elaborated-type-specifier: |
| 3406 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 3407 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3408 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 3409 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3410 | AccessSpecifier AS, DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3411 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3412 | if (Tok.is(tok::code_completion)) { |
| 3413 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3414 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3415 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3416 | } |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3417 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3418 | // If attributes exist after tag, parse them. |
| 3419 | ParsedAttributesWithRange attrs(AttrFactory); |
| 3420 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3421 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3422 | |
| 3423 | // If declspecs exist after tag, parse them. |
| 3424 | while (Tok.is(tok::kw___declspec)) |
| 3425 | ParseMicrosoftDeclSpec(attrs); |
| 3426 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3427 | SourceLocation ScopedEnumKWLoc; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3428 | bool IsScopedUsingClassTag = false; |
| 3429 | |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3430 | // In C++11, recognize 'enum class' and 'enum struct'. |
Richard Trieu | ed5a292 | 2013-04-23 02:47:36 +0000 | [diff] [blame] | 3431 | if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) { |
| 3432 | Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum |
| 3433 | : diag::ext_scoped_enum); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3434 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3435 | ScopedEnumKWLoc = ConsumeToken(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3436 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3437 | // Attributes are not allowed between these keywords. Diagnose, |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3438 | // but then just treat them like they appeared in the right place. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3439 | ProhibitAttributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3440 | |
| 3441 | // They are allowed afterwards, though. |
| 3442 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3443 | MaybeParseCXX11Attributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3444 | while (Tok.is(tok::kw___declspec)) |
| 3445 | ParseMicrosoftDeclSpec(attrs); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3446 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3447 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3448 | // C++11 [temp.explicit]p12: |
| 3449 | // The usual access controls do not apply to names used to specify |
| 3450 | // explicit instantiations. |
| 3451 | // We extend this to also cover explicit specializations. Note that |
| 3452 | // we don't suppress if this turns out to be an elaborated type |
| 3453 | // specifier. |
| 3454 | bool shouldDelayDiagsInTag = |
| 3455 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 3456 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 3457 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3458 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3459 | // Enum definitions should not be parsed in a trailing-return-type. |
| 3460 | bool AllowDeclaration = DSC != DSC_trailing; |
| 3461 | |
| 3462 | bool AllowFixedUnderlyingType = AllowDeclaration && |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3463 | (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt || |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3464 | getLangOpts().ObjC2); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3465 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3466 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3467 | if (getLangOpts().CPlusPlus) { |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3468 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 3469 | // if a fixed underlying type is allowed. |
| 3470 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3471 | |
| 3472 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Richard Smith | 725fe0e | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 3473 | /*EnteringContext=*/true)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3474 | return; |
| 3475 | |
| 3476 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3477 | Diag(Tok, diag::err_expected) << tok::identifier; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3478 | if (Tok.isNot(tok::l_brace)) { |
| 3479 | // Has no name and is not a definition. |
| 3480 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3481 | SkipUntil(tok::comma, StopAtSemi); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3482 | return; |
| 3483 | } |
| 3484 | } |
| 3485 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3486 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3487 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3488 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3489 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3490 | Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3491 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3492 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3493 | SkipUntil(tok::comma, StopAtSemi); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3494 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3495 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3496 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3497 | // If an identifier is present, consume and remember it. |
| 3498 | IdentifierInfo *Name = 0; |
| 3499 | SourceLocation NameLoc; |
| 3500 | if (Tok.is(tok::identifier)) { |
| 3501 | Name = Tok.getIdentifierInfo(); |
| 3502 | NameLoc = ConsumeToken(); |
| 3503 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3504 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3505 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3506 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 3507 | // declaration of a scoped enumeration. |
| 3508 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3509 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3510 | IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3511 | } |
| 3512 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3513 | // Okay, end the suppression area. We'll decide whether to emit the |
| 3514 | // diagnostics in a second. |
| 3515 | if (shouldDelayDiagsInTag) |
| 3516 | diagsFromTag.done(); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3517 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3518 | TypeResult BaseType; |
| 3519 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3520 | // Parse the fixed underlying type. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3521 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3522 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3523 | bool PossibleBitfield = false; |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3524 | if (CanBeBitfield) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3525 | // If we're in class scope, this can either be an enum declaration with |
| 3526 | // an underlying type, or a declaration of a bitfield member. We try to |
| 3527 | // use a simple disambiguation scheme first to catch the common cases |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3528 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 3529 | // anything that's a simple-type-specifier followed by '(' as an |
| 3530 | // expression. This suffices because function types are not valid |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3531 | // underlying types anyway. |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 3532 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 3533 | Sema::ConstantEvaluated); |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3534 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3535 | // If the next token starts an expression, we know we're parsing a |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3536 | // bit-field. This is the common case. |
| 3537 | if (TPR == TPResult::True()) |
| 3538 | PossibleBitfield = true; |
| 3539 | // If the next token starts a type-specifier-seq, it may be either a |
| 3540 | // a fixed underlying type or the start of a function-style cast in C++; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3541 | // lookahead one more token to see if it's obvious that we have a |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3542 | // fixed underlying type. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3543 | else if (TPR == TPResult::False() && |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3544 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 3545 | // Consume the ':'. |
| 3546 | ConsumeToken(); |
| 3547 | } else { |
| 3548 | // We have the start of a type-specifier-seq, so we have to perform |
| 3549 | // tentative parsing to determine whether we have an expression or a |
| 3550 | // type. |
| 3551 | TentativeParsingAction TPA(*this); |
| 3552 | |
| 3553 | // Consume the ':'. |
| 3554 | ConsumeToken(); |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3555 | |
| 3556 | // If we see a type specifier followed by an open-brace, we have an |
| 3557 | // ambiguity between an underlying type and a C++11 braced |
| 3558 | // function-style cast. Resolve this by always treating it as an |
| 3559 | // underlying type. |
| 3560 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 3561 | // this case. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3562 | if ((getLangOpts().CPlusPlus && |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3563 | isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3564 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3565 | // We'll parse this as a bitfield later. |
| 3566 | PossibleBitfield = true; |
| 3567 | TPA.Revert(); |
| 3568 | } else { |
| 3569 | // We have a type-specifier-seq. |
| 3570 | TPA.Commit(); |
| 3571 | } |
| 3572 | } |
| 3573 | } else { |
| 3574 | // Consume the ':'. |
| 3575 | ConsumeToken(); |
| 3576 | } |
| 3577 | |
| 3578 | if (!PossibleBitfield) { |
| 3579 | SourceRange Range; |
| 3580 | BaseType = ParseTypeName(&Range); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3581 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3582 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3583 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Eli Friedman | cef3a7b | 2012-11-02 01:34:28 +0000 | [diff] [blame] | 3584 | } else if (!getLangOpts().ObjC2) { |
| 3585 | if (getLangOpts().CPlusPlus) |
| 3586 | Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range; |
| 3587 | else |
| 3588 | Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range; |
| 3589 | } |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3590 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3591 | } |
| 3592 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3593 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 3594 | // friend declaration, and cannot have an accompanying definition. If we have |
| 3595 | // 'enum foo;', then this is a forward declaration. If we have |
| 3596 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 3597 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3598 | // |
| 3599 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 3600 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 3601 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 3602 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3603 | Sema::TagUseKind TUK; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3604 | if (!AllowDeclaration) { |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3605 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3606 | } else if (Tok.is(tok::l_brace)) { |
| 3607 | if (DS.isFriendSpecified()) { |
| 3608 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
| 3609 | << SourceRange(DS.getFriendSpecLoc()); |
| 3610 | ConsumeBrace(); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3611 | SkipUntil(tok::r_brace, StopAtSemi); |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3612 | TUK = Sema::TUK_Friend; |
| 3613 | } else { |
| 3614 | TUK = Sema::TUK_Definition; |
| 3615 | } |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3616 | } else if (!isTypeSpecifier(DSC) && |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3617 | (Tok.is(tok::semi) || |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3618 | (Tok.isAtStartOfLine() && |
| 3619 | !isValidAfterTypeSpecifier(CanBeBitfield)))) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3620 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
| 3621 | if (Tok.isNot(tok::semi)) { |
| 3622 | // A semicolon was missing after this declaration. Diagnose and recover. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3623 | ExpectAndConsume(tok::semi, diag::err_expected_after, "enum"); |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3624 | PP.EnterToken(Tok); |
| 3625 | Tok.setKind(tok::semi); |
| 3626 | } |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3627 | } else { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3628 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3629 | } |
| 3630 | |
| 3631 | // If this is an elaborated type specifier, and we delayed |
| 3632 | // diagnostics before, just merge them into the current pool. |
| 3633 | if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) { |
| 3634 | diagsFromTag.redelay(); |
| 3635 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3636 | |
| 3637 | MultiTemplateParamsArg TParams; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3638 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3639 | TUK != Sema::TUK_Reference) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3640 | if (!getLangOpts().CPlusPlus11 || !SS.isSet()) { |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3641 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3642 | Diag(Tok, diag::err_enum_template); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3643 | SkipUntil(tok::comma, StopAtSemi); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3644 | return; |
| 3645 | } |
| 3646 | |
| 3647 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 3648 | // Enumerations can't be explicitly instantiated. |
| 3649 | DS.SetTypeSpecError(); |
| 3650 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 3651 | return; |
| 3652 | } |
| 3653 | |
| 3654 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 3655 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 3656 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3657 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3658 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3659 | if (TUK == Sema::TUK_Reference) |
| 3660 | ProhibitAttributes(attrs); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3661 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3662 | if (!Name && TUK != Sema::TUK_Definition) { |
| 3663 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3664 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3665 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3666 | SkipUntil(tok::comma, StopAtSemi); |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3667 | return; |
| 3668 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3669 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3670 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3671 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3672 | const char *PrevSpec = 0; |
| 3673 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3674 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3675 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3676 | AS, DS.getModulePrivateSpecLoc(), TParams, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3677 | Owned, IsDependent, ScopedEnumKWLoc, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3678 | IsScopedUsingClassTag, BaseType, |
| 3679 | DSC == DSC_type_specifier); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3680 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3681 | if (IsDependent) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3682 | // This enum has a dependent nested-name-specifier. Handle it as a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3683 | // dependent tag. |
| 3684 | if (!Name) { |
| 3685 | DS.SetTypeSpecError(); |
| 3686 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 3687 | return; |
| 3688 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3689 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3690 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3691 | TUK, SS, Name, StartLoc, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3692 | NameLoc); |
| 3693 | if (Type.isInvalid()) { |
| 3694 | DS.SetTypeSpecError(); |
| 3695 | return; |
| 3696 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3697 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3698 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 3699 | NameLoc.isValid() ? NameLoc : StartLoc, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3700 | PrevSpec, DiagID, Type.get(), |
| 3701 | Actions.getASTContext().getPrintingPolicy())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3702 | Diag(StartLoc, DiagID) << PrevSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3703 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3704 | return; |
| 3705 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3706 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3707 | if (!TagDecl) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3708 | // The action failed to produce an enumeration tag. If this is a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3709 | // definition, consume the entire definition. |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3710 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3711 | ConsumeBrace(); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3712 | SkipUntil(tok::r_brace, StopAtSemi); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3713 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3714 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3715 | DS.SetTypeSpecError(); |
| 3716 | return; |
| 3717 | } |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3718 | |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3719 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3720 | ParseEnumBody(StartLoc, TagDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3721 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3722 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3723 | NameLoc.isValid() ? NameLoc : StartLoc, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3724 | PrevSpec, DiagID, TagDecl, Owned, |
| 3725 | Actions.getASTContext().getPrintingPolicy())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3726 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3727 | } |
| 3728 | |
| 3729 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3730 | /// enumerator-list: |
| 3731 | /// enumerator |
| 3732 | /// enumerator-list ',' enumerator |
| 3733 | /// enumerator: |
| 3734 | /// enumeration-constant |
| 3735 | /// enumeration-constant '=' constant-expression |
| 3736 | /// enumeration-constant: |
| 3737 | /// identifier |
| 3738 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3739 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3740 | // Enter the scope of the enum body and start the definition. |
| 3741 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3742 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3743 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3744 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3745 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3746 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3747 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3748 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3749 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3750 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3751 | SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3752 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3753 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3754 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3755 | // Parse the enumerator-list. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3756 | while (Tok.isNot(tok::r_brace)) { |
| 3757 | // Parse enumerator. If failed, try skipping till the start of the next |
| 3758 | // enumerator definition. |
| 3759 | if (Tok.isNot(tok::identifier)) { |
| 3760 | Diag(Tok.getLocation(), diag::err_expected) << tok::identifier; |
| 3761 | if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) && |
| 3762 | TryConsumeToken(tok::comma)) |
| 3763 | continue; |
| 3764 | break; |
| 3765 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3766 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3767 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3768 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3769 | // If attributes exist after the enumerator, parse them. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3770 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3771 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3772 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3773 | ProhibitAttributes(attrs); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3774 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3775 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3776 | ExprResult AssignedVal; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 3777 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3778 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3779 | if (TryConsumeToken(tok::equal, EqualLoc)) { |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3780 | AssignedVal = ParseConstantExpression(); |
| 3781 | if (AssignedVal.isInvalid()) |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3782 | SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3783 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3784 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3785 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3786 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3787 | LastEnumConstDecl, |
| 3788 | IdentLoc, Ident, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3789 | attrs.getList(), EqualLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3790 | AssignedVal.release()); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3791 | PD.complete(EnumConstDecl); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3792 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3793 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3794 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3795 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3796 | if (Tok.is(tok::identifier)) { |
| 3797 | // We're missing a comma between enumerators. |
| 3798 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3799 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3800 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3801 | continue; |
| 3802 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3803 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3804 | // Emumerator definition must be finished, only comma or r_brace are |
| 3805 | // allowed here. |
| 3806 | SourceLocation CommaLoc; |
| 3807 | if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) { |
| 3808 | if (EqualLoc.isValid()) |
| 3809 | Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace |
| 3810 | << tok::comma; |
| 3811 | else |
| 3812 | Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator); |
| 3813 | if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) { |
| 3814 | if (TryConsumeToken(tok::comma, CommaLoc)) |
| 3815 | continue; |
| 3816 | } else { |
| 3817 | break; |
| 3818 | } |
| 3819 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3820 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3821 | // If comma is followed by r_brace, emit appropriate warning. |
| 3822 | if (Tok.is(tok::r_brace) && CommaLoc.isValid()) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3823 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11) |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3824 | Diag(CommaLoc, getLangOpts().CPlusPlus ? |
| 3825 | diag::ext_enumerator_list_comma_cxx : |
| 3826 | diag::ext_enumerator_list_comma_c) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3827 | << FixItHint::CreateRemoval(CommaLoc); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3828 | else if (getLangOpts().CPlusPlus11) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3829 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3830 | << FixItHint::CreateRemoval(CommaLoc); |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3831 | break; |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3832 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3833 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3834 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3835 | // Eat the }. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3836 | T.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3837 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3838 | // If attributes exist after the identifier list, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3839 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3840 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3841 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3842 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 3843 | EnumDecl, EnumConstantDecls, |
| 3844 | getCurScope(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3845 | attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3846 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3847 | EnumScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3848 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3849 | T.getCloseLocation()); |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3850 | |
| 3851 | // The next token must be valid after an enum definition. If not, a ';' |
| 3852 | // was probably forgotten. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3853 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
| 3854 | if (!isValidAfterTypeSpecifier(CanBeBitfield)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3855 | ExpectAndConsume(tok::semi, diag::err_expected_after, "enum"); |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3856 | // Push this token back into the preprocessor and change our current token |
| 3857 | // to ';' so that the rest of the code recovers as though there were an |
| 3858 | // ';' after the definition. |
| 3859 | PP.EnterToken(Tok); |
| 3860 | Tok.setKind(tok::semi); |
| 3861 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3862 | } |
| 3863 | |
| 3864 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3865 | /// start of a type-qualifier-list. |
| 3866 | bool Parser::isTypeQualifier() const { |
| 3867 | switch (Tok.getKind()) { |
| 3868 | default: return false; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 3869 | // type-qualifier |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3870 | case tok::kw_const: |
| 3871 | case tok::kw_volatile: |
| 3872 | case tok::kw_restrict: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3873 | case tok::kw___private: |
| 3874 | case tok::kw___local: |
| 3875 | case tok::kw___global: |
| 3876 | case tok::kw___constant: |
| 3877 | case tok::kw___read_only: |
| 3878 | case tok::kw___read_write: |
| 3879 | case tok::kw___write_only: |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3880 | return true; |
| 3881 | } |
| 3882 | } |
| 3883 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3884 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3885 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3886 | /// specifier or if we're not sure. |
| 3887 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3888 | switch (Tok.getKind()) { |
| 3889 | default: return false; |
| 3890 | // type-specifiers |
| 3891 | case tok::kw_short: |
| 3892 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3893 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3894 | case tok::kw___int128: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3895 | case tok::kw_signed: |
| 3896 | case tok::kw_unsigned: |
| 3897 | case tok::kw__Complex: |
| 3898 | case tok::kw__Imaginary: |
| 3899 | case tok::kw_void: |
| 3900 | case tok::kw_char: |
| 3901 | case tok::kw_wchar_t: |
| 3902 | case tok::kw_char16_t: |
| 3903 | case tok::kw_char32_t: |
| 3904 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3905 | case tok::kw_half: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3906 | case tok::kw_float: |
| 3907 | case tok::kw_double: |
| 3908 | case tok::kw_bool: |
| 3909 | case tok::kw__Bool: |
| 3910 | case tok::kw__Decimal32: |
| 3911 | case tok::kw__Decimal64: |
| 3912 | case tok::kw__Decimal128: |
| 3913 | case tok::kw___vector: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3914 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3915 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3916 | case tok::kw_class: |
| 3917 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3918 | case tok::kw___interface: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3919 | case tok::kw_union: |
| 3920 | // enum-specifier |
| 3921 | case tok::kw_enum: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3922 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3923 | // typedef-name |
| 3924 | case tok::annot_typename: |
| 3925 | return true; |
| 3926 | } |
| 3927 | } |
| 3928 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3929 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3930 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3931 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3932 | switch (Tok.getKind()) { |
| 3933 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3934 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3935 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3936 | if (TryAltiVecVectorToken()) |
| 3937 | return true; |
| 3938 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3939 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3940 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3941 | // recurse to handle whatever we get. |
| 3942 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3943 | return true; |
| 3944 | if (Tok.is(tok::identifier)) |
| 3945 | return false; |
| 3946 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3947 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3948 | case tok::coloncolon: // ::foo::bar |
| 3949 | if (NextToken().is(tok::kw_new) || // ::new |
| 3950 | NextToken().is(tok::kw_delete)) // ::delete |
| 3951 | return false; |
| 3952 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3953 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3954 | return true; |
| 3955 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3956 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3957 | // GNU attributes support. |
| 3958 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3959 | // GNU typeof support. |
| 3960 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3961 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3962 | // type-specifiers |
| 3963 | case tok::kw_short: |
| 3964 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3965 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3966 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3967 | case tok::kw_signed: |
| 3968 | case tok::kw_unsigned: |
| 3969 | case tok::kw__Complex: |
| 3970 | case tok::kw__Imaginary: |
| 3971 | case tok::kw_void: |
| 3972 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3973 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3974 | case tok::kw_char16_t: |
| 3975 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3976 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3977 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3978 | case tok::kw_float: |
| 3979 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3980 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3981 | case tok::kw__Bool: |
| 3982 | case tok::kw__Decimal32: |
| 3983 | case tok::kw__Decimal64: |
| 3984 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3985 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3986 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3987 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3988 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3989 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3990 | case tok::kw___interface: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3991 | case tok::kw_union: |
| 3992 | // enum-specifier |
| 3993 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3994 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3995 | // type-qualifier |
| 3996 | case tok::kw_const: |
| 3997 | case tok::kw_volatile: |
| 3998 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3999 | |
John McCall | b8a8de3 | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4000 | // Debugger support. |
| 4001 | case tok::kw___unknown_anytype: |
| 4002 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4003 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 4004 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4005 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4006 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 4007 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4008 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4009 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4010 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4011 | case tok::kw___cdecl: |
| 4012 | case tok::kw___stdcall: |
| 4013 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4014 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4015 | case tok::kw___w64: |
| 4016 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4017 | case tok::kw___ptr32: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4018 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4019 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4020 | |
| 4021 | case tok::kw___private: |
| 4022 | case tok::kw___local: |
| 4023 | case tok::kw___global: |
| 4024 | case tok::kw___constant: |
| 4025 | case tok::kw___read_only: |
| 4026 | case tok::kw___read_write: |
| 4027 | case tok::kw___write_only: |
| 4028 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4029 | return true; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4030 | |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4031 | // C11 _Atomic |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4032 | case tok::kw__Atomic: |
| 4033 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4034 | } |
| 4035 | } |
| 4036 | |
| 4037 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 4038 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4039 | /// |
| 4040 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 4041 | /// this check is to disambiguate between an expression and a declaration. |
| 4042 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4043 | switch (Tok.getKind()) { |
| 4044 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4045 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4046 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4047 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4048 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4049 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4050 | if (TryAltiVecVectorToken()) |
| 4051 | return true; |
| 4052 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4053 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4054 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4055 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4056 | // recurse to handle whatever we get. |
| 4057 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4058 | return true; |
| 4059 | if (Tok.is(tok::identifier)) |
| 4060 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4061 | |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4062 | // If we're in Objective-C and we have an Objective-C class type followed |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4063 | // by an identifier and then either ':' or ']', in a place where an |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4064 | // expression is permitted, then this is probably a class message send |
| 4065 | // missing the initial '['. In this case, we won't consider this to be |
| 4066 | // the start of a declaration. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4067 | if (DisambiguatingWithExpression && |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4068 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 4069 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4070 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4071 | return isDeclarationSpecifier(); |
| 4072 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4073 | case tok::coloncolon: // ::foo::bar |
| 4074 | if (NextToken().is(tok::kw_new) || // ::new |
| 4075 | NextToken().is(tok::kw_delete)) // ::delete |
| 4076 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4077 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4078 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4079 | // recurse to handle whatever we get. |
| 4080 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4081 | return true; |
| 4082 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4083 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4084 | // storage-class-specifier |
| 4085 | case tok::kw_typedef: |
| 4086 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 4087 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4088 | case tok::kw_static: |
| 4089 | case tok::kw_auto: |
| 4090 | case tok::kw_register: |
| 4091 | case tok::kw___thread: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4092 | case tok::kw_thread_local: |
| 4093 | case tok::kw__Thread_local: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4094 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 4095 | // Modules |
| 4096 | case tok::kw___module_private__: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4097 | |
John McCall | b8a8de3 | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4098 | // Debugger support |
| 4099 | case tok::kw___unknown_anytype: |
| 4100 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4101 | // type-specifiers |
| 4102 | case tok::kw_short: |
| 4103 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4104 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4105 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4106 | case tok::kw_signed: |
| 4107 | case tok::kw_unsigned: |
| 4108 | case tok::kw__Complex: |
| 4109 | case tok::kw__Imaginary: |
| 4110 | case tok::kw_void: |
| 4111 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4112 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4113 | case tok::kw_char16_t: |
| 4114 | case tok::kw_char32_t: |
| 4115 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4116 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4117 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4118 | case tok::kw_float: |
| 4119 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4120 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4121 | case tok::kw__Bool: |
| 4122 | case tok::kw__Decimal32: |
| 4123 | case tok::kw__Decimal64: |
| 4124 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4125 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4126 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4127 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4128 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4129 | case tok::kw_struct: |
| 4130 | case tok::kw_union: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4131 | case tok::kw___interface: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4132 | // enum-specifier |
| 4133 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4134 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4135 | // type-qualifier |
| 4136 | case tok::kw_const: |
| 4137 | case tok::kw_volatile: |
| 4138 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4139 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4140 | // function-specifier |
| 4141 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4142 | case tok::kw_virtual: |
| 4143 | case tok::kw_explicit: |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4144 | case tok::kw__Noreturn: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4145 | |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 4146 | // alignment-specifier |
| 4147 | case tok::kw__Alignas: |
| 4148 | |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4149 | // friend keyword. |
| 4150 | case tok::kw_friend: |
| 4151 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 4152 | // static_assert-declaration |
| 4153 | case tok::kw__Static_assert: |
| 4154 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4155 | // GNU typeof support. |
| 4156 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4157 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4158 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4159 | case tok::kw___attribute: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4160 | |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4161 | // C++11 decltype and constexpr. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4162 | case tok::annot_decltype: |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4163 | case tok::kw_constexpr: |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 4164 | |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4165 | // C11 _Atomic |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4166 | case tok::kw__Atomic: |
| 4167 | return true; |
| 4168 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 4169 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4170 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4171 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4172 | |
Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 4173 | // typedef-name |
| 4174 | case tok::annot_typename: |
| 4175 | return !DisambiguatingWithExpression || |
| 4176 | !isStartOfObjCClassMessageMissingOpenBracket(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4177 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 4178 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4179 | case tok::kw___cdecl: |
| 4180 | case tok::kw___stdcall: |
| 4181 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4182 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4183 | case tok::kw___w64: |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4184 | case tok::kw___sptr: |
| 4185 | case tok::kw___uptr: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4186 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4187 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4188 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4189 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4190 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4191 | |
| 4192 | case tok::kw___private: |
| 4193 | case tok::kw___local: |
| 4194 | case tok::kw___global: |
| 4195 | case tok::kw___constant: |
| 4196 | case tok::kw___read_only: |
| 4197 | case tok::kw___read_write: |
| 4198 | case tok::kw___write_only: |
| 4199 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4200 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4201 | } |
| 4202 | } |
| 4203 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 4204 | bool Parser::isConstructorDeclarator(bool IsUnqualified) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4205 | TentativeParsingAction TPA(*this); |
| 4206 | |
| 4207 | // Parse the C++ scope specifier. |
| 4208 | CXXScopeSpec SS; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4209 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4210 | /*EnteringContext=*/true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4211 | TPA.Revert(); |
| 4212 | return false; |
| 4213 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4214 | |
| 4215 | // Parse the constructor name. |
| 4216 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 4217 | // We already know that we have a constructor name; just consume |
| 4218 | // the token. |
| 4219 | ConsumeToken(); |
| 4220 | } else { |
| 4221 | TPA.Revert(); |
| 4222 | return false; |
| 4223 | } |
| 4224 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4225 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4226 | if (Tok.isNot(tok::l_paren)) { |
| 4227 | TPA.Revert(); |
| 4228 | return false; |
| 4229 | } |
| 4230 | ConsumeParen(); |
| 4231 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4232 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 4233 | // that we have a constructor. |
| 4234 | if (Tok.is(tok::r_paren) || |
| 4235 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4236 | TPA.Revert(); |
| 4237 | return true; |
| 4238 | } |
| 4239 | |
Richard Smith | 9ec2891 | 2013-09-06 00:12:20 +0000 | [diff] [blame] | 4240 | // A C++11 attribute here signals that we have a constructor, and is an |
| 4241 | // attribute on the first constructor parameter. |
| 4242 | if (getLangOpts().CPlusPlus11 && |
| 4243 | isCXX11AttributeSpecifier(/*Disambiguate*/ false, |
| 4244 | /*OuterMightBeMessageSend*/ true)) { |
| 4245 | TPA.Revert(); |
| 4246 | return true; |
| 4247 | } |
| 4248 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4249 | // If we need to, enter the specified scope. |
| 4250 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4251 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4252 | DeclScopeObj.EnterDeclaratorScope(); |
| 4253 | |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4254 | // Optionally skip Microsoft attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4255 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4256 | MaybeParseMicrosoftAttributes(Attrs); |
| 4257 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4258 | // Check whether the next token(s) are part of a declaration |
| 4259 | // specifier, in which case we have the start of a parameter and, |
| 4260 | // therefore, we know that this is a constructor. |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4261 | bool IsConstructor = false; |
| 4262 | if (isDeclarationSpecifier()) |
| 4263 | IsConstructor = true; |
| 4264 | else if (Tok.is(tok::identifier) || |
| 4265 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 4266 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 4267 | // This might be a parenthesized member name, but is more likely to |
| 4268 | // be a constructor declaration with an invalid argument type. Keep |
| 4269 | // looking. |
| 4270 | if (Tok.is(tok::annot_cxxscope)) |
| 4271 | ConsumeToken(); |
| 4272 | ConsumeToken(); |
| 4273 | |
| 4274 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4275 | // which must have one of the following syntactic forms (see the |
| 4276 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4277 | switch (Tok.getKind()) { |
| 4278 | case tok::l_paren: |
| 4279 | // C(X ( int)); |
| 4280 | case tok::l_square: |
| 4281 | // C(X [ 5]); |
| 4282 | // C(X [ [attribute]]); |
| 4283 | case tok::coloncolon: |
| 4284 | // C(X :: Y); |
| 4285 | // C(X :: *p); |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4286 | // Assume this isn't a constructor, rather than assuming it's a |
| 4287 | // constructor with an unnamed parameter of an ill-formed type. |
| 4288 | break; |
| 4289 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 4290 | case tok::r_paren: |
| 4291 | // C(X ) |
| 4292 | if (NextToken().is(tok::colon) || NextToken().is(tok::kw_try)) { |
| 4293 | // Assume these were meant to be constructors: |
| 4294 | // C(X) : (the name of a bit-field cannot be parenthesized). |
| 4295 | // C(X) try (this is otherwise ill-formed). |
| 4296 | IsConstructor = true; |
| 4297 | } |
| 4298 | if (NextToken().is(tok::semi) || NextToken().is(tok::l_brace)) { |
| 4299 | // If we have a constructor name within the class definition, |
| 4300 | // assume these were meant to be constructors: |
| 4301 | // C(X) { |
| 4302 | // C(X) ; |
| 4303 | // ... because otherwise we would be declaring a non-static data |
| 4304 | // member that is ill-formed because it's of the same type as its |
| 4305 | // surrounding class. |
| 4306 | // |
| 4307 | // FIXME: We can actually do this whether or not the name is qualified, |
| 4308 | // because if it is qualified in this context it must be being used as |
| 4309 | // a constructor name. However, we do not implement that rule correctly |
| 4310 | // currently, so we're somewhat conservative here. |
| 4311 | IsConstructor = IsUnqualified; |
| 4312 | } |
| 4313 | break; |
| 4314 | |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4315 | default: |
| 4316 | IsConstructor = true; |
| 4317 | break; |
| 4318 | } |
| 4319 | } |
| 4320 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4321 | TPA.Revert(); |
| 4322 | return IsConstructor; |
| 4323 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4324 | |
| 4325 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4326 | /// type-qualifier-list: [C99 6.7.5] |
| 4327 | /// type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4328 | /// [vendor] attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4329 | /// [ only if VendorAttributesAllowed=true ] |
| 4330 | /// type-qualifier-list type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4331 | /// [vendor] type-qualifier-list attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4332 | /// [ only if VendorAttributesAllowed=true ] |
| 4333 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4334 | /// [ only if CXX11AttributesAllowed=true ] |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4335 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4336 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4337 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 4338 | bool VendorAttributesAllowed, |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4339 | bool CXX11AttributesAllowed, |
Bill Wendling | 7f3ec66 | 2013-11-26 04:10:07 +0000 | [diff] [blame] | 4340 | bool AtomicAllowed, |
| 4341 | bool IdentifierRequired) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4342 | if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed && |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4343 | isCXX11AttributeSpecifier()) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4344 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4345 | ParseCXX11Attributes(attrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4346 | DS.takeAttributesFrom(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4347 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4348 | |
| 4349 | SourceLocation EndLoc; |
| 4350 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4351 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4352 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4353 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4354 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4355 | SourceLocation Loc = Tok.getLocation(); |
| 4356 | |
| 4357 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4358 | case tok::code_completion: |
| 4359 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 4360 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4361 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4362 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4363 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4364 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4365 | break; |
| 4366 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4367 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4368 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4369 | break; |
| 4370 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4371 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4372 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4373 | break; |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4374 | case tok::kw__Atomic: |
| 4375 | if (!AtomicAllowed) |
| 4376 | goto DoneWithTypeQuals; |
| 4377 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 4378 | getLangOpts()); |
| 4379 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4380 | |
| 4381 | // OpenCL qualifiers: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4382 | case tok::kw___private: |
| 4383 | case tok::kw___global: |
| 4384 | case tok::kw___local: |
| 4385 | case tok::kw___constant: |
| 4386 | case tok::kw___read_only: |
| 4387 | case tok::kw___write_only: |
| 4388 | case tok::kw___read_write: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 4389 | ParseOpenCLQualifiers(DS.getAttributes()); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4390 | break; |
| 4391 | |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4392 | case tok::kw___uptr: |
Bill Wendling | 7f3ec66 | 2013-11-26 04:10:07 +0000 | [diff] [blame] | 4393 | // GNU libc headers in C mode use '__uptr' as an identifer which conflicts |
| 4394 | // with the MS modifier keyword. |
| 4395 | if (VendorAttributesAllowed && !getLangOpts().CPlusPlus && |
Bill Wendling | 03e463e | 2013-12-16 02:32:55 +0000 | [diff] [blame] | 4396 | IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) { |
| 4397 | if (TryKeywordIdentFallback(false)) |
| 4398 | continue; |
Bill Wendling | 7f3ec66 | 2013-11-26 04:10:07 +0000 | [diff] [blame] | 4399 | } |
| 4400 | case tok::kw___sptr: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4401 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 4402 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4403 | case tok::kw___ptr32: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4404 | case tok::kw___cdecl: |
| 4405 | case tok::kw___stdcall: |
| 4406 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4407 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4408 | case tok::kw___unaligned: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4409 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4410 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4411 | continue; |
| 4412 | } |
| 4413 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4414 | case tok::kw___pascal: |
| 4415 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4416 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4417 | continue; |
| 4418 | } |
| 4419 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4420 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4421 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4422 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4423 | continue; // do *not* consume the next token! |
| 4424 | } |
| 4425 | // otherwise, FALL THROUGH! |
| 4426 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4427 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4428 | // If this is not a type-qualifier token, we're done reading type |
| 4429 | // qualifiers. First verify that DeclSpec's are consistent. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 4430 | DS.Finish(Diags, PP, Actions.getASTContext().getPrintingPolicy()); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4431 | if (EndLoc.isValid()) |
| 4432 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4433 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4434 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4435 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4436 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 4437 | if (isInvalid) { |
| 4438 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4439 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4440 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4441 | EndLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4442 | } |
| 4443 | } |
| 4444 | |
| 4445 | |
| 4446 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 4447 | /// |
| 4448 | void Parser::ParseDeclarator(Declarator &D) { |
| 4449 | /// This implements the 'declarator' production in the C grammar, then checks |
| 4450 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4451 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4452 | } |
| 4453 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4454 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) { |
| 4455 | if (Kind == tok::star || Kind == tok::caret) |
| 4456 | return true; |
| 4457 | |
| 4458 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 4459 | if (!Lang.CPlusPlus) |
| 4460 | return false; |
| 4461 | |
| 4462 | return Kind == tok::amp || Kind == tok::ampamp; |
| 4463 | } |
| 4464 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4465 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 4466 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 4467 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4468 | /// ptr-operator production. |
| 4469 | /// |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4470 | /// If the grammar of this construct is extended, matching changes must also be |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4471 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 4472 | /// isConstructorDeclarator. |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4473 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4474 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 4475 | /// [C] pointer[opt] direct-declarator |
| 4476 | /// [C++] direct-declarator |
| 4477 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4478 | /// |
| 4479 | /// pointer: [C99 6.7.5] |
| 4480 | /// '*' type-qualifier-list[opt] |
| 4481 | /// '*' type-qualifier-list[opt] pointer |
| 4482 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4483 | /// ptr-operator: |
| 4484 | /// '*' cv-qualifier-seq[opt] |
| 4485 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4486 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4487 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4488 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4489 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4490 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 4491 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 4492 | if (Diags.hasAllExtensionsSilenced()) |
| 4493 | D.setExtension(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4494 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4495 | // C++ member pointers start with a '::' or a nested-name. |
| 4496 | // Member pointers get special handling, since there's no place for the |
| 4497 | // scope spec in the generic path below. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4498 | if (getLangOpts().CPlusPlus && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 4499 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 4500 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4501 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4502 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4503 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4504 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4505 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 4506 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4507 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4508 | // The scope spec really belongs to the direct-declarator. |
Richard Smith | 6a502c4 | 2013-01-08 22:43:49 +0000 | [diff] [blame] | 4509 | if (D.mayHaveIdentifier()) |
| 4510 | D.getCXXScopeSpec() = SS; |
| 4511 | else |
| 4512 | AnnotateScopeToken(SS, true); |
| 4513 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4514 | if (DirectDeclParser) |
| 4515 | (this->*DirectDeclParser)(D); |
| 4516 | return; |
| 4517 | } |
| 4518 | |
| 4519 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4520 | D.SetRangeEnd(Loc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4521 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4522 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4523 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4524 | |
| 4525 | // Recurse to parse whatever is left. |
| 4526 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 4527 | |
| 4528 | // Sema will have to catch (syntactically invalid) pointers into global |
| 4529 | // scope. It has to catch pointers into namespace scope anyway. |
| 4530 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4531 | Loc), |
| 4532 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4533 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4534 | return; |
| 4535 | } |
| 4536 | } |
| 4537 | |
| 4538 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4539 | // Not a pointer, C++ reference, or block. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4540 | if (!isPtrOperatorToken(Kind, getLangOpts())) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4541 | if (DirectDeclParser) |
| 4542 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4543 | return; |
| 4544 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4545 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4546 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 4547 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4548 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4549 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4550 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 4551 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4552 | // Is a pointer. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4553 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4554 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4555 | // FIXME: GNU attributes are not allowed here in a new-type-id. |
Bill Wendling | 7f3ec66 | 2013-11-26 04:10:07 +0000 | [diff] [blame] | 4556 | ParseTypeQualifierListOpt(DS, true, true, true, !D.mayOmitIdentifier()); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4557 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4558 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4559 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4560 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4561 | if (Kind == tok::star) |
| 4562 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 4563 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 4564 | DS.getConstSpecLoc(), |
| 4565 | DS.getVolatileSpecLoc(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4566 | DS.getRestrictSpecLoc()), |
| 4567 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4568 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4569 | else |
| 4570 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4571 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4572 | Loc), |
| 4573 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4574 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4575 | } else { |
| 4576 | // Is a reference |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4577 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4578 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4579 | // Complain about rvalue references in C++03, but then go on and build |
| 4580 | // the declarator. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4581 | if (Kind == tok::ampamp) |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4582 | Diag(Loc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4583 | diag::warn_cxx98_compat_rvalue_reference : |
| 4584 | diag::ext_rvalue_reference); |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4585 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4586 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 4587 | ParseTypeQualifierListOpt(DS); |
| 4588 | D.ExtendWithDeclSpec(DS); |
| 4589 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4590 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 4591 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 4592 | // template type argument, in which case the cv-qualifiers are ignored. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4593 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 4594 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 4595 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4596 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4597 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 4598 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4599 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4600 | // 'restrict' is permitted as an extension. |
| 4601 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 4602 | Diag(DS.getAtomicSpecLoc(), |
| 4603 | diag::err_invalid_reference_qualifier_application) << "_Atomic"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4604 | } |
| 4605 | |
| 4606 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4607 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4608 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4609 | if (D.getNumTypeObjects() > 0) { |
| 4610 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 4611 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 4612 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 4613 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 4614 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4615 | << II; |
| 4616 | else |
| 4617 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4618 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4619 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4620 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4621 | // can go ahead and build the (technically ill-formed) |
| 4622 | // declarator: reference collapsing will take care of it. |
| 4623 | } |
| 4624 | } |
| 4625 | |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4626 | // Remember that we parsed a reference type. |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4627 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4628 | Kind == tok::amp), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4629 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4630 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4631 | } |
| 4632 | } |
| 4633 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4634 | static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D, |
| 4635 | SourceLocation EllipsisLoc) { |
| 4636 | if (EllipsisLoc.isValid()) { |
| 4637 | FixItHint Insertion; |
| 4638 | if (!D.getEllipsisLoc().isValid()) { |
| 4639 | Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "..."); |
| 4640 | D.setEllipsisLoc(EllipsisLoc); |
| 4641 | } |
| 4642 | P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) |
| 4643 | << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName(); |
| 4644 | } |
| 4645 | } |
| 4646 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4647 | /// ParseDirectDeclarator |
| 4648 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4649 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4650 | /// '(' declarator ')' |
| 4651 | /// [GNU] '(' attributes declarator ')' |
| 4652 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4653 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4654 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4655 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4656 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4657 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 4658 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4659 | /// direct-declarator '(' parameter-type-list ')' |
| 4660 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4661 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4662 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 4663 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 4664 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4665 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 4666 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 4667 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4668 | /// [C++] declarator-id |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4669 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4670 | /// |
| 4671 | /// declarator-id: [C++ 8] |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4672 | /// '...'[opt] id-expression |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4673 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 4674 | /// |
| 4675 | /// id-expression: [C++ 5.1] |
| 4676 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4677 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4678 | /// |
| 4679 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4680 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4681 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4682 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4683 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 4684 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 4685 | /// |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4686 | /// Note, any additional constructs added here may need corresponding changes |
| 4687 | /// in isConstructorDeclarator. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4688 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4689 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4690 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4691 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4692 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4693 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4694 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4695 | D.getContext() == Declarator::MemberContext; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4696 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4697 | EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4698 | } |
| 4699 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4700 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4701 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 4702 | // Change the declaration context for name lookup, until this function |
| 4703 | // is exited (and the declarator has been parsed). |
| 4704 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4705 | } |
| 4706 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4707 | // C++0x [dcl.fct]p14: |
| 4708 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4709 | // of a parameter-declaration-clause without a preceding comma. In |
| 4710 | // this case, the ellipsis is parsed as part of the |
| 4711 | // abstract-declarator if the type of the parameter names a template |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4712 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 4713 | // as part of the parameter-declaration-clause. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4714 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4715 | !((D.getContext() == Declarator::PrototypeContext || |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4716 | D.getContext() == Declarator::LambdaExprParameterContext || |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4717 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4718 | NextToken().is(tok::r_paren) && |
Richard Smith | 30f2a74 | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4719 | !D.hasGroupingParens() && |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4720 | !Actions.containsUnexpandedParameterPacks(D))) { |
| 4721 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 4722 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) { |
| 4723 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 4724 | // the user what they should have done. |
| 4725 | ParseDeclarator(D); |
| 4726 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4727 | return; |
| 4728 | } else |
| 4729 | D.setEllipsisLoc(EllipsisLoc); |
| 4730 | |
| 4731 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 4732 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 4733 | // the l_paren token. |
| 4734 | } |
| 4735 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4736 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 4737 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 4738 | // We found something that indicates the start of an unqualified-id. |
| 4739 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4740 | bool AllowConstructorName; |
| 4741 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 4742 | AllowConstructorName = false; |
| 4743 | else if (D.getCXXScopeSpec().isSet()) |
| 4744 | AllowConstructorName = |
| 4745 | (D.getContext() == Declarator::FileContext || |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 4746 | D.getContext() == Declarator::MemberContext); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4747 | else |
| 4748 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 4749 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4750 | SourceLocation TemplateKWLoc; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4751 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 4752 | /*EnteringContext=*/true, |
| 4753 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4754 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4755 | ParsedType(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4756 | TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4757 | D.getName()) || |
| 4758 | // Once we're past the identifier, if the scope was bad, mark the |
| 4759 | // whole declarator bad. |
| 4760 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4761 | D.SetIdentifier(0, Tok.getLocation()); |
| 4762 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4763 | } else { |
| 4764 | // Parsed the unqualified-id; update range information and move along. |
| 4765 | if (D.getSourceRange().getBegin().isInvalid()) |
| 4766 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 4767 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4768 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4769 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4770 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4771 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4772 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4773 | "There's a C++-specific check for tok::identifier above"); |
| 4774 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 4775 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 4776 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4777 | goto PastIdentifier; |
Richard Smith | a38253c | 2013-07-11 05:10:21 +0000 | [diff] [blame] | 4778 | } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) { |
Richard Smith | 8d1ab8a | 2013-10-13 22:12:28 +0000 | [diff] [blame] | 4779 | // A virt-specifier isn't treated as an identifier if it appears after a |
| 4780 | // trailing-return-type. |
| 4781 | if (D.getContext() != Declarator::TrailingReturnContext || |
| 4782 | !isCXX11VirtSpecifier(Tok)) { |
| 4783 | Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id) |
| 4784 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 4785 | D.SetIdentifier(0, Tok.getLocation()); |
| 4786 | ConsumeToken(); |
| 4787 | goto PastIdentifier; |
| 4788 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4789 | } |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4790 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4791 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4792 | // direct-declarator: '(' declarator ')' |
| 4793 | // direct-declarator: '(' attributes declarator ')' |
| 4794 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 4795 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4796 | |
| 4797 | // If the declarator was parenthesized, we entered the declarator |
| 4798 | // scope when parsing the parenthesized declarator, then exited |
| 4799 | // the scope already. Re-enter the scope, if we need to. |
| 4800 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4801 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4802 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4803 | if (!D.isInvalidType() && |
| 4804 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4805 | // Change the declaration context for name lookup, until this function |
| 4806 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4807 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4808 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4809 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4810 | // This could be something simple like "int" (in which case the declarator |
| 4811 | // portion is empty), if an abstract-declarator is allowed. |
| 4812 | D.SetIdentifier(0, Tok.getLocation()); |
Richard Smith | 30f2a74 | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4813 | |
| 4814 | // The grammar for abstract-pack-declarator does not allow grouping parens. |
| 4815 | // FIXME: Revisit this once core issue 1488 is resolved. |
| 4816 | if (D.hasEllipsis() && D.hasGroupingParens()) |
| 4817 | Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()), |
| 4818 | diag::ext_abstract_pack_declarator_parens); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4819 | } else { |
David Blaikie | e75d9cf | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 4820 | if (Tok.getKind() == tok::annot_pragma_parser_crash) |
David Blaikie | 377da4c | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 4821 | LLVM_BUILTIN_TRAP; |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 4822 | if (D.getContext() == Declarator::MemberContext) |
| 4823 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 4824 | << D.getDeclSpec().getSourceRange(); |
Richard Trieu | db55c04c | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 4825 | else if (getLangOpts().CPlusPlus) { |
| 4826 | if (Tok.is(tok::period) || Tok.is(tok::arrow)) |
| 4827 | Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow); |
Richard Trieu | efb288c | 2013-09-05 02:31:33 +0000 | [diff] [blame] | 4828 | else { |
| 4829 | SourceLocation Loc = D.getCXXScopeSpec().getEndLoc(); |
| 4830 | if (Tok.isAtStartOfLine() && Loc.isValid()) |
| 4831 | Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id) |
| 4832 | << getLangOpts().CPlusPlus; |
| 4833 | else |
| 4834 | Diag(Tok, diag::err_expected_unqualified_id) |
| 4835 | << getLangOpts().CPlusPlus; |
| 4836 | } |
Richard Trieu | db55c04c | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 4837 | } else |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 4838 | Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_paren; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4839 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 4840 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4841 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4842 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4843 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4844 | assert(D.isPastIdentifier() && |
| 4845 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4846 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4847 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 4848 | if (D.hasName() && !D.getNumTypeObjects()) |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4849 | MaybeParseCXX11Attributes(D); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4850 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4851 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4852 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4853 | // Enter function-declaration scope, limiting any declarators to the |
| 4854 | // function prototype scope, including parameter declarators. |
| 4855 | ParseScope PrototypeScope(this, |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4856 | Scope::FunctionPrototypeScope|Scope::DeclScope| |
| 4857 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 4858 | ? Scope::FunctionDeclarationScope : 0)); |
| 4859 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4860 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 4861 | // In such a case, check if we actually have a function declarator; if it |
| 4862 | // is not, the declarator has been fully parsed. |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4863 | bool IsAmbiguous = false; |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 4864 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 4865 | // The name of the declarator, if any, is tentatively declared within |
| 4866 | // a possible direct initializer. |
| 4867 | TentativelyDeclaredIdentifiers.push_back(D.getIdentifier()); |
| 4868 | bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous); |
| 4869 | TentativelyDeclaredIdentifiers.pop_back(); |
| 4870 | if (!IsFunctionDecl) |
| 4871 | break; |
| 4872 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4873 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4874 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4875 | T.consumeOpen(); |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4876 | ParseFunctionDeclarator(D, attrs, T, IsAmbiguous); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4877 | PrototypeScope.Exit(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4878 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4879 | ParseBracketDeclarator(D); |
| 4880 | } else { |
| 4881 | break; |
| 4882 | } |
| 4883 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4884 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4885 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4886 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 4887 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4888 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4889 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 4890 | /// |
| 4891 | /// direct-declarator: |
| 4892 | /// '(' declarator ')' |
| 4893 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4894 | /// direct-declarator '(' parameter-type-list ')' |
| 4895 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4896 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4897 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4898 | /// |
| 4899 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4900 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4901 | T.consumeOpen(); |
| 4902 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4903 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4904 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4905 | // Eat any attributes before we look at whether this is a grouping or function |
| 4906 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 4907 | // the type being built up, for example: |
| 4908 | // int (__attribute__(()) *x)(long y) |
| 4909 | // If this ends up not being a grouping paren, the attribute applies to the |
| 4910 | // first argument, for example: |
| 4911 | // int (__attribute__(()) int x) |
| 4912 | // In either case, we need to eat any attributes to be able to determine what |
| 4913 | // sort of paren this is. |
| 4914 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4915 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4916 | bool RequiresArg = false; |
| 4917 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4918 | ParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4919 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4920 | // We require that the argument list (if this is a non-grouping paren) be |
| 4921 | // present even if the attribute list was empty. |
| 4922 | RequiresArg = true; |
| 4923 | } |
Chad Rosier | 9cab1c9 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 4924 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4925 | // Eat any Microsoft extensions. |
Chad Rosier | 9cab1c9 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 4926 | ParseMicrosoftTypeAttributes(attrs); |
| 4927 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4928 | // Eat any Borland extensions. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 4929 | if (Tok.is(tok::kw___pascal)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4930 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4931 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4932 | // If we haven't past the identifier yet (or where the identifier would be |
| 4933 | // stored, if this is an abstract declarator), then this is probably just |
| 4934 | // grouping parens. However, if this could be an abstract-declarator, then |
| 4935 | // this could also be the start of function arguments (consider 'void()'). |
| 4936 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4937 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4938 | if (!D.mayOmitIdentifier()) { |
| 4939 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 4940 | // paren, because we haven't seen the identifier yet. |
| 4941 | isGrouping = true; |
| 4942 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4943 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 4944 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4945 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 4946 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4947 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 4948 | // considered to be a type, not a K&R identifier-list. |
| 4949 | isGrouping = false; |
| 4950 | } else { |
| 4951 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 4952 | isGrouping = true; |
| 4953 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4954 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4955 | // If this is a grouping paren, handle: |
| 4956 | // direct-declarator: '(' declarator ')' |
| 4957 | // direct-declarator: '(' attributes declarator ')' |
| 4958 | if (isGrouping) { |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4959 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 4960 | D.setEllipsisLoc(SourceLocation()); |
| 4961 | |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4962 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4963 | D.setGroupingParens(true); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4964 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4965 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4966 | T.consumeClose(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4967 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4968 | T.getCloseLocation()), |
| 4969 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4970 | |
| 4971 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4972 | |
| 4973 | // An ellipsis cannot be placed outside parentheses. |
| 4974 | if (EllipsisLoc.isValid()) |
| 4975 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4976 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4977 | return; |
| 4978 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4979 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4980 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 4981 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4982 | // identifier (and remember where it would have been), then call into |
| 4983 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4984 | D.SetIdentifier(0, Tok.getLocation()); |
| 4985 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4986 | // Enter function-declaration scope, limiting any declarators to the |
| 4987 | // function prototype scope, including parameter declarators. |
| 4988 | ParseScope PrototypeScope(this, |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4989 | Scope::FunctionPrototypeScope | Scope::DeclScope | |
| 4990 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 4991 | ? Scope::FunctionDeclarationScope : 0)); |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4992 | ParseFunctionDeclarator(D, attrs, T, false, RequiresArg); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4993 | PrototypeScope.Exit(); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4994 | } |
| 4995 | |
| 4996 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 4997 | /// declarator D up to a paren, which indicates that we are parsing function |
| 4998 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4999 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5000 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 5001 | /// immediately after the open paren - they should be considered to be the |
| 5002 | /// first argument of a parameter. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5003 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5004 | /// If RequiresArg is true, then the first argument of the function is required |
| 5005 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5006 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5007 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 5008 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 5009 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 5010 | /// |
| 5011 | /// [C++11] exception-specification: |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5012 | /// dynamic-exception-specification |
| 5013 | /// noexcept-specification |
| 5014 | /// |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5015 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5016 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5017 | BalancedDelimiterTracker &Tracker, |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5018 | bool IsAmbiguous, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5019 | bool RequiresArg) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5020 | assert(getCurScope()->isFunctionPrototypeScope() && |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5021 | "Should call from a Function scope"); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5022 | // lparen is already consumed! |
| 5023 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 5024 | |
| 5025 | // This should be true when the function has typed arguments. |
| 5026 | // Otherwise, it is treated as a K&R-style function. |
| 5027 | bool HasProto = false; |
| 5028 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5029 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5030 | // Remember where we see an ellipsis, if any. |
| 5031 | SourceLocation EllipsisLoc; |
| 5032 | |
| 5033 | DeclSpec DS(AttrFactory); |
| 5034 | bool RefQualifierIsLValueRef = true; |
| 5035 | SourceLocation RefQualifierLoc; |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 5036 | SourceLocation ConstQualifierLoc; |
| 5037 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5038 | ExceptionSpecificationType ESpecType = EST_None; |
| 5039 | SourceRange ESpecRange; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5040 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 5041 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5042 | ExprResult NoexceptExpr; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5043 | ParsedAttributes FnAttrs(AttrFactory); |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 5044 | TypeResult TrailingReturnType; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5045 | |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5046 | /* LocalEndLoc is the end location for the local FunctionTypeLoc. |
| 5047 | EndLoc is the end location for the function declarator. |
| 5048 | They differ for trailing return types. */ |
| 5049 | SourceLocation StartLoc, LocalEndLoc, EndLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5050 | SourceLocation LParenLoc, RParenLoc; |
| 5051 | LParenLoc = Tracker.getOpenLocation(); |
| 5052 | StartLoc = LParenLoc; |
| 5053 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5054 | if (isFunctionDeclaratorIdentifierList()) { |
| 5055 | if (RequiresArg) |
| 5056 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5057 | |
| 5058 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 5059 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5060 | Tracker.consumeClose(); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5061 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5062 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5063 | EndLoc = RParenLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5064 | } else { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5065 | if (Tok.isNot(tok::r_paren)) |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5066 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, |
| 5067 | EllipsisLoc); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5068 | else if (RequiresArg) |
| 5069 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5070 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5071 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5072 | |
| 5073 | // If we have the closing ')', eat it. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5074 | Tracker.consumeClose(); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5075 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5076 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5077 | EndLoc = RParenLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5078 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5079 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5080 | // FIXME: Accept these components in any order, and produce fixits to |
| 5081 | // correct the order if the user gets it wrong. Ideally we should deal |
| 5082 | // with the virt-specifier-seq and pure-specifier in the same way. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5083 | |
| 5084 | // Parse cv-qualifier-seq[opt]. |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5085 | ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false, |
| 5086 | /*CXX11AttributesAllowed*/ false, |
| 5087 | /*AtomicAllowed*/ false); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5088 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 5089 | EndLoc = DS.getSourceRange().getEnd(); |
| 5090 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 5091 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 5092 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5093 | |
| 5094 | // Parse ref-qualifier[opt]. |
| 5095 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5096 | Diag(Tok, getLangOpts().CPlusPlus11 ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5097 | diag::warn_cxx98_compat_ref_qualifier : |
| 5098 | diag::ext_ref_qualifier); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5099 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5100 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 5101 | RefQualifierLoc = ConsumeToken(); |
| 5102 | EndLoc = RefQualifierLoc; |
| 5103 | } |
| 5104 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5105 | // C++11 [expr.prim.general]p3: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5106 | // If a declaration declares a member function or member function |
| 5107 | // template of a class X, the expression this is a prvalue of type |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5108 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5109 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5110 | // declarator. |
Richard Smith | d922779 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5111 | // FIXME: currently, "static" case isn't handled correctly. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5112 | bool IsCXX11MemberFunction = |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5113 | getLangOpts().CPlusPlus11 && |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5114 | D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef && |
Richard Smith | d922779 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5115 | (D.getContext() == Declarator::MemberContext |
| 5116 | ? !D.getDeclSpec().isFriendSpecified() |
| 5117 | : D.getContext() == Declarator::FileContext && |
| 5118 | D.getCXXScopeSpec().isValid() && |
| 5119 | Actions.CurContext->isRecord()); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5120 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 5121 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
Richard Smith | 7b19cb1 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 5122 | DS.getTypeQualifiers() | |
Richard Smith | 8404626 | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 5123 | (D.getDeclSpec().isConstexprSpecified() && |
| 5124 | !getLangOpts().CPlusPlus1y |
Richard Smith | 7b19cb1 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 5125 | ? Qualifiers::Const : 0), |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5126 | IsCXX11MemberFunction); |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5127 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5128 | // Parse exception-specification[opt]. |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5129 | ESpecType = tryParseExceptionSpecification(ESpecRange, |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 5130 | DynamicExceptions, |
| 5131 | DynamicExceptionRanges, |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5132 | NoexceptExpr); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5133 | if (ESpecType != EST_None) |
| 5134 | EndLoc = ESpecRange.getEnd(); |
| 5135 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5136 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 5137 | // after the exception-specification. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5138 | MaybeParseCXX11Attributes(FnAttrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5139 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5140 | // Parse trailing-return-type[opt]. |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5141 | LocalEndLoc = EndLoc; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5142 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5143 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5144 | if (D.getDeclSpec().getTypeSpecType() == TST_auto) |
| 5145 | StartLoc = D.getDeclSpec().getTypeSpecTypeLoc(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5146 | LocalEndLoc = Tok.getLocation(); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 5147 | SourceRange Range; |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 5148 | TrailingReturnType = ParseTrailingReturnType(Range); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5149 | EndLoc = Range.getEnd(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5150 | } |
| 5151 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5152 | } |
| 5153 | |
| 5154 | // Remember that we parsed a function type, and remember the attributes. |
| 5155 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5156 | IsAmbiguous, |
| 5157 | LParenLoc, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5158 | ParamInfo.data(), ParamInfo.size(), |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5159 | EllipsisLoc, RParenLoc, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5160 | DS.getTypeQualifiers(), |
| 5161 | RefQualifierIsLValueRef, |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 5162 | RefQualifierLoc, ConstQualifierLoc, |
| 5163 | VolatileQualifierLoc, |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 5164 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5165 | ESpecType, ESpecRange.getBegin(), |
| 5166 | DynamicExceptions.data(), |
| 5167 | DynamicExceptionRanges.data(), |
| 5168 | DynamicExceptions.size(), |
| 5169 | NoexceptExpr.isUsable() ? |
| 5170 | NoexceptExpr.get() : 0, |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5171 | StartLoc, LocalEndLoc, D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5172 | TrailingReturnType), |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5173 | FnAttrs, EndLoc); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5174 | } |
| 5175 | |
| 5176 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 5177 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 5178 | /// |
| 5179 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 5180 | /// abstract-declarators. |
| 5181 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5182 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5183 | && Tok.is(tok::identifier) |
| 5184 | && !TryAltiVecVectorToken() |
| 5185 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 5186 | // 6.7.5.3p11. |
| 5187 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 5188 | // Identifier lists follow a really simple grammar: the identifiers can |
| 5189 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 5190 | // identifier lists are really rare in the brave new modern world, and |
| 5191 | // it is very common for someone to typo a type in a non-K&R style |
| 5192 | // list. If we are presented with something like: "void foo(intptr x, |
| 5193 | // float y)", we don't want to start parsing the function declarator as |
| 5194 | // though it is a K&R style declarator just because intptr is an |
| 5195 | // invalid type. |
| 5196 | // |
| 5197 | // To handle this, we check to see if the token after the first |
| 5198 | // identifier is a "," or ")". Only then do we parse it as an |
| 5199 | // identifier list. |
| 5200 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 5201 | } |
| 5202 | |
| 5203 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 5204 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 5205 | /// |
| 5206 | /// After returning, ParamInfo will hold the parsed parameters. |
| 5207 | /// |
| 5208 | /// identifier-list: [C99 6.7.5] |
| 5209 | /// identifier |
| 5210 | /// identifier-list ',' identifier |
| 5211 | /// |
| 5212 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 5213 | Declarator &D, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5214 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5215 | // If there was no identifier specified for the declarator, either we are in |
| 5216 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 5217 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 5218 | // diagnose this. |
| 5219 | if (!D.getIdentifier()) |
| 5220 | Diag(Tok, diag::ext_ident_list_in_param); |
| 5221 | |
| 5222 | // Maintain an efficient lookup of params we have seen so far. |
| 5223 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 5224 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5225 | do { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5226 | // If this isn't an identifier, report the error and skip until ')'. |
| 5227 | if (Tok.isNot(tok::identifier)) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5228 | Diag(Tok, diag::err_expected) << tok::identifier; |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5229 | SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5230 | // Forget we parsed anything. |
| 5231 | ParamInfo.clear(); |
| 5232 | return; |
| 5233 | } |
| 5234 | |
| 5235 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 5236 | |
| 5237 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 5238 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 5239 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 5240 | |
| 5241 | // Verify that the argument identifier has not already been mentioned. |
| 5242 | if (!ParamsSoFar.insert(ParmII)) { |
| 5243 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 5244 | } else { |
| 5245 | // Remember this identifier in ParamInfo. |
| 5246 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 5247 | Tok.getLocation(), |
| 5248 | 0)); |
| 5249 | } |
| 5250 | |
| 5251 | // Eat the identifier. |
| 5252 | ConsumeToken(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5253 | // The list continues if we see a comma. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5254 | } while (TryConsumeToken(tok::comma)); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5255 | } |
| 5256 | |
| 5257 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 5258 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 5259 | /// identifier list. |
| 5260 | /// |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5261 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 5262 | /// caller parsed those arguments immediately after the open paren - they should |
| 5263 | /// be considered to be part of the first parameter. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5264 | /// |
| 5265 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 5266 | /// be the location of the ellipsis, if any was parsed. |
| 5267 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5268 | /// parameter-type-list: [C99 6.7.5] |
| 5269 | /// parameter-list |
| 5270 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5271 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5272 | /// |
| 5273 | /// parameter-list: [C99 6.7.5] |
| 5274 | /// parameter-declaration |
| 5275 | /// parameter-list ',' parameter-declaration |
| 5276 | /// |
| 5277 | /// parameter-declaration: [C99 6.7.5] |
| 5278 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5279 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5280 | /// [C++11] initializer-clause |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5281 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 5282 | /// declaration-specifiers abstract-declarator[opt] |
| 5283 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 5284 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5285 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5286 | /// [C++11] attribute-specifier-seq parameter-declaration |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5287 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5288 | void Parser::ParseParameterDeclarationClause( |
| 5289 | Declarator &D, |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5290 | ParsedAttributes &FirstArgAttrs, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5291 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5292 | SourceLocation &EllipsisLoc) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5293 | do { |
| 5294 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 5295 | // before deciding this was a parameter-declaration-clause. |
| 5296 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5297 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5298 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5299 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5300 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5301 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5302 | |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5303 | // Parse any C++11 attributes. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5304 | MaybeParseCXX11Attributes(DS.getAttributes()); |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5305 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5306 | // Skip any Microsoft attributes before a param. |
Chad Rosier | 16f90bf | 2012-12-20 20:37:53 +0000 | [diff] [blame] | 5307 | MaybeParseMicrosoftAttributes(DS.getAttributes()); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5308 | |
| 5309 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5310 | |
| 5311 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5312 | // Take them so that we only apply the attributes to the first parameter. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5313 | // FIXME: If we can leave the attributes in the token stream somehow, we can |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5314 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 5315 | // too much hassle. |
| 5316 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5317 | |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 5318 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5319 | |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5320 | |
| 5321 | // Parse the declarator. This is "PrototypeContext" or |
| 5322 | // "LambdaExprParameterContext", because we must accept either |
| 5323 | // 'declarator' or 'abstract-declarator' here. |
| 5324 | Declarator ParmDeclarator(DS, |
| 5325 | D.getContext() == Declarator::LambdaExprContext ? |
| 5326 | Declarator::LambdaExprParameterContext : |
| 5327 | Declarator::PrototypeContext); |
| 5328 | ParseDeclarator(ParmDeclarator); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5329 | |
| 5330 | // Parse GNU attributes, if present. |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5331 | MaybeParseGNUAttributes(ParmDeclarator); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5332 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5333 | // Remember this parsed parameter in ParamInfo. |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5334 | IdentifierInfo *ParmII = ParmDeclarator.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5335 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5336 | // DefArgToks is used when the parsing of default arguments needs |
| 5337 | // to be delayed. |
| 5338 | CachedTokens *DefArgToks = 0; |
| 5339 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5340 | // If no parameter was specified, verify that *something* was specified, |
| 5341 | // otherwise we have a missing type and identifier. |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5342 | if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 && |
| 5343 | ParmDeclarator.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5344 | // Completely missing, emit error. |
| 5345 | Diag(DSStart, diag::err_missing_param); |
| 5346 | } else { |
| 5347 | // Otherwise, we have something. Add it and let semantic analysis try |
| 5348 | // to grok it and add the result to the ParamInfo we are building. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5349 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5350 | // Inform the actions module about the parameter declarator, so it gets |
| 5351 | // added to the current scope. |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5352 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), |
| 5353 | ParmDeclarator); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5354 | // Parse the default argument, if any. We parse the default |
| 5355 | // arguments in all dialects; the semantic analysis in |
| 5356 | // ActOnParamDefaultArgument will reject the default argument in |
| 5357 | // C. |
| 5358 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5359 | SourceLocation EqualLoc = Tok.getLocation(); |
| 5360 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5361 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5362 | if (D.getContext() == Declarator::MemberContext) { |
| 5363 | // If we're inside a class definition, cache the tokens |
| 5364 | // corresponding to the default argument. We'll actually parse |
| 5365 | // them when we see the end of the class definition. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5366 | // FIXME: Can we use a smart pointer for Toks? |
| 5367 | DefArgToks = new CachedTokens; |
| 5368 | |
Richard Smith | 9bd3cdc | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 5369 | if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5370 | delete DefArgToks; |
| 5371 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5372 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5373 | } else { |
| 5374 | // Mark the end of the default argument so that we know when to |
| 5375 | // stop when we parse it later on. |
| 5376 | Token DefArgEnd; |
| 5377 | DefArgEnd.startToken(); |
| 5378 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 5379 | DefArgEnd.setLocation(Tok.getLocation()); |
| 5380 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5381 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 5382 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5383 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5384 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5385 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5386 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5387 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5388 | // The argument isn't actually potentially evaluated unless it is |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5389 | // used. |
| 5390 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 5391 | Sema::PotentiallyEvaluatedIfUsed, |
| 5392 | Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5393 | |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5394 | ExprResult DefArgResult; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5395 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5396 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5397 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5398 | } else |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5399 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5400 | if (DefArgResult.isInvalid()) { |
| 5401 | Actions.ActOnParamDefaultArgumentError(Param); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5402 | SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5403 | } else { |
| 5404 | // Inform the actions module about the default argument |
| 5405 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5406 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5407 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5408 | } |
| 5409 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5410 | |
| 5411 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5412 | ParmDeclarator.getIdentifierLoc(), |
| 5413 | Param, DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5414 | } |
| 5415 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5416 | if (TryConsumeToken(tok::ellipsis, EllipsisLoc) && |
| 5417 | !getLangOpts().CPlusPlus) { |
| 5418 | // We have ellipsis without a preceding ',', which is ill-formed |
| 5419 | // in C. Complain and provide the fix. |
| 5420 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
| 5421 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5422 | break; |
| 5423 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5424 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5425 | // If the next token is a comma, consume it and keep reading arguments. |
| 5426 | } while (TryConsumeToken(tok::comma)); |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 5427 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5428 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5429 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 5430 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 5431 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 5432 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 5433 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5434 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 5435 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5436 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5437 | if (CheckProhibitedCXX11Attribute()) |
| 5438 | return; |
| 5439 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5440 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 5441 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5442 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5443 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 5444 | // This code does a fast path to handle some of the most obvious cases. |
| 5445 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5446 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5447 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5448 | MaybeParseCXX11Attributes(attrs); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5449 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5450 | // Remember that we parsed the empty array type. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5451 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5452 | T.getOpenLocation(), |
| 5453 | T.getCloseLocation()), |
| 5454 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5455 | return; |
| 5456 | } else if (Tok.getKind() == tok::numeric_constant && |
| 5457 | GetLookAheadToken(1).is(tok::r_square)) { |
| 5458 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5459 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5460 | ConsumeToken(); |
| 5461 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5462 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5463 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5464 | MaybeParseCXX11Attributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5465 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5466 | // Remember that we parsed a array type, and remember its features. |
Nikola Smiljanic | ebf0fa8 | 2013-01-11 08:33:05 +0000 | [diff] [blame] | 5467 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5468 | ExprRes.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5469 | T.getOpenLocation(), |
| 5470 | T.getCloseLocation()), |
| 5471 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5472 | return; |
| 5473 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5474 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5475 | // If valid, this location is the position where we read the 'static' keyword. |
| 5476 | SourceLocation StaticLoc; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5477 | TryConsumeToken(tok::kw_static, StaticLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5478 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5479 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5480 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5481 | DeclSpec DS(AttrFactory); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5482 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5483 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5484 | // If we haven't already read 'static', check to see if there is one after the |
| 5485 | // type-qualifier-list. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5486 | if (!StaticLoc.isValid()) |
| 5487 | TryConsumeToken(tok::kw_static, StaticLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5488 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5489 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 5490 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5491 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5492 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5493 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 5494 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 5495 | // the token after the star is a ']'. Since stars in arrays are |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5496 | // infrequent, use of lookahead is not costly here. |
| 5497 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 5498 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5499 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5500 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5501 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5502 | StaticLoc = SourceLocation(); // Drop the static. |
| 5503 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5504 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5505 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5506 | // Note, in C89, this production uses the constant-expr production instead |
| 5507 | // of assignment-expr. The only difference is that assignment-expr allows |
| 5508 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 5509 | // are not i-c-e's, so we don't need to distinguish between the two here. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5510 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5511 | // Parse the constant-expression or assignment-expression now (depending |
| 5512 | // on dialect). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5513 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5514 | NumElements = ParseConstantExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5515 | } else { |
| 5516 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 5517 | Sema::ConstantEvaluated); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5518 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5519 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5520 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5521 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5522 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 5523 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 5524 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5525 | // If the expression was invalid, skip it. |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5526 | SkipUntil(tok::r_square, StopAtSemi); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5527 | return; |
| 5528 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5529 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5530 | T.consumeClose(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5531 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5532 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5533 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5534 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5535 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5536 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5537 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5538 | NumElements.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5539 | T.getOpenLocation(), |
| 5540 | T.getCloseLocation()), |
| 5541 | attrs, T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5542 | } |
| 5543 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5544 | /// [GNU] typeof-specifier: |
| 5545 | /// typeof ( expressions ) |
| 5546 | /// typeof ( type-name ) |
| 5547 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5548 | /// |
| 5549 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5550 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5551 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5552 | SourceLocation StartLoc = ConsumeToken(); |
| 5553 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5554 | const bool hasParens = Tok.is(tok::l_paren); |
| 5555 | |
Eli Friedman | 80bfa3d | 2012-09-26 04:34:21 +0000 | [diff] [blame] | 5556 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated, |
| 5557 | Sema::ReuseLambdaContextDecl); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5558 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5559 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5560 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5561 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5562 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 5563 | CastTy, CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5564 | if (hasParens) |
| 5565 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5566 | |
| 5567 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5568 | // FIXME: Not accurate, the range gets one token more than it should. |
| 5569 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5570 | else |
| 5571 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5572 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5573 | if (isCastExpr) { |
| 5574 | if (!CastTy) { |
| 5575 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5576 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 5577 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5578 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5579 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5580 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5581 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5582 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5583 | DiagID, CastTy, |
| 5584 | Actions.getASTContext().getPrintingPolicy())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5585 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5586 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5587 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5588 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5589 | // If we get here, the operand to the typeof was an expresion. |
| 5590 | if (Operand.isInvalid()) { |
| 5591 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 5592 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5593 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5594 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5595 | // We might need to transform the operand if it is potentially evaluated. |
| 5596 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 5597 | if (Operand.isInvalid()) { |
| 5598 | DS.SetTypeSpecError(); |
| 5599 | return; |
| 5600 | } |
| 5601 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5602 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5603 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5604 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5605 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5606 | DiagID, Operand.get(), |
| 5607 | Actions.getASTContext().getPrintingPolicy())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5608 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5609 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5610 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 5611 | /// [C11] atomic-specifier: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5612 | /// _Atomic ( type-name ) |
| 5613 | /// |
| 5614 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5615 | assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) && |
| 5616 | "Not an atomic specifier"); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5617 | |
| 5618 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5619 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5620 | if (T.consumeOpen()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5621 | return; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5622 | |
| 5623 | TypeResult Result = ParseTypeName(); |
| 5624 | if (Result.isInvalid()) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5625 | SkipUntil(tok::r_paren, StopAtSemi); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5626 | return; |
| 5627 | } |
| 5628 | |
| 5629 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5630 | T.consumeClose(); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5631 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5632 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5633 | return; |
| 5634 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5635 | DS.setTypeofParensRange(T.getRange()); |
| 5636 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5637 | |
| 5638 | const char *PrevSpec = 0; |
| 5639 | unsigned DiagID; |
| 5640 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5641 | DiagID, Result.release(), |
| 5642 | Actions.getASTContext().getPrintingPolicy())) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5643 | Diag(StartLoc, DiagID) << PrevSpec; |
| 5644 | } |
| 5645 | |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5646 | |
| 5647 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 5648 | /// from TryAltiVecVectorToken. |
| 5649 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 5650 | Token Next = NextToken(); |
| 5651 | switch (Next.getKind()) { |
| 5652 | default: return false; |
| 5653 | case tok::kw_short: |
| 5654 | case tok::kw_long: |
| 5655 | case tok::kw_signed: |
| 5656 | case tok::kw_unsigned: |
| 5657 | case tok::kw_void: |
| 5658 | case tok::kw_char: |
| 5659 | case tok::kw_int: |
| 5660 | case tok::kw_float: |
| 5661 | case tok::kw_double: |
| 5662 | case tok::kw_bool: |
| 5663 | case tok::kw___pixel: |
| 5664 | Tok.setKind(tok::kw___vector); |
| 5665 | return true; |
| 5666 | case tok::identifier: |
| 5667 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5668 | Tok.setKind(tok::kw___vector); |
| 5669 | return true; |
| 5670 | } |
Bill Schmidt | 3e3d20b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5671 | if (Next.getIdentifierInfo() == Ident_bool) { |
| 5672 | Tok.setKind(tok::kw___vector); |
| 5673 | return true; |
| 5674 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5675 | return false; |
| 5676 | } |
| 5677 | } |
| 5678 | |
| 5679 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 5680 | const char *&PrevSpec, unsigned &DiagID, |
| 5681 | bool &isInvalid) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5682 | const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy(); |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5683 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 5684 | Token Next = NextToken(); |
| 5685 | switch (Next.getKind()) { |
| 5686 | case tok::kw_short: |
| 5687 | case tok::kw_long: |
| 5688 | case tok::kw_signed: |
| 5689 | case tok::kw_unsigned: |
| 5690 | case tok::kw_void: |
| 5691 | case tok::kw_char: |
| 5692 | case tok::kw_int: |
| 5693 | case tok::kw_float: |
| 5694 | case tok::kw_double: |
| 5695 | case tok::kw_bool: |
| 5696 | case tok::kw___pixel: |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5697 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy); |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5698 | return true; |
| 5699 | case tok::identifier: |
| 5700 | if (Next.getIdentifierInfo() == Ident_pixel) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5701 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy); |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5702 | return true; |
| 5703 | } |
Bill Schmidt | 3e3d20b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5704 | if (Next.getIdentifierInfo() == Ident_bool) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5705 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy); |
Bill Schmidt | 3e3d20b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5706 | return true; |
| 5707 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5708 | break; |
| 5709 | default: |
| 5710 | break; |
| 5711 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 5712 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5713 | DS.isTypeAltiVecVector()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5714 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy); |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5715 | return true; |
Bill Schmidt | 3e3d20b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5716 | } else if ((Tok.getIdentifierInfo() == Ident_bool) && |
| 5717 | DS.isTypeAltiVecVector()) { |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame^] | 5718 | isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy); |
Bill Schmidt | 3e3d20b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5719 | return true; |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5720 | } |
| 5721 | return false; |
| 5722 | } |