Chris Lattner | 7ad0fbe | 2006-11-05 07:46:30 +0000 | [diff] [blame] | 1 | //===--- ParseDecl.cpp - Declaration Parsing ------------------------------===// |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Declaration portions of the Parser interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Parse/Parser.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "RAIIObjectsForParser.h" |
Larisse Voufo | 725de3e | 2013-06-21 00:08:46 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclTemplate.h" |
Benjamin Kramer | d7d2b1f | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 17 | #include "clang/Basic/AddressSpaces.h" |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 18 | #include "clang/Basic/CharInfo.h" |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 19 | #include "clang/Basic/OpenCL.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/Parse/ParseDiagnostic.h" |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 21 | #include "clang/Sema/Lookup.h" |
John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 22 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 23 | #include "clang/Sema/PrettyDeclStackTrace.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Sema/Scope.h" |
Chris Lattner | ad9ac94 | 2007-01-23 01:14:52 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallSet.h" |
Benjamin Kramer | 4903802 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallString.h" |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringSwitch.h" |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | // C99 6.7: Declarations. |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 34 | /// ParseTypeName |
| 35 | /// type-name: [C99 6.7.6] |
| 36 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 37 | /// |
| 38 | /// Called type-id in C++. |
Douglas Gregor | 205d5e3 | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 39 | TypeResult Parser::ParseTypeName(SourceRange *Range, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 40 | Declarator::TheContext Context, |
Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 41 | AccessSpecifier AS, |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 42 | Decl **OwnedType, |
| 43 | ParsedAttributes *Attrs) { |
Richard Smith | 62dad82 | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 44 | DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context); |
Richard Smith | 2f07ad5 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 45 | if (DSC == DSC_normal) |
| 46 | DSC = DSC_type_specifier; |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 47 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 48 | // Parse the common declaration-specifiers piece. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 49 | DeclSpec DS(AttrFactory); |
Richard Smith | 54ecd98 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 50 | if (Attrs) |
| 51 | DS.addAttributes(Attrs->getList()); |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 52 | ParseSpecifierQualifierList(DS, AS, DSC); |
Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 53 | if (OwnedType) |
| 54 | *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0; |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 55 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 56 | // Parse the abstract-declarator, if present. |
Douglas Gregor | 205d5e3 | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 57 | Declarator DeclaratorInfo(DS, Context); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 58 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | d643456 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 59 | if (Range) |
| 60 | *Range = DeclaratorInfo.getSourceRange(); |
| 61 | |
Chris Lattner | f6d1c9c | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 62 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 220cac5 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 63 | return true; |
| 64 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 65 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 68 | |
| 69 | /// isAttributeLateParsed - Return true if the attribute has arguments that |
| 70 | /// require late parsing. |
| 71 | static bool isAttributeLateParsed(const IdentifierInfo &II) { |
| 72 | return llvm::StringSwitch<bool>(II.getName()) |
| 73 | #include "clang/Parse/AttrLateParsed.inc" |
| 74 | .Default(false); |
| 75 | } |
| 76 | |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 77 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 78 | /// |
| 79 | /// [GNU] attributes: |
| 80 | /// attribute |
| 81 | /// attributes attribute |
| 82 | /// |
| 83 | /// [GNU] attribute: |
| 84 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 85 | /// |
| 86 | /// [GNU] attribute-list: |
| 87 | /// attrib |
| 88 | /// attribute_list ',' attrib |
| 89 | /// |
| 90 | /// [GNU] attrib: |
| 91 | /// empty |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 92 | /// attrib-name |
| 93 | /// attrib-name '(' identifier ')' |
| 94 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 95 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
Chris Lattner | b8cd5c2 | 2006-08-15 04:10:46 +0000 | [diff] [blame] | 96 | /// |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 97 | /// [GNU] attrib-name: |
| 98 | /// identifier |
| 99 | /// typespec |
| 100 | /// typequal |
| 101 | /// storageclass |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | /// |
Richard Smith | f7ca0c0 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 103 | /// Whether an attribute takes an 'identifier' is determined by the |
| 104 | /// attrib-name. GCC's behavior here is not worth imitating: |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 105 | /// |
Richard Smith | f7ca0c0 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 106 | /// * In C mode, if the attribute argument list starts with an identifier |
| 107 | /// followed by a ',' or an ')', and the identifier doesn't resolve to |
| 108 | /// a type, it is parsed as an identifier. If the attribute actually |
| 109 | /// wanted an expression, it's out of luck (but it turns out that no |
| 110 | /// attributes work that way, because C constant expressions are very |
| 111 | /// limited). |
| 112 | /// * In C++ mode, if the attribute argument list starts with an identifier, |
| 113 | /// and the attribute *wants* an identifier, it is parsed as an identifier. |
| 114 | /// At block scope, any additional tokens between the identifier and the |
| 115 | /// ',' or ')' are ignored, otherwise they produce a parse error. |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 116 | /// |
Richard Smith | f7ca0c0 | 2013-09-03 18:57:36 +0000 | [diff] [blame] | 117 | /// We follow the C++ model, but don't allow junk after the identifier. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 118 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 119 | SourceLocation *endLoc, |
| 120 | LateParsedAttrList *LateAttrs) { |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 121 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 123 | while (Tok.is(tok::kw___attribute)) { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 124 | ConsumeToken(); |
| 125 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 126 | "attribute")) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 127 | SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ; |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 128 | return; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 129 | } |
| 130 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 131 | SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ; |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 132 | return; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 133 | } |
| 134 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 135 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 136 | Tok.is(tok::comma)) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | if (Tok.is(tok::comma)) { |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 138 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 139 | ConsumeToken(); |
| 140 | continue; |
| 141 | } |
| 142 | // we have an identifier or declaration specifier (const, int, etc.) |
| 143 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 144 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 146 | if (Tok.is(tok::l_paren)) { |
| 147 | // handle "parameterized" attributes |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 148 | if (LateAttrs && isAttributeLateParsed(*AttrName)) { |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 149 | LateParsedAttribute *LA = |
| 150 | new LateParsedAttribute(this, *AttrName, AttrNameLoc); |
| 151 | LateAttrs->push_back(LA); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 152 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 153 | // Attributes in a class are parsed at the end of the class, along |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 154 | // with other late-parsed declarations. |
DeLesley Hutchins | 66e300e | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 155 | if (!ClassStack.empty() && !LateAttrs->parseSoon()) |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 156 | getCurrentClass().LateParsedDeclarations.push_back(LA); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 158 | // consume everything up to and including the matching right parens |
| 159 | ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 161 | Token Eof; |
| 162 | Eof.startToken(); |
| 163 | Eof.setLocation(Tok.getLocation()); |
| 164 | LA->Toks.push_back(Eof); |
| 165 | } else { |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 166 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, |
Michael Han | 360d225 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 167 | 0, SourceLocation(), AttributeList::AS_GNU); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 168 | } |
| 169 | } else { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 170 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 171 | AttributeList::AS_GNU); |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
Steve Naroff | 98d153c | 2007-06-06 23:19:11 +0000 | [diff] [blame] | 174 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 175 | SkipUntil(tok::r_paren, StopAtSemi); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 176 | SourceLocation Loc = Tok.getLocation(); |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 177 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 178 | SkipUntil(tok::r_paren, StopAtSemi); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 179 | if (endLoc) |
| 180 | *endLoc = Loc; |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 181 | } |
Steve Naroff | 0f2fe17 | 2007-06-01 17:11:19 +0000 | [diff] [blame] | 182 | } |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 183 | |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 184 | /// \brief Normalizes an attribute name by dropping prefixed and suffixed __. |
| 185 | static StringRef normalizeAttrName(StringRef Name) { |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 186 | if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__")) |
| 187 | Name = Name.drop_front(2).drop_back(2); |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 188 | return Name; |
| 189 | } |
| 190 | |
| 191 | /// \brief Determine whether the given attribute has an identifier argument. |
| 192 | static bool attributeHasIdentifierArg(const IdentifierInfo &II) { |
| 193 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 194 | #include "clang/Parse/AttrIdentifierArg.inc" |
Douglas Gregor | d2472d4 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 195 | .Default(false); |
| 196 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 197 | |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 198 | /// \brief Determine whether the given attribute parses a type argument. |
| 199 | static bool attributeIsTypeArgAttr(const IdentifierInfo &II) { |
| 200 | return llvm::StringSwitch<bool>(normalizeAttrName(II.getName())) |
| 201 | #include "clang/Parse/AttrTypeArg.inc" |
| 202 | .Default(false); |
| 203 | } |
| 204 | |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 205 | IdentifierLoc *Parser::ParseIdentifierLoc() { |
| 206 | assert(Tok.is(tok::identifier) && "expected an identifier"); |
| 207 | IdentifierLoc *IL = IdentifierLoc::create(Actions.Context, |
| 208 | Tok.getLocation(), |
| 209 | Tok.getIdentifierInfo()); |
| 210 | ConsumeToken(); |
| 211 | return IL; |
| 212 | } |
| 213 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 214 | void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName, |
| 215 | SourceLocation AttrNameLoc, |
| 216 | ParsedAttributes &Attrs, |
| 217 | SourceLocation *EndLoc) { |
| 218 | BalancedDelimiterTracker Parens(*this, tok::l_paren); |
| 219 | Parens.consumeOpen(); |
| 220 | |
| 221 | TypeResult T; |
| 222 | if (Tok.isNot(tok::r_paren)) |
| 223 | T = ParseTypeName(); |
| 224 | |
| 225 | if (Parens.consumeClose()) |
| 226 | return; |
| 227 | |
| 228 | if (T.isInvalid()) |
| 229 | return; |
| 230 | |
| 231 | if (T.isUsable()) |
| 232 | Attrs.addNewTypeAttr(&AttrName, |
| 233 | SourceRange(AttrNameLoc, Parens.getCloseLocation()), 0, |
| 234 | AttrNameLoc, T.get(), AttributeList::AS_GNU); |
| 235 | else |
| 236 | Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()), |
| 237 | 0, AttrNameLoc, 0, 0, AttributeList::AS_GNU); |
| 238 | } |
| 239 | |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 240 | /// Parse the arguments to a parameterized GNU attribute or |
| 241 | /// a C++11 attribute in "gnu" namespace. |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 242 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 243 | SourceLocation AttrNameLoc, |
| 244 | ParsedAttributes &Attrs, |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 245 | SourceLocation *EndLoc, |
| 246 | IdentifierInfo *ScopeName, |
| 247 | SourceLocation ScopeLoc, |
| 248 | AttributeList::Syntax Syntax) { |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 249 | |
| 250 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 251 | |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 252 | AttributeList::Kind AttrKind = |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 253 | AttributeList::getKind(AttrName, ScopeName, Syntax); |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 254 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 255 | // Availability attributes have their own grammar. |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 256 | // FIXME: All these cases fail to pass in the syntax and scope, and might be |
| 257 | // written as C++11 gnu:: attributes. |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 258 | if (AttrKind == AttributeList::AT_Availability) { |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 259 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 260 | return; |
| 261 | } |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 262 | |
| 263 | if (AttrKind == AttributeList::AT_ObjCBridgeRelated) { |
| 264 | ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 265 | return; |
| 266 | } |
| 267 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 268 | // Thread safety attributes are parsed in an unevaluated context. |
| 269 | // FIXME: Share the bulk of the parsing code here and just pull out |
| 270 | // the unevaluated context. |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 271 | if (IsThreadSafetyAttribute(AttrName->getName())) { |
| 272 | ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 273 | return; |
| 274 | } |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 275 | // Type safety attributes have their own grammar. |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 276 | if (AttrKind == AttributeList::AT_TypeTagForDatatype) { |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 277 | ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 278 | return; |
| 279 | } |
Aaron Ballman | 4768b31 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 280 | // Some attributes expect solely a type parameter. |
| 281 | if (attributeIsTypeArgAttr(*AttrName)) { |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 282 | ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 283 | return; |
| 284 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 285 | |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 286 | // Ignore the left paren location for now. |
| 287 | ConsumeParen(); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 288 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 289 | ArgsVector ArgExprs; |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 290 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 291 | if (Tok.is(tok::identifier)) { |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 292 | // If this attribute wants an 'identifier' argument, make it so. |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 293 | bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName); |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 294 | |
| 295 | // If we don't know how to parse this attribute, but this is the only |
| 296 | // token in this argument, assume it's meant to be an identifier. |
Aaron Ballman | 6603747 | 2013-12-04 15:32:26 +0000 | [diff] [blame] | 297 | if (AttrKind == AttributeList::UnknownAttribute || |
| 298 | AttrKind == AttributeList::IgnoredAttribute) { |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 299 | const Token &Next = NextToken(); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 300 | IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma); |
Richard Smith | 66e7168 | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 301 | } |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 302 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 303 | if (IsIdentifierArg) |
| 304 | ArgExprs.push_back(ParseIdentifierLoc()); |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 307 | if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) { |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 308 | // Eat the comma. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 309 | if (!ArgExprs.empty()) |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 310 | ConsumeToken(); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 311 | |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 312 | // Parse the non-empty comma-separated list of expressions. |
| 313 | while (1) { |
| 314 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 315 | if (ArgExpr.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 316 | SkipUntil(tok::r_paren, StopAtSemi); |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 317 | return; |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 318 | } |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 319 | ArgExprs.push_back(ArgExpr.release()); |
| 320 | if (Tok.isNot(tok::comma)) |
| 321 | break; |
| 322 | ConsumeToken(); // Eat the comma, move to the next argument |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 323 | } |
Fariborz Jahanian | 6b70865 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 324 | } |
Richard Smith | b12bf69 | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 325 | |
| 326 | SourceLocation RParen = Tok.getLocation(); |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 327 | if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
Michael Han | 360d225 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 328 | SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc; |
Richard Smith | b1f9a28 | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 329 | Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc, |
| 330 | ArgExprs.data(), ArgExprs.size(), Syntax); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 334 | /// \brief Parses a single argument for a declspec, including the |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 335 | /// surrounding parens. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 336 | void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName, |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 337 | SourceLocation AttrNameLoc, |
| 338 | ParsedAttributes &Attrs) |
| 339 | { |
| 340 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 341 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 342 | AttrName->getNameStart(), tok::r_paren)) |
| 343 | return; |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 344 | |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 345 | ExprResult ArgExpr(ParseConstantExpression()); |
| 346 | if (ArgExpr.isInvalid()) { |
| 347 | T.skipToEnd(); |
| 348 | return; |
| 349 | } |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 350 | ArgsUnion ExprList = ArgExpr.take(); |
| 351 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1, |
| 352 | AttributeList::AS_Declspec); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 353 | |
| 354 | T.consumeClose(); |
| 355 | } |
| 356 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 357 | /// \brief Determines whether a declspec is a "simple" one requiring no |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 358 | /// arguments. |
| 359 | bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) { |
| 360 | return llvm::StringSwitch<bool>(Ident->getName()) |
| 361 | .Case("dllimport", true) |
| 362 | .Case("dllexport", true) |
| 363 | .Case("noreturn", true) |
| 364 | .Case("nothrow", true) |
| 365 | .Case("noinline", true) |
| 366 | .Case("naked", true) |
| 367 | .Case("appdomain", true) |
| 368 | .Case("process", true) |
| 369 | .Case("jitintrinsic", true) |
| 370 | .Case("noalias", true) |
| 371 | .Case("restrict", true) |
| 372 | .Case("novtable", true) |
| 373 | .Case("selectany", true) |
| 374 | .Case("thread", true) |
Aaron Ballman | 444eb6e | 2013-05-04 16:58:37 +0000 | [diff] [blame] | 375 | .Case("safebuffers", true ) |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 376 | .Default(false); |
| 377 | } |
| 378 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 379 | /// \brief Attempts to parse a declspec which is not simple (one that takes |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 380 | /// parameters). Will return false if we properly handled the declspec, or |
| 381 | /// true if it is an unknown declspec. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 382 | void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident, |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 383 | SourceLocation Loc, |
| 384 | ParsedAttributes &Attrs) { |
| 385 | // Try to handle the easy case first -- these declspecs all take a single |
| 386 | // parameter as their argument. |
| 387 | if (llvm::StringSwitch<bool>(Ident->getName()) |
| 388 | .Case("uuid", true) |
| 389 | .Case("align", true) |
| 390 | .Case("allocate", true) |
| 391 | .Default(false)) { |
| 392 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 393 | } else if (Ident->getName() == "deprecated") { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 394 | // The deprecated declspec has an optional single argument, so we will |
| 395 | // check for a l-paren to decide whether we should parse an argument or |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 396 | // not. |
| 397 | if (Tok.getKind() == tok::l_paren) |
| 398 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 399 | else |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 400 | Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 401 | } else if (Ident->getName() == "property") { |
| 402 | // The property declspec is more complex in that it can take one or two |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 403 | // assignment expressions as a parameter, but the lhs of the assignment |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 404 | // must be named get or put. |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 405 | if (Tok.isNot(tok::l_paren)) { |
| 406 | Diag(Tok.getLocation(), diag::err_expected_lparen_after) |
| 407 | << Ident->getNameStart(); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 408 | return; |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 409 | } |
| 410 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 411 | T.expectAndConsume(diag::err_expected_lparen_after, |
| 412 | Ident->getNameStart(), tok::r_paren); |
| 413 | |
| 414 | enum AccessorKind { |
| 415 | AK_Invalid = -1, |
| 416 | AK_Put = 0, AK_Get = 1 // indices into AccessorNames |
| 417 | }; |
| 418 | IdentifierInfo *AccessorNames[] = { 0, 0 }; |
| 419 | bool HasInvalidAccessor = false; |
| 420 | |
| 421 | // Parse the accessor specifications. |
| 422 | while (true) { |
| 423 | // Stop if this doesn't look like an accessor spec. |
| 424 | if (!Tok.is(tok::identifier)) { |
| 425 | // If the user wrote a completely empty list, use a special diagnostic. |
| 426 | if (Tok.is(tok::r_paren) && !HasInvalidAccessor && |
| 427 | AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) { |
| 428 | Diag(Loc, diag::err_ms_property_no_getter_or_putter); |
| 429 | break; |
| 430 | } |
| 431 | |
| 432 | Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor); |
| 433 | break; |
| 434 | } |
| 435 | |
| 436 | AccessorKind Kind; |
| 437 | SourceLocation KindLoc = Tok.getLocation(); |
| 438 | StringRef KindStr = Tok.getIdentifierInfo()->getName(); |
| 439 | if (KindStr == "get") { |
| 440 | Kind = AK_Get; |
| 441 | } else if (KindStr == "put") { |
| 442 | Kind = AK_Put; |
| 443 | |
| 444 | // Recover from the common mistake of using 'set' instead of 'put'. |
| 445 | } else if (KindStr == "set") { |
| 446 | Diag(KindLoc, diag::err_ms_property_has_set_accessor) |
| 447 | << FixItHint::CreateReplacement(KindLoc, "put"); |
| 448 | Kind = AK_Put; |
| 449 | |
| 450 | // Handle the mistake of forgetting the accessor kind by skipping |
| 451 | // this accessor. |
| 452 | } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) { |
| 453 | Diag(KindLoc, diag::err_ms_property_missing_accessor_kind); |
| 454 | ConsumeToken(); |
| 455 | HasInvalidAccessor = true; |
| 456 | goto next_property_accessor; |
| 457 | |
| 458 | // Otherwise, complain about the unknown accessor kind. |
| 459 | } else { |
| 460 | Diag(KindLoc, diag::err_ms_property_unknown_accessor); |
| 461 | HasInvalidAccessor = true; |
| 462 | Kind = AK_Invalid; |
| 463 | |
| 464 | // Try to keep parsing unless it doesn't look like an accessor spec. |
| 465 | if (!NextToken().is(tok::equal)) break; |
| 466 | } |
| 467 | |
| 468 | // Consume the identifier. |
| 469 | ConsumeToken(); |
| 470 | |
| 471 | // Consume the '='. |
| 472 | if (Tok.is(tok::equal)) { |
| 473 | ConsumeToken(); |
| 474 | } else { |
| 475 | Diag(Tok.getLocation(), diag::err_ms_property_expected_equal) |
| 476 | << KindStr; |
| 477 | break; |
| 478 | } |
| 479 | |
| 480 | // Expect the method name. |
| 481 | if (!Tok.is(tok::identifier)) { |
| 482 | Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name); |
| 483 | break; |
| 484 | } |
| 485 | |
| 486 | if (Kind == AK_Invalid) { |
| 487 | // Just drop invalid accessors. |
| 488 | } else if (AccessorNames[Kind] != NULL) { |
| 489 | // Complain about the repeated accessor, ignore it, and keep parsing. |
| 490 | Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr; |
| 491 | } else { |
| 492 | AccessorNames[Kind] = Tok.getIdentifierInfo(); |
| 493 | } |
| 494 | ConsumeToken(); |
| 495 | |
| 496 | next_property_accessor: |
| 497 | // Keep processing accessors until we run out. |
| 498 | if (Tok.is(tok::comma)) { |
| 499 | ConsumeAnyToken(); |
| 500 | continue; |
| 501 | |
| 502 | // If we run into the ')', stop without consuming it. |
| 503 | } else if (Tok.is(tok::r_paren)) { |
| 504 | break; |
| 505 | } else { |
| 506 | Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen); |
| 507 | break; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | // Only add the property attribute if it was well-formed. |
| 512 | if (!HasInvalidAccessor) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 513 | Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(), |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 514 | AccessorNames[AK_Get], AccessorNames[AK_Put], |
| 515 | AttributeList::AS_Declspec); |
| 516 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 517 | T.skipToEnd(); |
| 518 | } else { |
| 519 | // We don't recognize this as a valid declspec, but instead of creating the |
| 520 | // attribute and allowing sema to warn about it, we will warn here instead. |
| 521 | // This is because some attributes have multiple spellings, but we need to |
| 522 | // disallow that for declspecs (such as align vs aligned). If we made the |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 523 | // attribute, we'd have to split the valid declspec spelling logic into |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 524 | // both locations. |
| 525 | Diag(Loc, diag::warn_ms_declspec_unknown) << Ident; |
| 526 | |
| 527 | // If there's an open paren, we should eat the open and close parens under |
| 528 | // the assumption that this unknown declspec has parameters. |
| 529 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 530 | if (!T.consumeOpen()) |
| 531 | T.skipToEnd(); |
| 532 | } |
| 533 | } |
| 534 | |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 535 | /// [MS] decl-specifier: |
| 536 | /// __declspec ( extended-decl-modifier-seq ) |
| 537 | /// |
| 538 | /// [MS] extended-decl-modifier-seq: |
| 539 | /// extended-decl-modifier[opt] |
| 540 | /// extended-decl-modifier extended-decl-modifier-seq |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 541 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) { |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 542 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | 06de2b5 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 543 | |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 544 | ConsumeToken(); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 545 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 546 | if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec", |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 547 | tok::r_paren)) |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 548 | return; |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 549 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 550 | // An empty declspec is perfectly legal and should not warn. Additionally, |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 551 | // you can specify multiple attributes per declspec. |
| 552 | while (Tok.getKind() != tok::r_paren) { |
| 553 | // We expect either a well-known identifier or a generic string. Anything |
| 554 | // else is a malformed declspec. |
| 555 | bool IsString = Tok.getKind() == tok::string_literal ? true : false; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 556 | if (!IsString && Tok.getKind() != tok::identifier && |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 557 | Tok.getKind() != tok::kw_restrict) { |
| 558 | Diag(Tok, diag::err_ms_declspec_type); |
| 559 | T.skipToEnd(); |
| 560 | return; |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 561 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 562 | |
| 563 | IdentifierInfo *AttrName; |
| 564 | SourceLocation AttrNameLoc; |
| 565 | if (IsString) { |
| 566 | SmallString<8> StrBuffer; |
| 567 | bool Invalid = false; |
| 568 | StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid); |
| 569 | if (Invalid) { |
| 570 | T.skipToEnd(); |
| 571 | return; |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 572 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 573 | AttrName = PP.getIdentifierInfo(Str); |
| 574 | AttrNameLoc = ConsumeStringToken(); |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 575 | } else { |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 576 | AttrName = Tok.getIdentifierInfo(); |
| 577 | AttrNameLoc = ConsumeToken(); |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 578 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 579 | |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 580 | if (IsString || IsSimpleMicrosoftDeclSpec(AttrName)) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 581 | // If we have a generic string, we will allow it because there is no |
| 582 | // documented list of allowable string declspecs, but we know they exist |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 583 | // (for instance, SAL declspecs in older versions of MSVC). |
| 584 | // |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 585 | // Alternatively, if the identifier is a simple one, then it requires no |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 586 | // arguments and can be turned into an attribute directly. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 587 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 588 | AttributeList::AS_Declspec); |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 589 | else |
| 590 | ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs); |
Jakob Stoklund Olesen | e1c0ae6 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 591 | } |
Aaron Ballman | 478faed | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 592 | T.consumeClose(); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 593 | } |
| 594 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 595 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 596 | // Treat these like attributes |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 597 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 598 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 599 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 600 | Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) || |
| 601 | Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) { |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 602 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 603 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 604 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 605 | AttributeList::AS_Keyword); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 606 | } |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 607 | } |
| 608 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 609 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 610 | // Treat these like attributes |
| 611 | while (Tok.is(tok::kw___pascal)) { |
| 612 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 613 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 614 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 615 | AttributeList::AS_Keyword); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 616 | } |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 619 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 620 | // Treat these like attributes |
| 621 | while (Tok.is(tok::kw___kernel)) { |
Richard Smith | 0cdcc98 | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 622 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 623 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 624 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 625 | AttributeList::AS_Keyword); |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 626 | } |
| 627 | } |
| 628 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 629 | void Parser::ParseOpenCLQualifiers(DeclSpec &DS) { |
Richard Smith | 0cdcc98 | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 630 | // FIXME: The mapping from attribute spelling to semantics should be |
| 631 | // performed in Sema, not here. |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 632 | SourceLocation Loc = Tok.getLocation(); |
| 633 | switch(Tok.getKind()) { |
| 634 | // OpenCL qualifiers: |
| 635 | case tok::kw___private: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 636 | case tok::kw_private: |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 637 | DS.getAttributes().addNewInteger( |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 638 | Actions.getASTContext(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 639 | PP.getIdentifierInfo("address_space"), Loc, 0); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 640 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 641 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 642 | case tok::kw___global: |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 643 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 644 | Actions.getASTContext(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 645 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 646 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 647 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 648 | case tok::kw___local: |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 649 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 650 | Actions.getASTContext(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 651 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 652 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 653 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 654 | case tok::kw___constant: |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 655 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 656 | Actions.getASTContext(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 657 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 658 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 659 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 660 | case tok::kw___read_only: |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 661 | DS.getAttributes().addNewInteger( |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 662 | Actions.getASTContext(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 663 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 664 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 665 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 666 | case tok::kw___write_only: |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 667 | DS.getAttributes().addNewInteger( |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 668 | Actions.getASTContext(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 669 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 670 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 671 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 672 | case tok::kw___read_write: |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 673 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 674 | Actions.getASTContext(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 675 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write); |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 676 | break; |
| 677 | default: break; |
| 678 | } |
| 679 | } |
| 680 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 681 | /// \brief Parse a version number. |
| 682 | /// |
| 683 | /// version: |
| 684 | /// simple-integer |
| 685 | /// simple-integer ',' simple-integer |
| 686 | /// simple-integer ',' simple-integer ',' simple-integer |
| 687 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 688 | Range = Tok.getLocation(); |
| 689 | |
| 690 | if (!Tok.is(tok::numeric_constant)) { |
| 691 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 692 | SkipUntil(tok::comma, tok::r_paren, |
| 693 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 694 | return VersionTuple(); |
| 695 | } |
| 696 | |
| 697 | // Parse the major (and possibly minor and subminor) versions, which |
| 698 | // are stored in the numeric constant. We utilize a quirk of the |
| 699 | // lexer, which is that it handles something like 1.2.3 as a single |
| 700 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | 2c1dd27 | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 701 | SmallString<512> Buffer; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 702 | Buffer.resize(Tok.getLength()+1); |
| 703 | const char *ThisTokBegin = &Buffer[0]; |
| 704 | |
| 705 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 706 | bool Invalid = false; |
| 707 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 708 | if (Invalid) |
| 709 | return VersionTuple(); |
| 710 | |
| 711 | // Parse the major version. |
| 712 | unsigned AfterMajor = 0; |
| 713 | unsigned Major = 0; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 714 | while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 715 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 716 | ++AfterMajor; |
| 717 | } |
| 718 | |
| 719 | if (AfterMajor == 0) { |
| 720 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 721 | SkipUntil(tok::comma, tok::r_paren, |
| 722 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 723 | return VersionTuple(); |
| 724 | } |
| 725 | |
| 726 | if (AfterMajor == ActualLength) { |
| 727 | ConsumeToken(); |
| 728 | |
| 729 | // We only had a single version component. |
| 730 | if (Major == 0) { |
| 731 | Diag(Tok, diag::err_zero_version); |
| 732 | return VersionTuple(); |
| 733 | } |
| 734 | |
| 735 | return VersionTuple(Major); |
| 736 | } |
| 737 | |
| 738 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 739 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 740 | SkipUntil(tok::comma, tok::r_paren, |
| 741 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 742 | return VersionTuple(); |
| 743 | } |
| 744 | |
| 745 | // Parse the minor version. |
| 746 | unsigned AfterMinor = AfterMajor + 1; |
| 747 | unsigned Minor = 0; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 748 | while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 749 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 750 | ++AfterMinor; |
| 751 | } |
| 752 | |
| 753 | if (AfterMinor == ActualLength) { |
| 754 | ConsumeToken(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 755 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 756 | // We had major.minor. |
| 757 | if (Major == 0 && Minor == 0) { |
| 758 | Diag(Tok, diag::err_zero_version); |
| 759 | return VersionTuple(); |
| 760 | } |
| 761 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 762 | return VersionTuple(Major, Minor); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | // If what follows is not a '.', we have a problem. |
| 766 | if (ThisTokBegin[AfterMinor] != '.') { |
| 767 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 768 | SkipUntil(tok::comma, tok::r_paren, |
| 769 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 770 | return VersionTuple(); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | // Parse the subminor version. |
| 774 | unsigned AfterSubminor = AfterMinor + 1; |
| 775 | unsigned Subminor = 0; |
Jordan Rose | a7d0384 | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 776 | while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 777 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 778 | ++AfterSubminor; |
| 779 | } |
| 780 | |
| 781 | if (AfterSubminor != ActualLength) { |
| 782 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 783 | SkipUntil(tok::comma, tok::r_paren, |
| 784 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 785 | return VersionTuple(); |
| 786 | } |
| 787 | ConsumeToken(); |
| 788 | return VersionTuple(Major, Minor, Subminor); |
| 789 | } |
| 790 | |
| 791 | /// \brief Parse the contents of the "availability" attribute. |
| 792 | /// |
| 793 | /// availability-attribute: |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 794 | /// 'availability' '(' platform ',' version-arg-list, opt-message')' |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 795 | /// |
| 796 | /// platform: |
| 797 | /// identifier |
| 798 | /// |
| 799 | /// version-arg-list: |
| 800 | /// version-arg |
| 801 | /// version-arg ',' version-arg-list |
| 802 | /// |
| 803 | /// version-arg: |
| 804 | /// 'introduced' '=' version |
| 805 | /// 'deprecated' '=' version |
Douglas Gregor | fdd417f | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 806 | /// 'obsoleted' = version |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 807 | /// 'unavailable' |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 808 | /// opt-message: |
| 809 | /// 'message' '=' <string> |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 810 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 811 | SourceLocation AvailabilityLoc, |
| 812 | ParsedAttributes &attrs, |
| 813 | SourceLocation *endLoc) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 814 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 815 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 816 | ExprResult MessageExpr; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 817 | |
| 818 | // Opening '('. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 819 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 820 | if (T.consumeOpen()) { |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 821 | Diag(Tok, diag::err_expected_lparen); |
| 822 | return; |
| 823 | } |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 824 | |
| 825 | // Parse the platform name, |
| 826 | if (Tok.isNot(tok::identifier)) { |
| 827 | Diag(Tok, diag::err_availability_expected_platform); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 828 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 829 | return; |
| 830 | } |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 831 | IdentifierLoc *Platform = ParseIdentifierLoc(); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 832 | |
| 833 | // Parse the ',' following the platform name. |
| 834 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren)) |
| 835 | return; |
| 836 | |
| 837 | // If we haven't grabbed the pointers for the identifiers |
| 838 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 839 | if (!Ident_introduced) { |
| 840 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 841 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 842 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 843 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 844 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 848 | SourceLocation UnavailableLoc; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 849 | do { |
| 850 | if (Tok.isNot(tok::identifier)) { |
| 851 | Diag(Tok, diag::err_availability_expected_change); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 852 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 853 | return; |
| 854 | } |
| 855 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 856 | SourceLocation KeywordLoc = ConsumeToken(); |
| 857 | |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 858 | if (Keyword == Ident_unavailable) { |
| 859 | if (UnavailableLoc.isValid()) { |
| 860 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 861 | << Keyword << SourceRange(UnavailableLoc); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 862 | } |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 863 | UnavailableLoc = KeywordLoc; |
| 864 | |
| 865 | if (Tok.isNot(tok::comma)) |
| 866 | break; |
| 867 | |
| 868 | ConsumeToken(); |
| 869 | continue; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 870 | } |
| 871 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 872 | if (Tok.isNot(tok::equal)) { |
| 873 | Diag(Tok, diag::err_expected_equal_after) |
| 874 | << Keyword; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 875 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 876 | return; |
| 877 | } |
| 878 | ConsumeToken(); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 879 | if (Keyword == Ident_message) { |
Benjamin Kramer | a9dfa92 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 880 | if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals. |
Andy Gibbs | a8df57a | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 881 | Diag(Tok, diag::err_expected_string_literal) |
| 882 | << /*Source='availability attribute'*/2; |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 883 | SkipUntil(tok::r_paren, StopAtSemi); |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 884 | return; |
| 885 | } |
| 886 | MessageExpr = ParseStringLiteralExpression(); |
| 887 | break; |
| 888 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 889 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 890 | SourceRange VersionRange; |
| 891 | VersionTuple Version = ParseVersionTuple(VersionRange); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 892 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 893 | if (Version.empty()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 894 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 895 | return; |
| 896 | } |
| 897 | |
| 898 | unsigned Index; |
| 899 | if (Keyword == Ident_introduced) |
| 900 | Index = Introduced; |
| 901 | else if (Keyword == Ident_deprecated) |
| 902 | Index = Deprecated; |
| 903 | else if (Keyword == Ident_obsoleted) |
| 904 | Index = Obsoleted; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 905 | else |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 906 | Index = Unknown; |
| 907 | |
| 908 | if (Index < Unknown) { |
| 909 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 910 | Diag(KeywordLoc, diag::err_availability_redundant) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 911 | << Keyword |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 912 | << SourceRange(Changes[Index].KeywordLoc, |
| 913 | Changes[Index].VersionRange.getEnd()); |
| 914 | } |
| 915 | |
| 916 | Changes[Index].KeywordLoc = KeywordLoc; |
| 917 | Changes[Index].Version = Version; |
| 918 | Changes[Index].VersionRange = VersionRange; |
| 919 | } else { |
| 920 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 921 | << Keyword << VersionRange; |
| 922 | } |
| 923 | |
| 924 | if (Tok.isNot(tok::comma)) |
| 925 | break; |
| 926 | |
| 927 | ConsumeToken(); |
| 928 | } while (true); |
| 929 | |
| 930 | // Closing ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 931 | if (T.consumeClose()) |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 932 | return; |
| 933 | |
| 934 | if (endLoc) |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 935 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 936 | |
Douglas Gregor | 7ab142b | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 937 | // The 'unavailable' availability cannot be combined with any other |
| 938 | // availability changes. Make sure that hasn't happened. |
| 939 | if (UnavailableLoc.isValid()) { |
| 940 | bool Complained = false; |
| 941 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 942 | if (Changes[Index].KeywordLoc.isValid()) { |
| 943 | if (!Complained) { |
| 944 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 945 | << SourceRange(Changes[Index].KeywordLoc, |
| 946 | Changes[Index].VersionRange.getEnd()); |
| 947 | Complained = true; |
| 948 | } |
| 949 | |
| 950 | // Clear out the availability. |
| 951 | Changes[Index] = AvailabilityChange(); |
| 952 | } |
| 953 | } |
| 954 | } |
| 955 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 956 | // Record this attribute |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 957 | attrs.addNew(&Availability, |
| 958 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Fariborz Jahanian | 586be88 | 2012-01-23 23:38:32 +0000 | [diff] [blame] | 959 | 0, AvailabilityLoc, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 960 | Platform, |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 961 | Changes[Introduced], |
| 962 | Changes[Deprecated], |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 963 | Changes[Obsoleted], |
Fariborz Jahanian | 88d510d | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 964 | UnavailableLoc, MessageExpr.take(), |
Alexis Hunt | a0e54d4 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 965 | AttributeList::AS_GNU); |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 966 | } |
| 967 | |
Fariborz Jahanian | 1a2519a | 2013-12-04 20:32:50 +0000 | [diff] [blame] | 968 | /// \brief Parse the contents of the "objc_bridge_related" attribute. |
| 969 | /// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')' |
| 970 | /// related_class: |
| 971 | /// Identifier |
| 972 | /// |
| 973 | /// opt-class_method: |
| 974 | /// Identifier: | <empty> |
| 975 | /// |
| 976 | /// opt-instance_method: |
| 977 | /// Identifier | <empty> |
| 978 | /// |
| 979 | void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated, |
| 980 | SourceLocation ObjCBridgeRelatedLoc, |
| 981 | ParsedAttributes &attrs, |
| 982 | SourceLocation *endLoc) { |
| 983 | // Opening '('. |
| 984 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 985 | if (T.consumeOpen()) { |
| 986 | Diag(Tok, diag::err_expected_lparen); |
| 987 | return; |
| 988 | } |
| 989 | |
| 990 | // Parse the related class name. |
| 991 | if (Tok.isNot(tok::identifier)) { |
| 992 | Diag(Tok, diag::err_objcbridge_related_expected_related_class); |
| 993 | SkipUntil(tok::r_paren, StopAtSemi); |
| 994 | return; |
| 995 | } |
| 996 | IdentifierLoc *RelatedClass = ParseIdentifierLoc(); |
| 997 | if (Tok.isNot(tok::comma)) { |
| 998 | Diag(Tok, diag::err_expected_comma); |
| 999 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1000 | return; |
| 1001 | } |
| 1002 | ConsumeToken(); |
| 1003 | |
| 1004 | // Parse optional class method name. |
| 1005 | IdentifierLoc *ClassMethod = 0; |
| 1006 | if (Tok.is(tok::identifier)) { |
| 1007 | ClassMethod = ParseIdentifierLoc(); |
| 1008 | if (Tok.isNot(tok::colon)) { |
| 1009 | Diag(Tok, diag::err_objcbridge_related_selector_name); |
| 1010 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1011 | return; |
| 1012 | } |
| 1013 | ConsumeToken(); |
| 1014 | } |
| 1015 | if (Tok.isNot(tok::comma)) { |
| 1016 | if (Tok.is(tok::colon)) |
| 1017 | Diag(Tok, diag::err_objcbridge_related_selector_name); |
| 1018 | else |
| 1019 | Diag(Tok, diag::err_expected_comma); |
| 1020 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1021 | return; |
| 1022 | } |
| 1023 | ConsumeToken(); |
| 1024 | |
| 1025 | // Parse optional instance method name. |
| 1026 | IdentifierLoc *InstanceMethod = 0; |
| 1027 | if (Tok.is(tok::identifier)) |
| 1028 | InstanceMethod = ParseIdentifierLoc(); |
| 1029 | else if (Tok.isNot(tok::r_paren)) { |
| 1030 | Diag(Tok, diag::err_expected_rparen); |
| 1031 | SkipUntil(tok::r_paren, StopAtSemi); |
| 1032 | return; |
| 1033 | } |
| 1034 | |
| 1035 | // Closing ')'. |
| 1036 | if (T.consumeClose()) |
| 1037 | return; |
| 1038 | |
| 1039 | if (endLoc) |
| 1040 | *endLoc = T.getCloseLocation(); |
| 1041 | |
| 1042 | // Record this attribute |
| 1043 | attrs.addNew(&ObjCBridgeRelated, |
| 1044 | SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()), |
| 1045 | 0, ObjCBridgeRelatedLoc, |
| 1046 | RelatedClass, |
| 1047 | ClassMethod, |
| 1048 | InstanceMethod, |
| 1049 | AttributeList::AS_GNU); |
| 1050 | |
| 1051 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1052 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 1053 | // Late Parsed Attributes: |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1054 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 1055 | |
| 1056 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 1057 | |
| 1058 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 1059 | Self->ParseLexedAttributes(*Class); |
| 1060 | } |
| 1061 | |
| 1062 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1063 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 1067 | /// scope appropriately. |
| 1068 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 1069 | // Deal with templates |
| 1070 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 1071 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 1072 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 1073 | HasTemplateScope); |
| 1074 | if (HasTemplateScope) |
| 1075 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 1076 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1077 | // Set or update the scope flags. |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1078 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1079 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1080 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 1081 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 1082 | |
DeLesley Hutchins | 6f86004 | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1083 | // Enter the scope of nested classes |
| 1084 | if (!AlreadyHasClassScope) |
| 1085 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 1086 | Class.TagOrTemplate); |
Benjamin Kramer | 1d373c6 | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 1087 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1088 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 1089 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 1090 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1091 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1092 | |
DeLesley Hutchins | 6f86004 | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1093 | if (!AlreadyHasClassScope) |
| 1094 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 1095 | Class.TagOrTemplate); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1096 | } |
| 1097 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1098 | |
| 1099 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 1100 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 1101 | bool EnterScope, bool OnDefinition) { |
DeLesley Hutchins | 66e300e | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1102 | assert(LAs.parseSoon() && |
| 1103 | "Attribute list should be marked for immediate parsing."); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1104 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 19c722d | 2012-08-15 22:41:04 +0000 | [diff] [blame] | 1105 | if (D) |
| 1106 | LAs[i]->addDecl(D); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1107 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | bafc49a | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 1108 | delete LAs[i]; |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1109 | } |
| 1110 | LAs.clear(); |
| 1111 | } |
| 1112 | |
| 1113 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1114 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 1115 | /// This will be called at the end of parsing a class declaration |
| 1116 | /// for each LateParsedAttribute. We consume the saved tokens and |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1117 | /// create an attribute with the arguments filled in. We add this |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1118 | /// to the Attribute list for the decl. |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1119 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 1120 | bool EnterScope, bool OnDefinition) { |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1121 | // Save the current token position. |
| 1122 | SourceLocation OrigLoc = Tok.getLocation(); |
| 1123 | |
| 1124 | // Append the current token at the end of the new token stream so that it |
| 1125 | // doesn't get lost. |
| 1126 | LA.Toks.push_back(Tok); |
| 1127 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 1128 | // Consume the previously pushed token. |
Argyrios Kyrtzidis | c36633c | 2013-03-27 23:58:17 +0000 | [diff] [blame] | 1129 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1130 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1131 | if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) { |
Richard Smith | 10876ef | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1132 | // FIXME: Do not warn on C++11 attributes, once we start supporting |
| 1133 | // them here. |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1134 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 1135 | << LA.AttrName.getName(); |
| 1136 | } |
| 1137 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1138 | ParsedAttributes Attrs(AttrFactory); |
| 1139 | SourceLocation endLoc; |
| 1140 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1141 | if (LA.Decls.size() > 0) { |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1142 | Decl *D = LA.Decls[0]; |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1143 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1144 | RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1145 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1146 | // Allow 'this' within late-parsed attributes. |
Richard Smith | c3d2ebb | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 1147 | Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0, |
| 1148 | ND && ND->isCXXInstanceMember()); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1149 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1150 | if (LA.Decls.size() == 1) { |
| 1151 | // If the Decl is templatized, add template parameters to scope. |
| 1152 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 1153 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 1154 | if (HasTemplateScope) |
| 1155 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1156 | |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1157 | // If the Decl is on a function, add function parameters to the scope. |
| 1158 | bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
| 1159 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope); |
| 1160 | if (HasFunScope) |
| 1161 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1162 | |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1163 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Michael Han | 360d225 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 1164 | 0, SourceLocation(), AttributeList::AS_GNU); |
DeLesley Hutchins | f1150d3 | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1165 | |
| 1166 | if (HasFunScope) { |
| 1167 | Actions.ActOnExitFunctionContext(); |
| 1168 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 1169 | } |
| 1170 | if (HasTemplateScope) { |
| 1171 | TempScope.Exit(); |
| 1172 | } |
| 1173 | } else { |
| 1174 | // If there are multiple decls, then the decl cannot be within the |
| 1175 | // function scope. |
Michael Han | 23214e5 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1176 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Michael Han | 360d225 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 1177 | 0, SourceLocation(), AttributeList::AS_GNU); |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1178 | } |
DeLesley Hutchins | 71d6103 | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 1179 | } else { |
| 1180 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | 990d571 | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1183 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) { |
| 1184 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
| 1185 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1186 | |
| 1187 | if (Tok.getLocation() != OrigLoc) { |
| 1188 | // Due to a parsing error, we either went over the cached tokens or |
| 1189 | // there are still cached tokens left, so we skip the leftover tokens. |
| 1190 | // Since this is an uncommon situation that should be avoided, use the |
| 1191 | // expensive isBeforeInTranslationUnit call. |
| 1192 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 1193 | OrigLoc)) |
| 1194 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
Douglas Gregor | 0cf55e9 | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 1195 | ConsumeAnyToken(); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1196 | } |
| 1197 | } |
| 1198 | |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1199 | /// \brief Wrapper around a case statement checking if AttrName is |
| 1200 | /// one of the thread safety attributes |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1201 | bool Parser::IsThreadSafetyAttribute(StringRef AttrName) { |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1202 | return llvm::StringSwitch<bool>(AttrName) |
| 1203 | .Case("guarded_by", true) |
| 1204 | .Case("guarded_var", true) |
| 1205 | .Case("pt_guarded_by", true) |
| 1206 | .Case("pt_guarded_var", true) |
| 1207 | .Case("lockable", true) |
| 1208 | .Case("scoped_lockable", true) |
| 1209 | .Case("no_thread_safety_analysis", true) |
| 1210 | .Case("acquired_after", true) |
| 1211 | .Case("acquired_before", true) |
| 1212 | .Case("exclusive_lock_function", true) |
| 1213 | .Case("shared_lock_function", true) |
| 1214 | .Case("exclusive_trylock_function", true) |
| 1215 | .Case("shared_trylock_function", true) |
| 1216 | .Case("unlock_function", true) |
| 1217 | .Case("lock_returned", true) |
| 1218 | .Case("locks_excluded", true) |
| 1219 | .Case("exclusive_locks_required", true) |
| 1220 | .Case("shared_locks_required", true) |
| 1221 | .Default(false); |
| 1222 | } |
| 1223 | |
| 1224 | /// \brief Parse the contents of thread safety attributes. These |
| 1225 | /// should always be parsed as an expression list. |
| 1226 | /// |
| 1227 | /// We need to special case the parsing due to the fact that if the first token |
| 1228 | /// of the first argument is an identifier, the main parse loop will store |
| 1229 | /// that token as a "parameter" and the rest of |
| 1230 | /// the arguments will be added to a list of "arguments". However, |
| 1231 | /// subsequent tokens in the first argument are lost. We instead parse each |
| 1232 | /// argument as an expression and add all arguments to the list of "arguments". |
| 1233 | /// In future, we will take advantage of this special case to also |
| 1234 | /// deal with some argument scoping issues here (for example, referring to a |
| 1235 | /// function parameter in the attribute on that function). |
| 1236 | void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName, |
| 1237 | SourceLocation AttrNameLoc, |
| 1238 | ParsedAttributes &Attrs, |
| 1239 | SourceLocation *EndLoc) { |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1240 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1241 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1242 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1243 | T.consumeOpen(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1244 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1245 | ArgsVector ArgExprs; |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1246 | bool ArgExprsOk = true; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1247 | |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1248 | // now parse the list of expressions |
DeLesley Hutchins | 36f5d85 | 2011-12-14 19:36:06 +0000 | [diff] [blame] | 1249 | while (Tok.isNot(tok::r_paren)) { |
DeLesley Hutchins | eb849c6 | 2013-02-07 19:01:07 +0000 | [diff] [blame] | 1250 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1251 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 1252 | if (ArgExpr.isInvalid()) { |
| 1253 | ArgExprsOk = false; |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1254 | T.consumeClose(); |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1255 | break; |
| 1256 | } else { |
| 1257 | ArgExprs.push_back(ArgExpr.release()); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1258 | } |
Caitlin Sadowski | 9385dd7 | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1259 | if (Tok.isNot(tok::comma)) |
| 1260 | break; |
| 1261 | ConsumeToken(); // Eat the comma, move to the next argument |
| 1262 | } |
| 1263 | // Match the ')'. |
DeLesley Hutchins | 30398dd | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 1264 | if (ArgExprsOk && !T.consumeClose()) { |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1265 | Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, ArgExprs.data(), |
| 1266 | ArgExprs.size(), AttributeList::AS_GNU); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1267 | } |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1268 | if (EndLoc) |
| 1269 | *EndLoc = T.getCloseLocation(); |
Caitlin Sadowski | 4b1e839 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1270 | } |
| 1271 | |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1272 | void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName, |
| 1273 | SourceLocation AttrNameLoc, |
| 1274 | ParsedAttributes &Attrs, |
| 1275 | SourceLocation *EndLoc) { |
| 1276 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 1277 | |
| 1278 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1279 | T.consumeOpen(); |
| 1280 | |
| 1281 | if (Tok.isNot(tok::identifier)) { |
| 1282 | Diag(Tok, diag::err_expected_ident); |
| 1283 | T.skipToEnd(); |
| 1284 | return; |
| 1285 | } |
Richard Smith | feefaf5 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 1286 | IdentifierLoc *ArgumentKind = ParseIdentifierLoc(); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1287 | |
| 1288 | if (Tok.isNot(tok::comma)) { |
| 1289 | Diag(Tok, diag::err_expected_comma); |
| 1290 | T.skipToEnd(); |
| 1291 | return; |
| 1292 | } |
| 1293 | ConsumeToken(); |
| 1294 | |
| 1295 | SourceRange MatchingCTypeRange; |
| 1296 | TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange); |
| 1297 | if (MatchingCType.isInvalid()) { |
| 1298 | T.skipToEnd(); |
| 1299 | return; |
| 1300 | } |
| 1301 | |
| 1302 | bool LayoutCompatible = false; |
| 1303 | bool MustBeNull = false; |
| 1304 | while (Tok.is(tok::comma)) { |
| 1305 | ConsumeToken(); |
| 1306 | if (Tok.isNot(tok::identifier)) { |
| 1307 | Diag(Tok, diag::err_expected_ident); |
| 1308 | T.skipToEnd(); |
| 1309 | return; |
| 1310 | } |
| 1311 | IdentifierInfo *Flag = Tok.getIdentifierInfo(); |
| 1312 | if (Flag->isStr("layout_compatible")) |
| 1313 | LayoutCompatible = true; |
| 1314 | else if (Flag->isStr("must_be_null")) |
| 1315 | MustBeNull = true; |
| 1316 | else { |
| 1317 | Diag(Tok, diag::err_type_safety_unknown_flag) << Flag; |
| 1318 | T.skipToEnd(); |
| 1319 | return; |
| 1320 | } |
| 1321 | ConsumeToken(); // consume flag |
| 1322 | } |
| 1323 | |
| 1324 | if (!T.consumeClose()) { |
| 1325 | Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc, |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1326 | ArgumentKind, MatchingCType.release(), |
| 1327 | LayoutCompatible, MustBeNull, |
| 1328 | AttributeList::AS_GNU); |
Dmitri Gribenko | e4a5a90 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | if (EndLoc) |
| 1332 | *EndLoc = T.getCloseLocation(); |
| 1333 | } |
| 1334 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1335 | /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets |
| 1336 | /// of a C++11 attribute-specifier in a location where an attribute is not |
| 1337 | /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this |
| 1338 | /// situation. |
| 1339 | /// |
| 1340 | /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if |
| 1341 | /// this doesn't appear to actually be an attribute-specifier, and the caller |
| 1342 | /// should try to parse it. |
| 1343 | bool Parser::DiagnoseProhibitedCXX11Attribute() { |
| 1344 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)); |
| 1345 | |
| 1346 | switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) { |
| 1347 | case CAK_NotAttributeSpecifier: |
| 1348 | // No diagnostic: we're in Obj-C++11 and this is not actually an attribute. |
| 1349 | return false; |
| 1350 | |
| 1351 | case CAK_InvalidAttributeSpecifier: |
| 1352 | Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute); |
| 1353 | return false; |
| 1354 | |
| 1355 | case CAK_AttributeSpecifier: |
| 1356 | // Parse and discard the attributes. |
| 1357 | SourceLocation BeginLoc = ConsumeBracket(); |
| 1358 | ConsumeBracket(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1359 | SkipUntil(tok::r_square); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1360 | assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied"); |
| 1361 | SourceLocation EndLoc = ConsumeBracket(); |
| 1362 | Diag(BeginLoc, diag::err_attributes_not_allowed) |
| 1363 | << SourceRange(BeginLoc, EndLoc); |
| 1364 | return true; |
| 1365 | } |
Chandler Carruth | d8f7d38 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 1366 | llvm_unreachable("All cases handled above."); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
Richard Smith | 98155ad | 2013-02-20 01:17:14 +0000 | [diff] [blame] | 1369 | /// \brief We have found the opening square brackets of a C++11 |
| 1370 | /// attribute-specifier in a location where an attribute is not permitted, but |
| 1371 | /// we know where the attributes ought to be written. Parse them anyway, and |
| 1372 | /// provide a fixit moving them to the right place. |
Richard Smith | 4c96e99 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1373 | void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs, |
| 1374 | SourceLocation CorrectLocation) { |
| 1375 | assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) || |
| 1376 | Tok.is(tok::kw_alignas)); |
| 1377 | |
| 1378 | // Consume the attributes. |
| 1379 | SourceLocation Loc = Tok.getLocation(); |
| 1380 | ParseCXX11Attributes(Attrs); |
| 1381 | CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true); |
| 1382 | |
| 1383 | Diag(Loc, diag::err_attributes_not_allowed) |
| 1384 | << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange) |
| 1385 | << FixItHint::CreateRemoval(AttrRange); |
| 1386 | } |
| 1387 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1388 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 1389 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 1390 | << attrs.Range; |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1393 | void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) { |
| 1394 | AttributeList *AttrList = attrs.getList(); |
| 1395 | while (AttrList) { |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1396 | if (AttrList->isCXX11Attribute()) { |
Richard Smith | 810ad3e | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 1397 | Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr) |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1398 | << AttrList->getName(); |
| 1399 | AttrList->setInvalid(); |
| 1400 | } |
| 1401 | AttrList = AttrList->getNext(); |
| 1402 | } |
| 1403 | } |
| 1404 | |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1405 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 1406 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1407 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 1408 | /// location of the semicolon in DeclEnd. |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1409 | /// |
| 1410 | /// declaration: [C99 6.7] |
| 1411 | /// block-declaration -> |
| 1412 | /// simple-declaration |
| 1413 | /// others [FIXME] |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1414 | /// [C++] template-declaration |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1415 | /// [C++] namespace-definition |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1416 | /// [C++] using-directive |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1417 | /// [C++] using-declaration |
Richard Smith | c202b28 | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1418 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1419 | /// others... [FIXME] |
| 1420 | /// |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1421 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 1422 | unsigned Context, |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1423 | SourceLocation &DeclEnd, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1424 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 355094e | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 1425 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | 59b7528 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 1426 | // Must temporarily exit the objective-c container scope for |
| 1427 | // parsing c none objective-c decls. |
| 1428 | ObjCDeclContextSwitch ObjCDC(*this); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1429 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1430 | Decl *SingleDecl = 0; |
Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1431 | Decl *OwnedType = 0; |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1432 | switch (Tok.getKind()) { |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1433 | case tok::kw_template: |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1434 | case tok::kw_export: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1435 | ProhibitAttributes(attrs); |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1436 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1437 | break; |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1438 | case tok::kw_inline: |
Sebastian Redl | 5a5f2c7 | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 1439 | // Could be the start of an inline namespace. Allowed as an ext in C++03. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1440 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1441 | ProhibitAttributes(attrs); |
Sebastian Redl | 6766794 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1442 | SourceLocation InlineLoc = ConsumeToken(); |
| 1443 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 1444 | break; |
| 1445 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1446 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | 1db5c94 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1447 | true); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1448 | case tok::kw_namespace: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1449 | ProhibitAttributes(attrs); |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1450 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1451 | break; |
Douglas Gregor | d7c4d98 | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1452 | case tok::kw_using: |
John McCall | 9b72f89 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1453 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1454 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1455 | break; |
Anders Carlsson | f24fcff6 | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1456 | case tok::kw_static_assert: |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1457 | case tok::kw__Static_assert: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1458 | ProhibitAttributes(attrs); |
Chris Lattner | 49836b4 | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1459 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1460 | break; |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1461 | default: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1462 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1463 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1464 | |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1465 | // This routine returns a DeclGroup, if the thing we parsed only contains a |
Richard Smith | cd1c055 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1466 | // single decl, convert it now. Alias declarations can also declare a type; |
| 1467 | // include that too if it is present. |
| 1468 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1472 | /// declaration-specifiers init-declarator-list[opt] ';' |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1473 | /// [C++11] attribute-specifier-seq decl-specifier-seq[opt] |
| 1474 | /// init-declarator-list ';' |
Chris Lattner | a523517 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1475 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 1476 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1477 | /// |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1478 | /// for-range-declaration: [C++11 6.5p1: stmt.ranged] |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1479 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1480 | /// |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1481 | /// If RequireSemi is false, this does not check for a ';' at the end of the |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1482 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1483 | /// |
| 1484 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 1485 | /// of a simple-declaration. If we find that we are, we also parse the |
| 1486 | /// for-range-initializer, and place it here. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1487 | Parser::DeclGroupPtrTy |
| 1488 | Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context, |
| 1489 | SourceLocation &DeclEnd, |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1490 | ParsedAttributesWithRange &Attrs, |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1491 | bool RequireSemi, ForRangeInit *FRI) { |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1492 | // Parse the common declaration-specifiers piece. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1493 | ParsingDeclSpec DS(*this); |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1494 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 1495 | DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context); |
| 1496 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext); |
| 1497 | |
| 1498 | // If we had a free-standing type definition with a missing semicolon, we |
| 1499 | // may get this far before the problem becomes obvious. |
| 1500 | if (DS.hasTagDefinition() && |
| 1501 | DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext)) |
| 1502 | return DeclGroupPtrTy(); |
Abramo Bagnara | 1cd8368 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1503 | |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 1504 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1505 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1506 | if (Tok.is(tok::semi)) { |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1507 | ProhibitAttributes(Attrs); |
Argyrios Kyrtzidis | fbb2bb5 | 2012-05-16 23:49:15 +0000 | [diff] [blame] | 1508 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1509 | if (RequireSemi) ConsumeToken(); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1510 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1511 | DS); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1512 | DS.complete(TheDecl); |
Chris Lattner | 5bbb3c8 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1513 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 0e89462 | 2006-08-13 19:58:17 +0000 | [diff] [blame] | 1514 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1515 | |
Richard Smith | 2386c8b | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1516 | DS.takeAttributesFrom(Attrs); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1517 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1518 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1519 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1520 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1521 | /// for a declarator. |
| 1522 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1523 | switch (Tok.getKind()) { |
| 1524 | case tok::annot_cxxscope: |
| 1525 | case tok::annot_template_id: |
| 1526 | case tok::caret: |
| 1527 | case tok::code_completion: |
| 1528 | case tok::coloncolon: |
| 1529 | case tok::ellipsis: |
| 1530 | case tok::kw___attribute: |
| 1531 | case tok::kw_operator: |
| 1532 | case tok::l_paren: |
| 1533 | case tok::star: |
| 1534 | return true; |
| 1535 | |
| 1536 | case tok::amp: |
| 1537 | case tok::ampamp: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1538 | return getLangOpts().CPlusPlus; |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1539 | |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1540 | case tok::l_square: // Might be an attribute on an unnamed bit-field. |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1541 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 && |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1542 | NextToken().is(tok::l_square); |
| 1543 | |
| 1544 | case tok::colon: // Might be a typo for '::' or an unnamed bit-field. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1545 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1546 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1547 | case tok::identifier: |
| 1548 | switch (NextToken().getKind()) { |
| 1549 | case tok::code_completion: |
| 1550 | case tok::coloncolon: |
| 1551 | case tok::comma: |
| 1552 | case tok::equal: |
| 1553 | case tok::equalequal: // Might be a typo for '='. |
| 1554 | case tok::kw_alignas: |
| 1555 | case tok::kw_asm: |
| 1556 | case tok::kw___attribute: |
| 1557 | case tok::l_brace: |
| 1558 | case tok::l_paren: |
| 1559 | case tok::l_square: |
| 1560 | case tok::less: |
| 1561 | case tok::r_brace: |
| 1562 | case tok::r_paren: |
| 1563 | case tok::r_square: |
| 1564 | case tok::semi: |
| 1565 | return true; |
| 1566 | |
| 1567 | case tok::colon: |
| 1568 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1569 | // and in block scope it's probably a label. Inside a class definition, |
| 1570 | // this is a bit-field. |
| 1571 | return Context == Declarator::MemberContext || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1572 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | c8a7903 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1573 | |
| 1574 | case tok::identifier: // Possible virt-specifier. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1575 | return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken()); |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1576 | |
| 1577 | default: |
| 1578 | return false; |
| 1579 | } |
| 1580 | |
| 1581 | default: |
| 1582 | return false; |
| 1583 | } |
| 1584 | } |
| 1585 | |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1586 | /// Skip until we reach something which seems like a sensible place to pick |
| 1587 | /// up parsing after a malformed declaration. This will sometimes stop sooner |
| 1588 | /// than SkipUntil(tok::r_brace) would, but will never stop later. |
| 1589 | void Parser::SkipMalformedDecl() { |
| 1590 | while (true) { |
| 1591 | switch (Tok.getKind()) { |
| 1592 | case tok::l_brace: |
| 1593 | // Skip until matching }, then stop. We've probably skipped over |
| 1594 | // a malformed class or function definition or similar. |
| 1595 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1596 | SkipUntil(tok::r_brace); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1597 | if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) { |
| 1598 | // This declaration isn't over yet. Keep skipping. |
| 1599 | continue; |
| 1600 | } |
| 1601 | if (Tok.is(tok::semi)) |
| 1602 | ConsumeToken(); |
| 1603 | return; |
| 1604 | |
| 1605 | case tok::l_square: |
| 1606 | ConsumeBracket(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1607 | SkipUntil(tok::r_square); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1608 | continue; |
| 1609 | |
| 1610 | case tok::l_paren: |
| 1611 | ConsumeParen(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1612 | SkipUntil(tok::r_paren); |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1613 | continue; |
| 1614 | |
| 1615 | case tok::r_brace: |
| 1616 | return; |
| 1617 | |
| 1618 | case tok::semi: |
| 1619 | ConsumeToken(); |
| 1620 | return; |
| 1621 | |
| 1622 | case tok::kw_inline: |
| 1623 | // 'inline namespace' at the start of a line is almost certainly |
Jordan Rose | 12e730c | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1624 | // a good place to pick back up parsing, except in an Objective-C |
| 1625 | // @interface context. |
| 1626 | if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) && |
| 1627 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1628 | return; |
| 1629 | break; |
| 1630 | |
| 1631 | case tok::kw_namespace: |
| 1632 | // 'namespace' at the start of a line is almost certainly a good |
Jordan Rose | 12e730c | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1633 | // place to pick back up parsing, except in an Objective-C |
| 1634 | // @interface context. |
| 1635 | if (Tok.isAtStartOfLine() && |
| 1636 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
| 1637 | return; |
| 1638 | break; |
| 1639 | |
| 1640 | case tok::at: |
| 1641 | // @end is very much like } in Objective-C contexts. |
| 1642 | if (NextToken().isObjCAtKeyword(tok::objc_end) && |
| 1643 | ParsingInObjCContainer) |
| 1644 | return; |
| 1645 | break; |
| 1646 | |
| 1647 | case tok::minus: |
| 1648 | case tok::plus: |
| 1649 | // - and + probably start new method declarations in Objective-C contexts. |
| 1650 | if (Tok.isAtStartOfLine() && ParsingInObjCContainer) |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1651 | return; |
| 1652 | break; |
| 1653 | |
| 1654 | case tok::eof: |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 1655 | case tok::annot_module_begin: |
| 1656 | case tok::annot_module_end: |
| 1657 | case tok::annot_module_include: |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1658 | return; |
| 1659 | |
| 1660 | default: |
| 1661 | break; |
| 1662 | } |
| 1663 | |
| 1664 | ConsumeAnyToken(); |
| 1665 | } |
| 1666 | } |
| 1667 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1668 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1669 | /// definition or a group of object declarations, actually parse the |
| 1670 | /// result. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1671 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1672 | unsigned Context, |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1673 | bool AllowFunctionDefinitions, |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1674 | SourceLocation *DeclEnd, |
| 1675 | ForRangeInit *FRI) { |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1676 | // Parse the first declarator. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1677 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1678 | ParseDeclarator(D); |
Chris Lattner | 32dc41c | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1679 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1680 | // Bail out if the first declarator didn't seem well-formed. |
| 1681 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | b8caac8 | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1682 | SkipMalformedDecl(); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1683 | return DeclGroupPtrTy(); |
Chris Lattner | efb0f11 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1684 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1685 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1686 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1687 | // appropriate function scope after the function Decl has been constructed. |
DeLesley Hutchins | 66e300e | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1688 | // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList. |
| 1689 | LateParsedAttrList LateParsedAttrs(true); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1690 | if (D.isFunctionDeclarator()) |
| 1691 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1692 | |
Chris Lattner | dbb1e93 | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1693 | // Check to see if we have a function *definition* which must have a body. |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1694 | if (D.isFunctionDeclarator() && |
Chris Lattner | dbb1e93 | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1695 | // Look at the next token to make sure that this isn't a function |
| 1696 | // declaration. We have to check this because __attribute__ might be the |
| 1697 | // start of a function definition in GCC-extended K&R C. |
Fariborz Jahanian | 712bb81 | 2012-08-10 15:54:40 +0000 | [diff] [blame] | 1698 | !isDeclarationAfterDeclarator()) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1699 | |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1700 | if (AllowFunctionDefinitions) { |
| 1701 | if (isStartOfFunctionDefinition(D)) { |
| 1702 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1703 | Diag(Tok, diag::err_function_declared_typedef); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1704 | |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1705 | // Recover by treating the 'typedef' as spurious. |
| 1706 | DS.ClearStorageClassSpecs(); |
| 1707 | } |
| 1708 | |
| 1709 | Decl *TheDecl = |
| 1710 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
| 1711 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1714 | if (isDeclarationSpecifier()) { |
| 1715 | // If there is an invalid declaration specifier right after the function |
| 1716 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1717 | // actually a body. Just fall through into the code that handles it as a |
| 1718 | // prototype, and let the top-level code handle the erroneous declspec |
| 1719 | // where it would otherwise expect a comma or semicolon. |
| 1720 | } else { |
| 1721 | Diag(Tok, diag::err_expected_fn_body); |
| 1722 | SkipUntil(tok::semi); |
| 1723 | return DeclGroupPtrTy(); |
| 1724 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1725 | } else { |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1726 | if (Tok.is(tok::l_brace)) { |
| 1727 | Diag(Tok, diag::err_function_definition_not_allowed); |
Serge Pavlov | 1de5151 | 2013-12-09 05:25:47 +0000 | [diff] [blame^] | 1728 | SkipMalformedDecl(); |
| 1729 | return DeclGroupPtrTy(); |
Douglas Gregor | 012efe2 | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1730 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1731 | } |
| 1732 | } |
| 1733 | |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1734 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1735 | return DeclGroupPtrTy(); |
| 1736 | |
| 1737 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1738 | // must parse and analyze the for-range-initializer before the declaration is |
| 1739 | // analyzed. |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1740 | // |
| 1741 | // Handle the Objective-C for-in loop variable similarly, although we |
| 1742 | // don't need to parse the container in advance. |
| 1743 | if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) { |
| 1744 | bool IsForRangeLoop = false; |
| 1745 | if (Tok.is(tok::colon)) { |
| 1746 | IsForRangeLoop = true; |
| 1747 | FRI->ColonLoc = ConsumeToken(); |
| 1748 | if (Tok.is(tok::l_brace)) |
| 1749 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1750 | else |
| 1751 | FRI->RangeExpr = ParseExpression(); |
| 1752 | } |
| 1753 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1754 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | 2eb1c57 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1755 | if (IsForRangeLoop) |
| 1756 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1757 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | cf6e0c8 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 1758 | D.complete(ThisDecl); |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1759 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1760 | } |
| 1761 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1762 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1763 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1764 | if (LateParsedAttrs.size() > 0) |
| 1765 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1766 | D.complete(FirstDecl); |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1767 | if (FirstDecl) |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1768 | DeclsInGroup.push_back(FirstDecl); |
| 1769 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1770 | bool ExpectSemi = Context != Declarator::ForContext; |
Fariborz Jahanian | 577574a | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 1771 | |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1772 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1773 | // error, bail out. |
| 1774 | while (Tok.is(tok::comma)) { |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1775 | SourceLocation CommaLoc = ConsumeToken(); |
| 1776 | |
| 1777 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1778 | // This comma was followed by a line-break and something which can't be |
| 1779 | // the start of a declarator. The comma was probably a typo for a |
| 1780 | // semicolon. |
| 1781 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1782 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1783 | ExpectSemi = false; |
| 1784 | break; |
| 1785 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1786 | |
| 1787 | // Parse the next declarator. |
| 1788 | D.clear(); |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1789 | D.setCommaLoc(CommaLoc); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1790 | |
| 1791 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1792 | // declaration, these would be part of the declspec. In subsequent |
| 1793 | // declarators, they become part of the declarator itself, so that they |
| 1794 | // don't apply to declarators after *this* one. Examples: |
| 1795 | // short __attribute__((common)) var; -> declspec |
| 1796 | // short var __attribute__((common)); -> declarator |
| 1797 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1798 | MaybeParseGNUAttributes(D); |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1799 | |
| 1800 | ParseDeclarator(D); |
Fariborz Jahanian | 372030b | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1801 | if (!D.isInvalidType()) { |
| 1802 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1803 | D.complete(ThisDecl); |
| 1804 | if (ThisDecl) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1805 | DeclsInGroup.push_back(ThisDecl); |
Fariborz Jahanian | 372030b | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1806 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | if (DeclEnd) |
| 1810 | *DeclEnd = Tok.getLocation(); |
| 1811 | |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1812 | if (ExpectSemi && |
Chris Lattner | 02f1b61 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 1813 | ExpectAndConsumeSemi(Context == Declarator::FileContext |
| 1814 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1815 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 1390134 | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1816 | // Okay, there was no semicolon and one was expected. If we see a |
| 1817 | // declaration specifier, just assume it was missing and continue parsing. |
| 1818 | // Otherwise things are very confused and we skip to recover. |
| 1819 | if (!isDeclarationSpecifier()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1820 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Chris Lattner | 1390134 | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1821 | if (Tok.is(tok::semi)) |
| 1822 | ConsumeToken(); |
| 1823 | } |
John McCall | d5a3632 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
Rafael Espindola | ab41769 | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1826 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1829 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1830 | /// declarator. Returns true on an error. |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1831 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1832 | // If a simple-asm-expr is present, parse it. |
| 1833 | if (Tok.is(tok::kw_asm)) { |
| 1834 | SourceLocation Loc; |
| 1835 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1836 | if (AsmLabel.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1837 | SkipUntil(tok::semi, StopBeforeMatch); |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1838 | return true; |
| 1839 | } |
| 1840 | |
| 1841 | D.setAsmLabel(AsmLabel.release()); |
| 1842 | D.SetRangeEnd(Loc); |
| 1843 | } |
| 1844 | |
| 1845 | MaybeParseGNUAttributes(D); |
| 1846 | return false; |
| 1847 | } |
| 1848 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1849 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1850 | /// declarator'. This method parses the remainder of the declaration |
| 1851 | /// (including any attributes or initializer, among other things) and |
| 1852 | /// finalizes the declaration. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 1853 | /// |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 1854 | /// init-declarator: [C99 6.7] |
| 1855 | /// declarator |
| 1856 | /// declarator '=' initializer |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 1857 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1858 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1859 | /// [C++] declarator initializer[opt] |
| 1860 | /// |
| 1861 | /// [C++] initializer: |
| 1862 | /// [C++] '=' initializer-clause |
| 1863 | /// [C++] '(' expression-list ')' |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1864 | /// [C++0x] '=' 'default' [TODO] |
| 1865 | /// [C++0x] '=' 'delete' |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1866 | /// [C++0x] braced-init-list |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1867 | /// |
| 1868 | /// According to the standard grammar, =default and =delete are function |
| 1869 | /// definitions, but that definitely doesn't fit with the parser here. |
Chris Lattner | f0f3baa | 2006-08-14 00:15:20 +0000 | [diff] [blame] | 1870 | /// |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1871 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | b52fabb | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1872 | const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | 3fc6e4a | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1873 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1874 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1875 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1876 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1877 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1878 | |
Richard Smith | 02e85f3 | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1879 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1880 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1881 | // Inform the current actions module that we just parsed this declarator. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1882 | Decl *ThisDecl = 0; |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1883 | switch (TemplateInfo.Kind) { |
| 1884 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1885 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1886 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1887 | |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1888 | case ParsedTemplateInfo::Template: |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1889 | case ParsedTemplateInfo::ExplicitSpecialization: { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1890 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
Benjamin Kramer | cc4c49d | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1891 | *TemplateInfo.TemplateParams, |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1892 | D); |
Larisse Voufo | 833b05a | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 1893 | if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl)) |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1894 | // Re-direct this decl to refer to the templated decl so that we can |
| 1895 | // initialize it. |
| 1896 | ThisDecl = VT->getTemplatedDecl(); |
| 1897 | break; |
| 1898 | } |
| 1899 | case ParsedTemplateInfo::ExplicitInstantiation: { |
| 1900 | if (Tok.is(tok::semi)) { |
| 1901 | DeclResult ThisRes = Actions.ActOnExplicitInstantiation( |
| 1902 | getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D); |
| 1903 | if (ThisRes.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1904 | SkipUntil(tok::semi, StopBeforeMatch); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1905 | return 0; |
| 1906 | } |
| 1907 | ThisDecl = ThisRes.get(); |
| 1908 | } else { |
| 1909 | // FIXME: This check should be for a variable template instantiation only. |
| 1910 | |
| 1911 | // Check that this is a valid instantiation |
| 1912 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 1913 | // If the declarator-id is not a template-id, issue a diagnostic and |
| 1914 | // recover by ignoring the 'template' keyword. |
| 1915 | Diag(Tok, diag::err_template_defn_explicit_instantiation) |
| 1916 | << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc); |
| 1917 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1918 | } else { |
| 1919 | SourceLocation LAngleLoc = |
| 1920 | PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
| 1921 | Diag(D.getIdentifierLoc(), |
| 1922 | diag::err_explicit_instantiation_with_definition) |
| 1923 | << SourceRange(TemplateInfo.TemplateLoc) |
| 1924 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
| 1925 | |
| 1926 | // Recover as if it were an explicit specialization. |
| 1927 | TemplateParameterLists FakedParamLists; |
| 1928 | FakedParamLists.push_back(Actions.ActOnTemplateParameterList( |
| 1929 | 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0, |
| 1930 | LAngleLoc)); |
| 1931 | |
| 1932 | ThisDecl = |
| 1933 | Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D); |
| 1934 | } |
| 1935 | } |
Douglas Gregor | 450f0084 | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1936 | break; |
| 1937 | } |
| 1938 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1939 | |
Richard Smith | 74aeef5 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1940 | bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType(); |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1941 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1942 | // Parse declarator '=' initializer. |
Richard Trieu | c64d323 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1943 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | 4972a6d | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1944 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1945 | ConsumeToken(); |
Larisse Voufo | 39a1e50 | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1946 | |
Anders Carlsson | 991285e | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1947 | if (Tok.is(tok::kw_delete)) { |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1948 | if (D.isFunctionDeclarator()) |
| 1949 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1950 | << 1 /* delete */; |
| 1951 | else |
| 1952 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Alexis Hunt | 5dafebc | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1953 | } else if (Tok.is(tok::kw_default)) { |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1954 | if (D.isFunctionDeclarator()) |
Sebastian Redl | 46afb55 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1955 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1956 | << 0 /* default */; |
Alexis Hunt | 5a7fa25 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1957 | else |
| 1958 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1959 | } else { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1960 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1961 | EnterScope(0); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1962 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1963 | } |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1964 | |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1965 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1966 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Peter Collingbourne | 6b4fdc2 | 2012-07-27 12:56:09 +0000 | [diff] [blame] | 1967 | Actions.FinalizeDeclaration(ThisDecl); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1968 | cutOffParsing(); |
| 1969 | return 0; |
Douglas Gregor | 7aa6b22 | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1970 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1971 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1972 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1973 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1974 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1975 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 1f4ee7b | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1976 | ExitScope(); |
| 1977 | } |
Argyrios Kyrtzidis | 3df1978 | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1978 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1979 | if (Init.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1980 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 604c302 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1981 | Actions.ActOnInitializerError(ThisDecl); |
| 1982 | } else |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1983 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1984 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1985 | } |
| 1986 | } else if (Tok.is(tok::l_paren)) { |
| 1987 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1988 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1989 | T.consumeOpen(); |
| 1990 | |
Benjamin Kramer | f062343 | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1991 | ExprVector Exprs; |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1992 | CommaLocsTy CommaLocs; |
| 1993 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1994 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1995 | EnterScope(0); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1996 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1999 | if (ParseExpressionList(Exprs, CommaLocs)) { |
David Blaikie | eae0411 | 2012-10-10 23:15:05 +0000 | [diff] [blame] | 2000 | Actions.ActOnInitializerError(ThisDecl); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2001 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 2002 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2003 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2004 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 2005 | ExitScope(); |
| 2006 | } |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2007 | } else { |
| 2008 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2009 | T.consumeClose(); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2010 | |
| 2011 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 2012 | "Unexpected number of commas!"); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 2013 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2014 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2015 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | 613bf10 | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 2016 | ExitScope(); |
| 2017 | } |
| 2018 | |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2019 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 2020 | T.getCloseLocation(), |
Benjamin Kramer | 62b95d8 | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 2021 | Exprs); |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 2022 | Actions.AddInitializerToDecl(ThisDecl, Initializer.take(), |
| 2023 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2024 | } |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2025 | } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) && |
Fariborz Jahanian | 8be1ecd | 2012-07-03 23:22:13 +0000 | [diff] [blame] | 2026 | (!CurParsedObjCImpl || !D.isFunctionDeclarator())) { |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2027 | // Parse C++0x braced-init-list. |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2028 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 2029 | |
Sebastian Redl | 3da3489 | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 2030 | if (D.getCXXScopeSpec().isSet()) { |
| 2031 | EnterScope(0); |
| 2032 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 2033 | } |
| 2034 | |
| 2035 | ExprResult Init(ParseBraceInitializer()); |
| 2036 | |
| 2037 | if (D.getCXXScopeSpec().isSet()) { |
| 2038 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 2039 | ExitScope(); |
| 2040 | } |
| 2041 | |
| 2042 | if (Init.isInvalid()) { |
| 2043 | Actions.ActOnInitializerError(ThisDecl); |
| 2044 | } else |
| 2045 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 2046 | /*DirectInit=*/true, TypeContainsAuto); |
| 2047 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2048 | } else { |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 2049 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2050 | } |
| 2051 | |
Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 2052 | Actions.FinalizeDeclaration(ThisDecl); |
| 2053 | |
Douglas Gregor | 2399628 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 2054 | return ThisDecl; |
| 2055 | } |
| 2056 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2057 | /// ParseSpecifierQualifierList |
| 2058 | /// specifier-qualifier-list: |
| 2059 | /// type-specifier specifier-qualifier-list[opt] |
| 2060 | /// type-qualifier specifier-qualifier-list[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 2061 | /// [GNU] attributes specifier-qualifier-list[opt] |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2062 | /// |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2063 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 2064 | DeclSpecContext DSC) { |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2065 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 2066 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2067 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2068 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2069 | |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2070 | // Validate declspec for type-name. |
| 2071 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | 2f07ad5 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 2072 | if ((DSC == DSC_type_specifier || DSC == DSC_trailing) && |
| 2073 | !DS.hasTypeSpecifier()) { |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2074 | Diag(Tok, diag::err_expected_type); |
| 2075 | DS.SetTypeSpecError(); |
| 2076 | } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 2077 | !DS.hasAttributes()) { |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2078 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2079 | if (!DS.hasTypeSpecifier()) |
| 2080 | DS.SetTypeSpecError(); |
| 2081 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2082 | |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 2083 | // Issue diagnostic and remove storage class if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2084 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 2085 | if (DS.getStorageClassSpecLoc().isValid()) |
| 2086 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 2087 | else |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2088 | Diag(DS.getThreadStorageClassSpecLoc(), |
| 2089 | diag::err_typename_invalid_storageclass); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 2090 | DS.ClearStorageClassSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2091 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2092 | |
Chris Lattner | 1b22eed | 2006-11-28 05:12:07 +0000 | [diff] [blame] | 2093 | // Issue diagnostic and remove function specfier if present. |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2094 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2095 | if (DS.isInlineSpecified()) |
| 2096 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 2097 | if (DS.isVirtualSpecified()) |
| 2098 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 2099 | if (DS.isExplicitSpecified()) |
| 2100 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Chris Lattner | a925dc6 | 2006-11-28 04:33:46 +0000 | [diff] [blame] | 2101 | DS.ClearFunctionSpecs(); |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2102 | } |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2103 | |
| 2104 | // Issue diagnostic and remove constexpr specfier if present. |
| 2105 | if (DS.isConstexprSpecified()) { |
| 2106 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 2107 | DS.ClearConstexprSpec(); |
| 2108 | } |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 2109 | } |
Chris Lattner | 53361ac | 2006-08-10 05:19:57 +0000 | [diff] [blame] | 2110 | |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2111 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 2112 | /// specified token is valid after the identifier in a declarator which |
| 2113 | /// immediately follows the declspec. For example, these things are valid: |
| 2114 | /// |
| 2115 | /// int x [ 4]; // direct-declarator |
| 2116 | /// int x ( int y); // direct-declarator |
| 2117 | /// int(int x ) // direct-declarator |
| 2118 | /// int x ; // simple-declaration |
| 2119 | /// int x = 17; // init-declarator-list |
| 2120 | /// int x , y; // init-declarator-list |
| 2121 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | a723ba9 | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2122 | /// int x : 4; // struct-declarator |
Chris Lattner | 2b988c1 | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 2123 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2124 | /// |
| 2125 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 2126 | /// ')' happens to be valid anyway). |
| 2127 | /// int (x) |
| 2128 | /// |
| 2129 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 2130 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 2131 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | a723ba9 | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2132 | T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon); |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2135 | |
| 2136 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 2137 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 2138 | /// the declspec has no type specifier. In this case, the declspec is either |
| 2139 | /// malformed or is "implicit int" (in K&R and C89). |
| 2140 | /// |
| 2141 | /// This method handles diagnosing this prettily and returns false if the |
| 2142 | /// declspec is done being processed. If it recovers and thinks there may be |
| 2143 | /// other pieces of declspec after it, it returns true. |
| 2144 | /// |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2145 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2146 | const ParsedTemplateInfo &TemplateInfo, |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2147 | AccessSpecifier AS, DeclSpecContext DSC, |
| 2148 | ParsedAttributesWithRange &Attrs) { |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2149 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2150 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2151 | SourceLocation Loc = Tok.getLocation(); |
| 2152 | // If we see an identifier that is not a type name, we normally would |
| 2153 | // parse it as the identifer being declared. However, when a typename |
| 2154 | // is typo'd or the definition is not included, this will incorrectly |
| 2155 | // parse the typename as the identifier name and fall over misparsing |
| 2156 | // later parts of the diagnostic. |
| 2157 | // |
| 2158 | // As such, we try to do some look-ahead in cases where this would |
| 2159 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 2160 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 2161 | // an identifier with implicit int, we'd get a parse error because the |
| 2162 | // next token is obviously invalid for a type. Parse these as a case |
| 2163 | // with an invalid type specifier. |
| 2164 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2165 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2166 | // Since we know that this either implicit int (which is rare) or an |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2167 | // error, do lookahead to try to do better recovery. This never applies |
| 2168 | // within a type specifier. Outside of C++, we allow this even if the |
| 2169 | // language doesn't "officially" support implicit int -- we support |
Richard Smith | 3b87038 | 2013-04-30 22:43:51 +0000 | [diff] [blame] | 2170 | // implicit int as an extension in C99 and C11. |
Richard Smith | 2f07ad5 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 2171 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
Richard Smith | 3b87038 | 2013-04-30 22:43:51 +0000 | [diff] [blame] | 2172 | !getLangOpts().CPlusPlus && |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2173 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2174 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 2175 | // we just avoid eating the identifier, so it will be parsed as the |
| 2176 | // identifier in the declarator. |
| 2177 | return false; |
| 2178 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2179 | |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2180 | if (getLangOpts().CPlusPlus && |
| 2181 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 2182 | // Don't require a type specifier if we have the 'auto' storage class |
| 2183 | // specifier in C++98 -- we'll promote it to a type specifier. |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2184 | if (SS) |
| 2185 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2186 | return false; |
| 2187 | } |
| 2188 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2189 | // Otherwise, if we don't consume this token, we are going to emit an |
| 2190 | // error anyway. Try to recover from various common problems. Check |
| 2191 | // to see if this was a reference to a tag name without a tag specified. |
| 2192 | // This is a common problem in C (saying 'foo' instead of 'struct foo'). |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2193 | // |
| 2194 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 2195 | if (SS == 0) { |
Argyrios Kyrtzidis | 1f32940 | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2196 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2197 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2198 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2199 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2200 | default: break; |
Argyrios Kyrtzidis | 1f32940 | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2201 | case DeclSpec::TST_enum: |
| 2202 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 2203 | case DeclSpec::TST_union: |
| 2204 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 2205 | case DeclSpec::TST_struct: |
| 2206 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2207 | case DeclSpec::TST_interface: |
| 2208 | TagName="__interface"; FixitTagName = "__interface "; |
| 2209 | TagKind=tok::kw___interface;break; |
Argyrios Kyrtzidis | 1f32940 | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2210 | case DeclSpec::TST_class: |
| 2211 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2212 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2213 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2214 | if (TagName) { |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2215 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 2216 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 2217 | Sema::LookupOrdinaryName); |
| 2218 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2219 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2220 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 2221 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 2222 | |
| 2223 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 2224 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 2225 | I != IEnd; ++I) |
Kaelyn Uhrain | 3fe3f85 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 2226 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | 031643e | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2227 | << TokenName << TagName; |
| 2228 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2229 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2230 | // Parse this as a tag as if the missing tag were present. |
| 2231 | if (TagKind == tok::kw_enum) |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2232 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2233 | else |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2234 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2235 | /*EnteringContext*/ false, DSC_normal, Attrs); |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2236 | return true; |
| 2237 | } |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2238 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2239 | |
Richard Smith | fe904f0 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2240 | // Determine whether this identifier could plausibly be the name of something |
Richard Smith | edd124e | 2012-05-15 21:42:17 +0000 | [diff] [blame] | 2241 | // being declared (with a missing type). |
Richard Smith | fe904f0 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2242 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
| 2243 | (!SS || DSC == DSC_top_level || DSC == DSC_class)) { |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2244 | // Look ahead to the next token to try to figure out what this declaration |
| 2245 | // was supposed to be. |
| 2246 | switch (NextToken().getKind()) { |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2247 | case tok::l_paren: { |
| 2248 | // static x(4); // 'x' is not a type |
| 2249 | // x(int n); // 'x' is not a type |
| 2250 | // x (*p)[]; // 'x' is a type |
| 2251 | // |
| 2252 | // Since we're in an error case (or the rare 'implicit int in C++' MS |
| 2253 | // extension), we can afford to perform a tentative parse to determine |
| 2254 | // which case we're in. |
| 2255 | TentativeParsingAction PA(*this); |
| 2256 | ConsumeToken(); |
| 2257 | TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false); |
| 2258 | PA.Revert(); |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2259 | |
| 2260 | if (TPR != TPResult::False()) { |
| 2261 | // The identifier is followed by a parenthesized declarator. |
| 2262 | // It's supposed to be a type. |
| 2263 | break; |
| 2264 | } |
| 2265 | |
| 2266 | // If we're in a context where we could be declaring a constructor, |
| 2267 | // check whether this is a constructor declaration with a bogus name. |
| 2268 | if (DSC == DSC_class || (DSC == DSC_top_level && SS)) { |
| 2269 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2270 | if (Actions.isCurrentClassNameTypo(II, SS)) { |
| 2271 | Diag(Loc, diag::err_constructor_bad_name) |
| 2272 | << Tok.getIdentifierInfo() << II |
| 2273 | << FixItHint::CreateReplacement(Tok.getLocation(), II->getName()); |
| 2274 | Tok.setIdentifierInfo(II); |
| 2275 | } |
| 2276 | } |
| 2277 | // Fall through. |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2278 | } |
Richard Smith | fb8b7b9 | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2279 | case tok::comma: |
| 2280 | case tok::equal: |
| 2281 | case tok::kw_asm: |
| 2282 | case tok::l_brace: |
| 2283 | case tok::l_square: |
| 2284 | case tok::semi: |
| 2285 | // This looks like a variable or function declaration. The type is |
| 2286 | // probably missing. We're done parsing decl-specifiers. |
| 2287 | if (SS) |
| 2288 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
| 2289 | return false; |
Richard Smith | a952ebb | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2290 | |
| 2291 | default: |
| 2292 | // This is probably supposed to be a type. This includes cases like: |
| 2293 | // int f(itn); |
| 2294 | // struct S { unsinged : 4; }; |
| 2295 | break; |
| 2296 | } |
| 2297 | } |
| 2298 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2299 | // This is almost certainly an invalid type name. Let the action emit a |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2300 | // diagnostic and attempt to recover. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2301 | ParsedType T; |
Kaelyn Uhrain | b5b17fe | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2302 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2303 | if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) { |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2304 | // The action emitted a diagnostic, so we don't have to. |
| 2305 | if (T) { |
| 2306 | // The action has suggested that the type T could be used. Set that as |
| 2307 | // the type in the declaration specifiers, consume the would-be type |
| 2308 | // name token, and we're done. |
| 2309 | const char *PrevSpec; |
| 2310 | unsigned DiagID; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2311 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2312 | DS.SetRangeEnd(Tok.getLocation()); |
| 2313 | ConsumeToken(); |
Kaelyn Uhrain | b5b17fe | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2314 | // There may be other declaration specifiers after this. |
| 2315 | return true; |
| 2316 | } else if (II != Tok.getIdentifierInfo()) { |
| 2317 | // If no type was suggested, the correction is to a keyword |
| 2318 | Tok.setKind(II->getTokenID()); |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2319 | // There may be other declaration specifiers after this. |
| 2320 | return true; |
| 2321 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2322 | |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2323 | // Fall through; the action had no suggestion for us. |
| 2324 | } else { |
| 2325 | // The action did not emit a diagnostic, so emit one now. |
| 2326 | SourceRange R; |
| 2327 | if (SS) R = SS->getRange(); |
| 2328 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 2329 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2330 | |
Douglas Gregor | 15e5602 | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2331 | // Mark this as an error. |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2332 | DS.SetTypeSpecError(); |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2333 | DS.SetRangeEnd(Tok.getLocation()); |
| 2334 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2335 | |
Chris Lattner | 20a0c61 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2336 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 2337 | // avoid rippling error messages on subsequent uses of the same type, |
| 2338 | // could be useful if #include was forgotten. |
| 2339 | return false; |
| 2340 | } |
| 2341 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2342 | /// \brief Determine the declaration specifier context from the declarator |
| 2343 | /// context. |
| 2344 | /// |
| 2345 | /// \param Context the declarator context, which is one of the |
| 2346 | /// Declarator::TheContext enumerator values. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2347 | Parser::DeclSpecContext |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2348 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 2349 | if (Context == Declarator::MemberContext) |
| 2350 | return DSC_class; |
| 2351 | if (Context == Declarator::FileContext) |
| 2352 | return DSC_top_level; |
Richard Smith | 62dad82 | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 2353 | if (Context == Declarator::TrailingReturnContext) |
| 2354 | return DSC_trailing; |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2355 | return DSC_normal; |
| 2356 | } |
| 2357 | |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2358 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 2359 | /// |
| 2360 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 2361 | /// type. Ideally, the type should be propagated directly into Sema. |
| 2362 | /// |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2363 | /// [C11] type-id |
| 2364 | /// [C11] constant-expression |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2365 | /// [C++0x] type-id ...[opt] |
| 2366 | /// [C++0x] assignment-expression ...[opt] |
| 2367 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 2368 | SourceLocation &EllipsisLoc) { |
| 2369 | ExprResult ER; |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2370 | if (isTypeIdInParens()) { |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2371 | SourceLocation TypeLoc = Tok.getLocation(); |
| 2372 | ParsedType Ty = ParseTypeName().get(); |
| 2373 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2374 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 2375 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2376 | } else |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2377 | ER = ParseConstantExpression(); |
| 2378 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2379 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis)) |
Peter Collingbourne | ccbcce0 | 2011-10-24 17:56:00 +0000 | [diff] [blame] | 2380 | EllipsisLoc = ConsumeToken(); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2381 | |
| 2382 | return ER; |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
| 2385 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 2386 | /// attribute to Attrs. |
| 2387 | /// |
| 2388 | /// alignment-specifier: |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2389 | /// [C11] '_Alignas' '(' type-id ')' |
| 2390 | /// [C11] '_Alignas' '(' constant-expression ')' |
Richard Smith | d11c7a1 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2391 | /// [C++11] 'alignas' '(' type-id ...[opt] ')' |
| 2392 | /// [C++11] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2393 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2394 | SourceLocation *EndLoc) { |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2395 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 2396 | "Not an alignment-specifier!"); |
| 2397 | |
Richard Smith | d11c7a1 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2398 | IdentifierInfo *KWName = Tok.getIdentifierInfo(); |
| 2399 | SourceLocation KWLoc = ConsumeToken(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2400 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2401 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2402 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2403 | return; |
| 2404 | |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2405 | SourceLocation EllipsisLoc; |
| 2406 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2407 | if (ArgExpr.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2408 | T.skipToEnd(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2409 | return; |
| 2410 | } |
| 2411 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2412 | T.consumeClose(); |
Richard Smith | 44c247f | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2413 | if (EndLoc) |
| 2414 | *EndLoc = T.getCloseLocation(); |
Peter Collingbourne | 7d33cd3 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2415 | |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2416 | ArgsVector ArgExprs; |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2417 | ArgExprs.push_back(ArgExpr.release()); |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2418 | Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1, |
| 2419 | AttributeList::AS_Keyword, EllipsisLoc); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2422 | /// Determine whether we're looking at something that might be a declarator |
| 2423 | /// in a simple-declaration. If it can't possibly be a declarator, maybe |
| 2424 | /// diagnose a missing semicolon after a prior tag definition in the decl |
| 2425 | /// specifier. |
| 2426 | /// |
| 2427 | /// \return \c true if an error occurred and this can't be any kind of |
| 2428 | /// declaration. |
| 2429 | bool |
| 2430 | Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS, |
| 2431 | DeclSpecContext DSContext, |
| 2432 | LateParsedAttrList *LateAttrs) { |
| 2433 | assert(DS.hasTagDefinition() && "shouldn't call this"); |
| 2434 | |
| 2435 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2436 | |
| 2437 | if (getLangOpts().CPlusPlus && |
| 2438 | (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) || |
| 2439 | Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) && |
| 2440 | TryAnnotateCXXScopeToken(EnteringContext)) { |
| 2441 | SkipMalformedDecl(); |
| 2442 | return true; |
| 2443 | } |
| 2444 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2445 | bool HasScope = Tok.is(tok::annot_cxxscope); |
| 2446 | // Make a copy in case GetLookAheadToken invalidates the result of NextToken. |
| 2447 | Token AfterScope = HasScope ? NextToken() : Tok; |
| 2448 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2449 | // Determine whether the following tokens could possibly be a |
| 2450 | // declarator. |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2451 | bool MightBeDeclarator = true; |
| 2452 | if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) { |
| 2453 | // A declarator-id can't start with 'typename'. |
| 2454 | MightBeDeclarator = false; |
| 2455 | } else if (AfterScope.is(tok::annot_template_id)) { |
| 2456 | // If we have a type expressed as a template-id, this cannot be a |
| 2457 | // declarator-id (such a type cannot be redeclared in a simple-declaration). |
| 2458 | TemplateIdAnnotation *Annot = |
| 2459 | static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue()); |
| 2460 | if (Annot->Kind == TNK_Type_template) |
| 2461 | MightBeDeclarator = false; |
| 2462 | } else if (AfterScope.is(tok::identifier)) { |
| 2463 | const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken(); |
| 2464 | |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2465 | // These tokens cannot come after the declarator-id in a |
| 2466 | // simple-declaration, and are likely to come after a type-specifier. |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2467 | if (Next.is(tok::star) || Next.is(tok::amp) || Next.is(tok::ampamp) || |
| 2468 | Next.is(tok::identifier) || Next.is(tok::annot_cxxscope) || |
| 2469 | Next.is(tok::coloncolon)) { |
| 2470 | // Missing a semicolon. |
| 2471 | MightBeDeclarator = false; |
| 2472 | } else if (HasScope) { |
| 2473 | // If the declarator-id has a scope specifier, it must redeclare a |
| 2474 | // previously-declared entity. If that's a type (and this is not a |
| 2475 | // typedef), that's an error. |
| 2476 | CXXScopeSpec SS; |
| 2477 | Actions.RestoreNestedNameSpecifierAnnotation( |
| 2478 | Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS); |
| 2479 | IdentifierInfo *Name = AfterScope.getIdentifierInfo(); |
| 2480 | Sema::NameClassification Classification = Actions.ClassifyName( |
| 2481 | getCurScope(), SS, Name, AfterScope.getLocation(), Next, |
| 2482 | /*IsAddressOfOperand*/false); |
| 2483 | switch (Classification.getKind()) { |
| 2484 | case Sema::NC_Error: |
| 2485 | SkipMalformedDecl(); |
| 2486 | return true; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2487 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2488 | case Sema::NC_Keyword: |
| 2489 | case Sema::NC_NestedNameSpecifier: |
| 2490 | llvm_unreachable("typo correction and nested name specifiers not " |
| 2491 | "possible here"); |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2492 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2493 | case Sema::NC_Type: |
| 2494 | case Sema::NC_TypeTemplate: |
| 2495 | // Not a previously-declared non-type entity. |
| 2496 | MightBeDeclarator = false; |
| 2497 | break; |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2498 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2499 | case Sema::NC_Unknown: |
| 2500 | case Sema::NC_Expression: |
| 2501 | case Sema::NC_VarTemplate: |
| 2502 | case Sema::NC_FunctionTemplate: |
| 2503 | // Might be a redeclaration of a prior entity. |
| 2504 | break; |
| 2505 | } |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2506 | } |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
Richard Smith | 698875a | 2013-11-20 23:40:57 +0000 | [diff] [blame] | 2509 | if (MightBeDeclarator) |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2510 | return false; |
| 2511 | |
| 2512 | Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()), |
| 2513 | diag::err_expected_semi_after_tagdecl) |
| 2514 | << DeclSpec::getSpecifierName(DS.getTypeSpecType()); |
| 2515 | |
| 2516 | // Try to recover from the typo, by dropping the tag definition and parsing |
| 2517 | // the problematic tokens as a type. |
| 2518 | // |
| 2519 | // FIXME: Split the DeclSpec into pieces for the standalone |
| 2520 | // declaration and pieces for the following declaration, instead |
| 2521 | // of assuming that all the other pieces attach to new declaration, |
| 2522 | // and call ParsedFreeStandingDeclSpec as appropriate. |
| 2523 | DS.ClearTypeSpecType(); |
| 2524 | ParsedTemplateInfo NotATemplate; |
| 2525 | ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs); |
| 2526 | return false; |
| 2527 | } |
| 2528 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2529 | /// ParseDeclarationSpecifiers |
| 2530 | /// declaration-specifiers: [C99 6.7] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2531 | /// storage-class-specifier declaration-specifiers[opt] |
| 2532 | /// type-specifier declaration-specifiers[opt] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2533 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2534 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 2535 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2536 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2537 | /// |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2538 | /// storage-class-specifier: [C99 6.7.1] |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 2539 | /// 'typedef' |
| 2540 | /// 'extern' |
| 2541 | /// 'static' |
| 2542 | /// 'auto' |
| 2543 | /// 'register' |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2544 | /// [C++] 'mutable' |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2545 | /// [C++11] 'thread_local' |
| 2546 | /// [C11] '_Thread_local' |
Chris Lattner | da48a8e | 2006-08-04 05:25:55 +0000 | [diff] [blame] | 2547 | /// [GNU] '__thread' |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2548 | /// function-specifier: [C99 6.7.4] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 2549 | /// [C99] 'inline' |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2550 | /// [C++] 'virtual' |
| 2551 | /// [C++] 'explicit' |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2552 | /// [OpenCL] '__kernel' |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2553 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2554 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2555 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2556 | /// |
Douglas Gregor | b9bd8a9 | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 2557 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 1b57ff3 | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2558 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2559 | AccessSpecifier AS, |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2560 | DeclSpecContext DSContext, |
| 2561 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2562 | if (DS.getSourceRange().isInvalid()) { |
| 2563 | DS.SetRangeStart(Tok.getLocation()); |
| 2564 | DS.SetRangeEnd(Tok.getLocation()); |
| 2565 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2566 | |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2567 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2568 | bool AttrsLastTime = false; |
| 2569 | ParsedAttributesWithRange attrs(AttrFactory); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2570 | while (1) { |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2571 | bool isInvalid = false; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2572 | const char *PrevSpec = 0; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2573 | unsigned DiagID = 0; |
| 2574 | |
Chris Lattner | 4d8f873 | 2006-11-28 05:05:08 +0000 | [diff] [blame] | 2575 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 450c75a | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2576 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 2577 | switch (Tok.getKind()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2578 | default: |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2579 | DoneWithDeclSpec: |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2580 | if (!AttrsLastTime) |
| 2581 | ProhibitAttributes(attrs); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2582 | else { |
| 2583 | // Reject C++11 attributes that appertain to decl specifiers as |
| 2584 | // we don't support any C++11 attributes that appertain to decl |
| 2585 | // specifiers. This also conforms to what g++ 4.8 is doing. |
| 2586 | ProhibitCXX11Attributes(attrs); |
| 2587 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2588 | DS.takeAttributesFrom(attrs); |
Michael Han | 64536a6 | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2589 | } |
Peter Collingbourne | 70188b3 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 2590 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2591 | // If this is not a declaration specifier token, we're done reading decl |
| 2592 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | e3e01a2 | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2593 | DS.Finish(Diags, PP); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 2594 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2595 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2596 | case tok::l_square: |
| 2597 | case tok::kw_alignas: |
Richard Smith | 4cabd04 | 2013-02-22 09:15:49 +0000 | [diff] [blame] | 2598 | if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier()) |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2599 | goto DoneWithDeclSpec; |
| 2600 | |
| 2601 | ProhibitAttributes(attrs); |
| 2602 | // FIXME: It would be good to recover by accepting the attributes, |
| 2603 | // but attempting to do that now would cause serious |
| 2604 | // madness in terms of diagnostics. |
| 2605 | attrs.clear(); |
| 2606 | attrs.Range = SourceRange(); |
| 2607 | |
| 2608 | ParseCXX11Attributes(attrs); |
| 2609 | AttrsLastTime = true; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2610 | continue; |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2611 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2612 | case tok::code_completion: { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2613 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2614 | if (DS.hasTypeSpecifier()) { |
| 2615 | bool AllowNonIdentifiers |
| 2616 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 2617 | Scope::BlockScope | |
| 2618 | Scope::TemplateParamScope | |
| 2619 | Scope::FunctionPrototypeScope | |
| 2620 | Scope::AtCatchScope)) == 0; |
| 2621 | bool AllowNestedNameSpecifiers |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2622 | = DSContext == DSC_top_level || |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2623 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 2624 | |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2625 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2626 | AllowNonIdentifiers, |
Douglas Gregor | bfcea8b | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2627 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2628 | return cutOffParsing(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2629 | } |
| 2630 | |
Douglas Gregor | 8003924 | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2631 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 2632 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 2633 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2634 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2635 | : Sema::PCC_Template; |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2636 | else if (DSContext == DSC_class) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2637 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | b6c6a58 | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 2638 | else if (CurParsedObjCImpl) |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2639 | CCC = Sema::PCC_ObjCImplementation; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2640 | |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2641 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2642 | return cutOffParsing(); |
Douglas Gregor | c49f5b2 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2643 | } |
| 2644 | |
Chris Lattner | bd31aa3 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2645 | case tok::coloncolon: // ::foo::bar |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2646 | // C++ scope specifier. Annotate and loop, or bail out on error. |
Eli Friedman | 2a1d9a9 | 2013-08-15 23:59:20 +0000 | [diff] [blame] | 2647 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2648 | if (!DS.hasTypeSpecifier()) |
| 2649 | DS.SetTypeSpecError(); |
| 2650 | goto DoneWithDeclSpec; |
| 2651 | } |
John McCall | 8bc2a70 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 2652 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 2653 | goto DoneWithDeclSpec; |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2654 | continue; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2655 | |
| 2656 | case tok::annot_cxxscope: { |
Richard Smith | 3092a3b | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2657 | if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector()) |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2658 | goto DoneWithDeclSpec; |
| 2659 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2660 | CXXScopeSpec SS; |
Douglas Gregor | 869ad45 | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 2661 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 2662 | Tok.getAnnotationRange(), |
| 2663 | SS); |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2664 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2665 | // We are looking for a qualified typename. |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2666 | Token Next = NextToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2667 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2668 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2669 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2670 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2671 | |
| 2672 | // C++ [class.qual]p2: |
| 2673 | // In a lookup in which the constructor is an acceptable lookup |
| 2674 | // result and the nested-name-specifier nominates a class C: |
| 2675 | // |
| 2676 | // - if the name specified after the |
| 2677 | // nested-name-specifier, when looked up in C, is the |
| 2678 | // injected-class-name of C (Clause 9), or |
| 2679 | // |
| 2680 | // - if the name specified after the nested-name-specifier |
| 2681 | // is the same as the identifier or the |
| 2682 | // simple-template-id's template-name in the last |
| 2683 | // component of the nested-name-specifier, |
| 2684 | // |
| 2685 | // the name is instead considered to name the constructor of |
| 2686 | // class C. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2687 | // |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2688 | // Thus, if the template-name is actually the constructor |
| 2689 | // name, then the code is ill-formed; this interpretation is |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2690 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2691 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
Dmitri Gribenko | d1c91f1 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2692 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2693 | TemplateId->Name && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2694 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2695 | if (isConstructorDeclarator()) { |
| 2696 | // The user meant this to be an out-of-line constructor |
| 2697 | // definition, but template arguments are not allowed |
| 2698 | // there. Just allow this as a constructor; we'll |
| 2699 | // complain about it later. |
| 2700 | goto DoneWithDeclSpec; |
| 2701 | } |
| 2702 | |
| 2703 | // The user meant this to name a type, but it actually names |
| 2704 | // a constructor with some extraneous template |
| 2705 | // arguments. Complain, then parse it as a type as the user |
| 2706 | // intended. |
| 2707 | Diag(TemplateId->TemplateNameLoc, |
| 2708 | diag::err_out_of_line_template_id_names_constructor) |
| 2709 | << TemplateId->Name; |
| 2710 | } |
| 2711 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2712 | DS.getTypeSpecScope() = SS; |
| 2713 | ConsumeToken(); // The C++ scope. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2714 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2715 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | e7c2065 | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2716 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2717 | continue; |
| 2718 | } |
| 2719 | |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2720 | if (Next.is(tok::annot_typename)) { |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2721 | DS.getTypeSpecScope() = SS; |
| 2722 | ConsumeToken(); // The C++ scope. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2723 | if (Tok.getAnnotationValue()) { |
| 2724 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 7743034 | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2725 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2726 | Tok.getAnnotationEndLoc(), |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2727 | PrevSpec, DiagID, T); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2728 | if (isInvalid) |
| 2729 | break; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2730 | } |
Douglas Gregor | c5790df | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2731 | else |
| 2732 | DS.SetTypeSpecError(); |
| 2733 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2734 | ConsumeToken(); // The typename |
| 2735 | } |
| 2736 | |
Douglas Gregor | 167fa62 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2737 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2738 | goto DoneWithDeclSpec; |
| 2739 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2740 | // If we're in a context where the identifier could be a class name, |
| 2741 | // check whether this is a constructor declaration. |
Dmitri Gribenko | d1c91f1 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2742 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2743 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2744 | &SS)) { |
| 2745 | if (isConstructorDeclarator()) |
| 2746 | goto DoneWithDeclSpec; |
| 2747 | |
| 2748 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 2749 | // of the class is qualified in a context where it could name |
| 2750 | // a constructor, its a constructor name. However, we've |
| 2751 | // looked at the declarator, and the user probably meant this |
| 2752 | // to be a type. Complain that it isn't supposed to be treated |
| 2753 | // as a type, then proceed to parse it as a type. |
| 2754 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 2755 | << Next.getIdentifierInfo(); |
| 2756 | } |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2757 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2758 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 2759 | Next.getLocation(), |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2760 | getCurScope(), &SS, |
| 2761 | false, false, ParsedType(), |
Abramo Bagnara | 4244b43 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2762 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 844cb50 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2763 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2764 | |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2765 | // If the referenced identifier is not a type, then this declspec is |
| 2766 | // erroneous: We already checked about that it has no type specifier, and |
| 2767 | // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2768 | // typename. |
David Blaikie | 7d17010 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 2769 | if (!TypeRep) { |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2770 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2771 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2772 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) { |
| 2773 | if (!Attrs.empty()) { |
| 2774 | AttrsLastTime = true; |
| 2775 | attrs.takeAllFrom(Attrs); |
| 2776 | } |
| 2777 | continue; |
| 2778 | } |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2779 | goto DoneWithDeclSpec; |
Chris Lattner | b4a8fe8 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2780 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2781 | |
John McCall | 9dab4e6 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2782 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2783 | ConsumeToken(); // The C++ scope. |
| 2784 | |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2785 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2786 | DiagID, TypeRep); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2787 | if (isInvalid) |
| 2788 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2789 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2790 | DS.SetRangeEnd(Tok.getLocation()); |
| 2791 | ConsumeToken(); // The typename. |
| 2792 | |
| 2793 | continue; |
| 2794 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2795 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2796 | case tok::annot_typename: { |
Richard Smith | 404dfb4 | 2013-11-19 22:47:36 +0000 | [diff] [blame] | 2797 | // If we've previously seen a tag definition, we were almost surely |
| 2798 | // missing a semicolon after it. |
| 2799 | if (DS.hasTypeSpecifier() && DS.hasTagDefinition()) |
| 2800 | goto DoneWithDeclSpec; |
| 2801 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2802 | if (Tok.getAnnotationValue()) { |
| 2803 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 7f8bb36 | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 2804 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2805 | DiagID, T); |
| 2806 | } else |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2807 | DS.SetTypeSpecError(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2808 | |
Chris Lattner | 005fc1b | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 2809 | if (isInvalid) |
| 2810 | break; |
| 2811 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2812 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2813 | ConsumeToken(); // The typename |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2814 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2815 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2816 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2817 | // Objective-C interface. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2818 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 06e41ae | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2819 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2820 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2821 | continue; |
| 2822 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2823 | |
Douglas Gregor | 0687309 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2824 | case tok::kw___is_signed: |
| 2825 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 2826 | // typically treats it as a trait. If we see __is_signed as it appears |
| 2827 | // in libstdc++, e.g., |
| 2828 | // |
| 2829 | // static const bool __is_signed; |
| 2830 | // |
| 2831 | // then treat __is_signed as an identifier rather than as a keyword. |
| 2832 | if (DS.getTypeSpecType() == TST_bool && |
| 2833 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
Alp Toker | 47642d2 | 2013-12-03 06:13:01 +0000 | [diff] [blame] | 2834 | DS.getStorageClassSpec() == DeclSpec::SCS_static) |
| 2835 | TryKeywordIdentFallback(true); |
Douglas Gregor | 0687309 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2836 | |
| 2837 | // We're done with the declaration-specifiers. |
| 2838 | goto DoneWithDeclSpec; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2839 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2840 | // typedef-name |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2841 | case tok::kw_decltype: |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2842 | case tok::identifier: { |
Chris Lattner | bd31aa3 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2843 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 2844 | // so handle it as such. This is important for ctor parsing. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2845 | if (getLangOpts().CPlusPlus) { |
Eli Friedman | 2a1d9a9 | 2013-08-15 23:59:20 +0000 | [diff] [blame] | 2846 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2847 | if (!DS.hasTypeSpecifier()) |
| 2848 | DS.SetTypeSpecError(); |
| 2849 | goto DoneWithDeclSpec; |
| 2850 | } |
| 2851 | if (!Tok.is(tok::identifier)) |
| 2852 | continue; |
| 2853 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2854 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2855 | // This identifier can only be a typedef name if we haven't already seen |
| 2856 | // a type-specifier. Without this check we misparse: |
| 2857 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 2858 | if (DS.hasTypeSpecifier()) |
| 2859 | goto DoneWithDeclSpec; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2860 | |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2861 | // Check for need to substitute AltiVec keyword tokens. |
| 2862 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2863 | break; |
| 2864 | |
Richard Smith | 3092a3b | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2865 | // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not |
| 2866 | // allow the use of a typedef name as a type specifier. |
| 2867 | if (DS.isTypeAltiVecVector()) |
| 2868 | goto DoneWithDeclSpec; |
| 2869 | |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2870 | ParsedType TypeRep = |
| 2871 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 2872 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2873 | |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2874 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 2875 | // it must be an implicit int or an error. |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2876 | if (!TypeRep) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2877 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2878 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) { |
| 2879 | if (!Attrs.empty()) { |
| 2880 | AttrsLastTime = true; |
| 2881 | attrs.takeAllFrom(Attrs); |
| 2882 | } |
| 2883 | continue; |
| 2884 | } |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2885 | goto DoneWithDeclSpec; |
Chris Lattner | 6cc055a | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2886 | } |
Douglas Gregor | 8bf4205 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2887 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2888 | // If we're in a context where the identifier could be a class name, |
| 2889 | // check whether this is a constructor declaration. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2890 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2891 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2892 | isConstructorDeclarator()) |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2893 | goto DoneWithDeclSpec; |
| 2894 | |
Douglas Gregor | 9817f4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2895 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2896 | DiagID, TypeRep); |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2897 | if (isInvalid) |
| 2898 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2899 | |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2900 | DS.SetRangeEnd(Tok.getLocation()); |
| 2901 | ConsumeToken(); // The identifier |
| 2902 | |
| 2903 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2904 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2905 | // Objective-C interface. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2906 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 06e41ae | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2907 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2908 | |
Steve Naroff | cd5e782 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2909 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2910 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2911 | continue; |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2912 | } |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2913 | |
| 2914 | // type-name |
| 2915 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | c0c5dd2 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2916 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | b67535d | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2917 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2918 | // This template-id does not refer to a type name, so we're |
| 2919 | // done with the type-specifiers. |
| 2920 | goto DoneWithDeclSpec; |
| 2921 | } |
| 2922 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2923 | // If we're in a context where the template-id could be a |
| 2924 | // constructor name or specialization, check whether this is a |
| 2925 | // constructor declaration. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2926 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2927 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2928 | isConstructorDeclarator()) |
| 2929 | goto DoneWithDeclSpec; |
| 2930 | |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2931 | // Turn the template-id annotation token into a type annotation |
| 2932 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | fe3d7d0 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2933 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2934 | continue; |
| 2935 | } |
| 2936 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 2937 | // GNU attributes support. |
| 2938 | case tok::kw___attribute: |
DeLesley Hutchins | bd2ee13 | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2939 | ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs); |
Chris Lattner | b95cca0 | 2006-10-17 03:01:08 +0000 | [diff] [blame] | 2940 | continue; |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2941 | |
| 2942 | // Microsoft declspec support. |
| 2943 | case tok::kw___declspec: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2944 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | 3a9b7e0 | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2945 | continue; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2946 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2947 | // Microsoft single token adornments. |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2948 | case tok::kw___forceinline: { |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2949 | isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID); |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2950 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2951 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Alexis Hunt | a0e54d4 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 2952 | // FIXME: This does not work correctly if it is set to be a declspec |
| 2953 | // attribute, and a GNU attribute is simply incorrect. |
Aaron Ballman | 00e9996 | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2954 | DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 2955 | AttributeList::AS_GNU); |
Richard Smith | da83703 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2956 | break; |
Michael J. Spencer | f97bd8c | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2957 | } |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2958 | |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 2959 | case tok::kw___sptr: |
| 2960 | case tok::kw___uptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2961 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2962 | case tok::kw___ptr32: |
Steve Naroff | f9c29d4 | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2963 | case tok::kw___w64: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2964 | case tok::kw___cdecl: |
| 2965 | case tok::kw___stdcall: |
| 2966 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2967 | case tok::kw___thiscall: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2968 | case tok::kw___unaligned: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2969 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2970 | continue; |
| 2971 | |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2972 | // Borland single token adornments. |
| 2973 | case tok::kw___pascal: |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2974 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2975 | continue; |
| 2976 | |
Peter Collingbourne | 7ce13fc | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2977 | // OpenCL single token adornments. |
| 2978 | case tok::kw___kernel: |
| 2979 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2980 | continue; |
| 2981 | |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2982 | // storage-class-specifier |
| 2983 | case tok::kw_typedef: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2984 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
| 2985 | PrevSpec, DiagID); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2986 | break; |
| 2987 | case tok::kw_extern: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2988 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2989 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2990 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
| 2991 | PrevSpec, DiagID); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2992 | break; |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2993 | case tok::kw___private_extern__: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2994 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
| 2995 | Loc, PrevSpec, DiagID); |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2996 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 2997 | case tok::kw_static: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2998 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2999 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3000 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
| 3001 | PrevSpec, DiagID); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3002 | break; |
| 3003 | case tok::kw_auto: |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3004 | if (getLangOpts().CPlusPlus11) { |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3005 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3006 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 3007 | PrevSpec, DiagID); |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3008 | if (!isInvalid) |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 3009 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3010 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 3011 | } else |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3012 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 3013 | DiagID); |
Richard Smith | 58c7433 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 3014 | } else |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3015 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 3016 | PrevSpec, DiagID); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3017 | break; |
| 3018 | case tok::kw_register: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3019 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
| 3020 | PrevSpec, DiagID); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3021 | break; |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 3022 | case tok::kw_mutable: |
Peter Collingbourne | 485b80f | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 3023 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
| 3024 | PrevSpec, DiagID); |
Sebastian Redl | ccdfaba | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 3025 | break; |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3026 | case tok::kw___thread: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 3027 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc, |
| 3028 | PrevSpec, DiagID); |
| 3029 | break; |
| 3030 | case tok::kw_thread_local: |
| 3031 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc, |
| 3032 | PrevSpec, DiagID); |
| 3033 | break; |
| 3034 | case tok::kw__Thread_local: |
| 3035 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local, |
| 3036 | Loc, PrevSpec, DiagID); |
Chris Lattner | f63f89a | 2006-08-05 03:28:50 +0000 | [diff] [blame] | 3037 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3038 | |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3039 | // function-specifier |
| 3040 | case tok::kw_inline: |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 3041 | isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID); |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3042 | break; |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3043 | case tok::kw_virtual: |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 3044 | isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3045 | break; |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3046 | case tok::kw_explicit: |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 3047 | isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3048 | break; |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 3049 | case tok::kw__Noreturn: |
| 3050 | if (!getLangOpts().C11) |
| 3051 | Diag(Loc, diag::ext_c11_noreturn); |
Serge Pavlov | 750db65 | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 3052 | isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID); |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 3053 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3054 | |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 3055 | // alignment-specifier |
| 3056 | case tok::kw__Alignas: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3057 | if (!getLangOpts().C11) |
Jordan Rose | 58d5472 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 3058 | Diag(Tok, diag::ext_c11_alignment) << Tok.getName(); |
Peter Collingbourne | 2f3cf4b | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 3059 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 3060 | continue; |
| 3061 | |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 3062 | // friend |
| 3063 | case tok::kw_friend: |
John McCall | 07e91c0 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 3064 | if (DSContext == DSC_class) |
| 3065 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 3066 | else { |
| 3067 | PrevSpec = ""; // not actually used by the diagnostic |
| 3068 | DiagID = diag::err_friend_invalid_in_context; |
| 3069 | isInvalid = true; |
| 3070 | } |
Anders Carlsson | cd8db41 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 3071 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3072 | |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 3073 | // Modules |
| 3074 | case tok::kw___module_private__: |
| 3075 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 3076 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3077 | |
Sebastian Redl | 39c2a8b | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 3078 | // constexpr |
| 3079 | case tok::kw_constexpr: |
| 3080 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 3081 | break; |
| 3082 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3083 | // type-specifier |
| 3084 | case tok::kw_short: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3085 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 3086 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3087 | break; |
| 3088 | case tok::kw_long: |
| 3089 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3090 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 3091 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3092 | else |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3093 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 3094 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3095 | break; |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3096 | case tok::kw___int64: |
| 3097 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 3098 | DiagID); |
| 3099 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3100 | case tok::kw_signed: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3101 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 3102 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3103 | break; |
| 3104 | case tok::kw_unsigned: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3105 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 3106 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3107 | break; |
| 3108 | case tok::kw__Complex: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3109 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 3110 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3111 | break; |
| 3112 | case tok::kw__Imaginary: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3113 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 3114 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3115 | break; |
| 3116 | case tok::kw_void: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3117 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 3118 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3119 | break; |
| 3120 | case tok::kw_char: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3121 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 3122 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3123 | break; |
| 3124 | case tok::kw_int: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3125 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 3126 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3127 | break; |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3128 | case tok::kw___int128: |
| 3129 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
| 3130 | DiagID); |
| 3131 | break; |
| 3132 | case tok::kw_half: |
| 3133 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
| 3134 | DiagID); |
| 3135 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3136 | case tok::kw_float: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3137 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 3138 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3139 | break; |
| 3140 | case tok::kw_double: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3141 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 3142 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3143 | break; |
| 3144 | case tok::kw_wchar_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3145 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 3146 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3147 | break; |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3148 | case tok::kw_char16_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3149 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 3150 | DiagID); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3151 | break; |
| 3152 | case tok::kw_char32_t: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3153 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 3154 | DiagID); |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3155 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3156 | case tok::kw_bool: |
| 3157 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3158 | if (Tok.is(tok::kw_bool) && |
| 3159 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 3160 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 3161 | PrevSpec = ""; // Not used by the diagnostic. |
| 3162 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | 2b05999 | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3163 | // For better error recovery. |
| 3164 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 20ee5ae | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3165 | isInvalid = true; |
| 3166 | } else { |
| 3167 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 3168 | DiagID); |
| 3169 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3170 | break; |
| 3171 | case tok::kw__Decimal32: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3172 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 3173 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3174 | break; |
| 3175 | case tok::kw__Decimal64: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3176 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 3177 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3178 | break; |
| 3179 | case tok::kw__Decimal128: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3180 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 3181 | DiagID); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3182 | break; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3183 | case tok::kw___vector: |
| 3184 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 3185 | break; |
| 3186 | case tok::kw___pixel: |
| 3187 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 3188 | break; |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3189 | case tok::kw_image1d_t: |
| 3190 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc, |
| 3191 | PrevSpec, DiagID); |
| 3192 | break; |
| 3193 | case tok::kw_image1d_array_t: |
| 3194 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc, |
| 3195 | PrevSpec, DiagID); |
| 3196 | break; |
| 3197 | case tok::kw_image1d_buffer_t: |
| 3198 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc, |
| 3199 | PrevSpec, DiagID); |
| 3200 | break; |
| 3201 | case tok::kw_image2d_t: |
| 3202 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc, |
| 3203 | PrevSpec, DiagID); |
| 3204 | break; |
| 3205 | case tok::kw_image2d_array_t: |
| 3206 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc, |
| 3207 | PrevSpec, DiagID); |
| 3208 | break; |
| 3209 | case tok::kw_image3d_t: |
| 3210 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc, |
| 3211 | PrevSpec, DiagID); |
| 3212 | break; |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3213 | case tok::kw_sampler_t: |
| 3214 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc, |
| 3215 | PrevSpec, DiagID); |
| 3216 | break; |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3217 | case tok::kw_event_t: |
| 3218 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc, |
| 3219 | PrevSpec, DiagID); |
| 3220 | break; |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3221 | case tok::kw___unknown_anytype: |
| 3222 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 3223 | PrevSpec, DiagID); |
| 3224 | break; |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3225 | |
| 3226 | // class-specifier: |
| 3227 | case tok::kw_class: |
| 3228 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3229 | case tok::kw___interface: |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3230 | case tok::kw_union: { |
| 3231 | tok::TokenKind Kind = Tok.getKind(); |
| 3232 | ConsumeToken(); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3233 | |
| 3234 | // These are attributes following class specifiers. |
| 3235 | // To produce better diagnostic, we parse them when |
| 3236 | // parsing class specifier. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3237 | ParsedAttributesWithRange Attributes(AttrFactory); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3238 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3239 | EnteringContext, DSContext, Attributes); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3240 | |
| 3241 | // If there are attributes following class specifier, |
| 3242 | // take them over and handle them here. |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3243 | if (!Attributes.empty()) { |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3244 | AttrsLastTime = true; |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3245 | attrs.takeAllFrom(Attributes); |
Michael Han | 9407e50 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3246 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3247 | continue; |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3248 | } |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3249 | |
| 3250 | // enum-specifier: |
| 3251 | case tok::kw_enum: |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3252 | ConsumeToken(); |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3253 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3254 | continue; |
| 3255 | |
| 3256 | // cv-qualifier: |
| 3257 | case tok::kw_const: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3258 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3259 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3260 | break; |
| 3261 | case tok::kw_volatile: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3262 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3263 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3264 | break; |
| 3265 | case tok::kw_restrict: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3266 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3267 | getLangOpts()); |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3268 | break; |
| 3269 | |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3270 | // C++ typename-specifier: |
| 3271 | case tok::kw_typename: |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3272 | if (TryAnnotateTypeOrScopeToken()) { |
| 3273 | DS.SetTypeSpecError(); |
| 3274 | goto DoneWithDeclSpec; |
| 3275 | } |
| 3276 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3277 | continue; |
| 3278 | break; |
| 3279 | |
Chris Lattner | e387d9e | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3280 | // GNU typeof support. |
| 3281 | case tok::kw_typeof: |
| 3282 | ParseTypeofSpecifier(DS); |
| 3283 | continue; |
| 3284 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3285 | case tok::annot_decltype: |
Anders Carlsson | 74948d0 | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 3286 | ParseDecltypeSpecifier(DS); |
| 3287 | continue; |
| 3288 | |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3289 | case tok::kw___underlying_type: |
| 3290 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3291 | continue; |
| 3292 | |
| 3293 | case tok::kw__Atomic: |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3294 | // C11 6.7.2.4/4: |
| 3295 | // If the _Atomic keyword is immediately followed by a left parenthesis, |
| 3296 | // it is interpreted as a type specifier (with a type name), not as a |
| 3297 | // type qualifier. |
| 3298 | if (NextToken().is(tok::l_paren)) { |
| 3299 | ParseAtomicSpecifier(DS); |
| 3300 | continue; |
| 3301 | } |
| 3302 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 3303 | getLangOpts()); |
| 3304 | break; |
Alexis Hunt | 4a25707 | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3305 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3306 | // OpenCL qualifiers: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3307 | case tok::kw_private: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3308 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3309 | goto DoneWithDeclSpec; |
| 3310 | case tok::kw___private: |
| 3311 | case tok::kw___global: |
| 3312 | case tok::kw___local: |
| 3313 | case tok::kw___constant: |
| 3314 | case tok::kw___read_only: |
| 3315 | case tok::kw___write_only: |
| 3316 | case tok::kw___read_write: |
| 3317 | ParseOpenCLQualifiers(DS); |
| 3318 | break; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3319 | |
Steve Naroff | cfdf616 | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 3320 | case tok::less: |
Chris Lattner | 16fac4f | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3321 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3322 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 3323 | // but we support it. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3324 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | 0974b23 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3325 | goto DoneWithDeclSpec; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3326 | |
Douglas Gregor | 3a001f4 | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 3327 | if (!ParseObjCProtocolQualifiers(DS)) |
| 3328 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 3329 | << FixItHint::CreateInsertion(Loc, "id") |
| 3330 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3331 | |
Douglas Gregor | 06e41ae | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 3332 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 3333 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 3334 | continue; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3335 | } |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3336 | // If the specifier wasn't legal, issue a diagnostic. |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3337 | if (isInvalid) { |
| 3338 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3339 | assert(DiagID); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3340 | |
Douglas Gregor | a05f5ab | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 3341 | if (DiagID == diag::ext_duplicate_declspec) |
| 3342 | Diag(Tok, DiagID) |
| 3343 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 3344 | else |
| 3345 | Diag(Tok, DiagID) << PrevSpec; |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 3346 | } |
Fariborz Jahanian | bb6db56 | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3347 | |
Chris Lattner | 2e23209 | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 3348 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | 2b05999 | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3349 | if (DiagID != diag::err_bool_redeclaration) |
| 3350 | ConsumeToken(); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3351 | |
| 3352 | AttrsLastTime = false; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 3353 | } |
| 3354 | } |
Douglas Gregor | eb31f39 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 3355 | |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3356 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 3357 | /// semicolon. |
| 3358 | /// |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3359 | /// struct-declaration: |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3360 | /// specifier-qualifier-list struct-declarator-list |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 3361 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3362 | /// [GNU] specifier-qualifier-list |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3363 | /// struct-declarator-list: |
| 3364 | /// struct-declarator |
| 3365 | /// struct-declarator-list ',' struct-declarator |
| 3366 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 3367 | /// struct-declarator: |
| 3368 | /// declarator |
| 3369 | /// [GNU] declarator attributes[opt] |
| 3370 | /// declarator[opt] ':' constant-expression |
| 3371 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 3372 | /// |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3373 | void Parser:: |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3374 | ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3375 | |
Chris Lattner | f02ef3e | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3376 | if (Tok.is(tok::kw___extension__)) { |
| 3377 | // __extension__ silences extension warnings in the subexpression. |
| 3378 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3379 | ConsumeToken(); |
Chris Lattner | f02ef3e | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3380 | return ParseStructDeclaration(DS, Fields); |
| 3381 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3382 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3383 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3384 | ParseSpecifierQualifierList(DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3385 | |
Douglas Gregor | c6f58fe | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3386 | // If there are no declarators, this is a free-standing declaration |
| 3387 | // specifier. Let the actions module cope with it. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3388 | if (Tok.is(tok::semi)) { |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3389 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
| 3390 | DS); |
| 3391 | DS.complete(TheDecl); |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3392 | return; |
| 3393 | } |
| 3394 | |
| 3395 | // Read struct-declarators until we find the semicolon. |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3396 | bool FirstDeclarator = true; |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3397 | SourceLocation CommaLoc; |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3398 | while (1) { |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3399 | ParsingFieldDeclarator DeclaratorInfo(*this, DS); |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3400 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3401 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3402 | // Attributes are only allowed here on successive declarators. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3403 | if (!FirstDeclarator) |
| 3404 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3405 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3406 | /// struct-declarator: declarator |
| 3407 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | 17c3b1f | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3408 | if (Tok.isNot(tok::colon)) { |
| 3409 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 3410 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3411 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | 17c3b1f | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3412 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3413 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3414 | if (Tok.is(tok::colon)) { |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3415 | ConsumeToken(); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3416 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3417 | if (Res.isInvalid()) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3418 | SkipUntil(tok::semi, StopBeforeMatch); |
Chris Lattner | 32295d3 | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 3419 | else |
Sebastian Redl | d9f7b1c | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 3420 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3421 | } |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3422 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3423 | // If attributes exist after the declarator, parse them. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3424 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3425 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3426 | // We're done with this declarator; invoke the callback. |
Eli Friedman | ba01f2b | 2012-08-08 23:35:12 +0000 | [diff] [blame] | 3427 | Fields.invoke(DeclaratorInfo); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3428 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3429 | // If we don't have a comma, it is either the end of the list (a ';') |
| 3430 | // or an error, bail out. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3431 | if (Tok.isNot(tok::comma)) |
Chris Lattner | 70ae491 | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3432 | return; |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3433 | |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3434 | // Consume the comma. |
Richard Smith | 8d06f42 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3435 | CommaLoc = ConsumeToken(); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3436 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3437 | FirstDeclarator = false; |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3438 | } |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3439 | } |
| 3440 | |
| 3441 | /// ParseStructUnionBody |
| 3442 | /// struct-contents: |
| 3443 | /// struct-declaration-list |
| 3444 | /// [EXT] empty |
| 3445 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 3446 | /// struct-declaration-list: |
| 3447 | /// struct-declaration |
| 3448 | /// struct-declaration-list struct-declaration |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3449 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 9717080 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3450 | /// |
Chris Lattner | 1300fb9 | 2007-01-23 23:42:53 +0000 | [diff] [blame] | 3451 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3452 | unsigned TagType, Decl *TagDecl) { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3453 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 3454 | "parsing struct/union body"); |
Andy Gibbs | 22e140b | 2013-04-03 09:31:19 +0000 | [diff] [blame] | 3455 | assert(!getLangOpts().CPlusPlus && "C++ declarations not supported"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3456 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3457 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3458 | if (T.consumeOpen()) |
| 3459 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3460 | |
Douglas Gregor | 658b955 | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3461 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3462 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3463 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3464 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3465 | |
Chris Lattner | 7b9ace6 | 2007-01-23 20:11:08 +0000 | [diff] [blame] | 3466 | // While we still have something to read, read the declarations in the struct. |
Richard Smith | 34f3051 | 2013-11-23 04:06:09 +0000 | [diff] [blame] | 3467 | while (Tok.isNot(tok::r_brace) && !isEofOrEom()) { |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3468 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3469 | |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 3470 | // Check for extraneous top-level semicolon. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3471 | if (Tok.is(tok::semi)) { |
Richard Smith | 87f5dc5 | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3472 | ConsumeExtraSemi(InsideStruct, TagType); |
Chris Lattner | 36e46a2 | 2007-06-09 05:49:55 +0000 | [diff] [blame] | 3473 | continue; |
| 3474 | } |
Chris Lattner | a12405b | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3475 | |
Andy Gibbs | c804e08 | 2013-04-03 09:46:04 +0000 | [diff] [blame] | 3476 | // Parse _Static_assert declaration. |
| 3477 | if (Tok.is(tok::kw__Static_assert)) { |
| 3478 | SourceLocation DeclEnd; |
| 3479 | ParseStaticAssertDeclaration(DeclEnd); |
| 3480 | continue; |
| 3481 | } |
| 3482 | |
Argyrios Kyrtzidis | 71c12fb | 2013-04-18 01:42:35 +0000 | [diff] [blame] | 3483 | if (Tok.is(tok::annot_pragma_pack)) { |
| 3484 | HandlePragmaPack(); |
| 3485 | continue; |
| 3486 | } |
| 3487 | |
| 3488 | if (Tok.is(tok::annot_pragma_align)) { |
| 3489 | HandlePragmaAlign(); |
| 3490 | continue; |
| 3491 | } |
| 3492 | |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3493 | if (!Tok.is(tok::at)) { |
| 3494 | struct CFieldCallback : FieldCallback { |
| 3495 | Parser &P; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3496 | Decl *TagDecl; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3497 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3498 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3499 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3500 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3501 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 3502 | |
Eli Friedman | 934dbbf | 2012-08-08 23:53:27 +0000 | [diff] [blame] | 3503 | void invoke(ParsingFieldDeclarator &FD) { |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3504 | // Install the declarator into the current TagDecl. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3505 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 5e6253b | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 3506 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 3507 | FD.D, FD.BitfieldSize); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3508 | FieldDecls.push_back(Field); |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3509 | FD.complete(Field); |
Douglas Gregor | 66a985d | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3510 | } |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3511 | } Callback(*this, TagDecl, FieldDecls); |
| 3512 | |
Eli Friedman | 89b1f2c | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3513 | // Parse all the comma separated declarators. |
| 3514 | ParsingDeclSpec DS(*this); |
John McCall | cfefb6d | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3515 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3516 | } else { // Handle @defs |
| 3517 | ConsumeToken(); |
| 3518 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 3519 | Diag(Tok, diag::err_unexpected_at); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3520 | SkipUntil(tok::semi); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3521 | continue; |
| 3522 | } |
| 3523 | ConsumeToken(); |
| 3524 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 3525 | if (!Tok.is(tok::identifier)) { |
| 3526 | Diag(Tok, diag::err_expected_ident); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3527 | SkipUntil(tok::semi); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3528 | continue; |
| 3529 | } |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3530 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3531 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 91f8421 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3532 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 535b830 | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3533 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 3534 | ConsumeToken(); |
| 3535 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3536 | } |
Chris Lattner | 736ed5d | 2007-06-09 05:59:07 +0000 | [diff] [blame] | 3537 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3538 | if (Tok.is(tok::semi)) { |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3539 | ConsumeToken(); |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3540 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 245c533 | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3541 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Chris Lattner | 0c7e82d | 2007-06-09 05:54:40 +0000 | [diff] [blame] | 3542 | break; |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3543 | } else { |
Chris Lattner | 245c533 | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3544 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 3545 | // Skip to end of block or statement to avoid ext-warning on extra ';'. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3546 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Chris Lattner | 245c533 | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3547 | // If we stopped at a ';', eat it. |
| 3548 | if (Tok.is(tok::semi)) ConsumeToken(); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3549 | } |
| 3550 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3551 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3552 | T.consumeClose(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3553 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3554 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3555 | // If attributes exist after struct contents, parse them. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3556 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 15619c7 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 3557 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3558 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 751c558 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 3559 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3560 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3561 | attrs.getList()); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3562 | StructScope.Exit(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3563 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 3564 | T.getCloseLocation()); |
Chris Lattner | 90a26b0 | 2007-01-23 04:38:16 +0000 | [diff] [blame] | 3565 | } |
| 3566 | |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3567 | /// ParseEnumSpecifier |
Chris Lattner | 1890ac8 | 2006-08-13 01:16:23 +0000 | [diff] [blame] | 3568 | /// enum-specifier: [C99 6.7.2.2] |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3569 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3570 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 3571 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3572 | /// '}' attributes[opt] |
Aaron Ballman | 9ecff02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 3573 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3574 | /// '}' |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3575 | /// 'enum' identifier |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 3576 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3577 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3578 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 3579 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3580 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3581 | /// enum-head: [C++11] |
| 3582 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 3583 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 3584 | /// identifier enum-base[opt] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3585 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3586 | /// enum-key: [C++11] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3587 | /// 'enum' |
| 3588 | /// 'enum' 'class' |
| 3589 | /// 'enum' 'struct' |
| 3590 | /// |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3591 | /// enum-base: [C++11] |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3592 | /// ':' type-specifier-seq |
| 3593 | /// |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3594 | /// [C++] elaborated-type-specifier: |
| 3595 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 3596 | /// |
Chris Lattner | ffaa0e6 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3597 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | dc70c3a | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 3598 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | c5b0552 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3599 | AccessSpecifier AS, DeclSpecContext DSC) { |
Chris Lattner | ffbc271 | 2007-01-25 06:05:38 +0000 | [diff] [blame] | 3600 | // Parse the tag portion of this. |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3601 | if (Tok.is(tok::code_completion)) { |
| 3602 | // Code completion for an enum name. |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3603 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3604 | return cutOffParsing(); |
Douglas Gregor | f45b0cf | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3605 | } |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3606 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3607 | // If attributes exist after tag, parse them. |
| 3608 | ParsedAttributesWithRange attrs(AttrFactory); |
| 3609 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3610 | MaybeParseCXX11Attributes(attrs); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3611 | |
| 3612 | // If declspecs exist after tag, parse them. |
| 3613 | while (Tok.is(tok::kw___declspec)) |
| 3614 | ParseMicrosoftDeclSpec(attrs); |
| 3615 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3616 | SourceLocation ScopedEnumKWLoc; |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3617 | bool IsScopedUsingClassTag = false; |
| 3618 | |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3619 | // In C++11, recognize 'enum class' and 'enum struct'. |
Richard Trieu | d0d87b5 | 2013-04-23 02:47:36 +0000 | [diff] [blame] | 3620 | if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) { |
| 3621 | Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum |
| 3622 | : diag::ext_scoped_enum); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3623 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3624 | ScopedEnumKWLoc = ConsumeToken(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3625 | |
Bill Wendling | 4442605 | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3626 | // Attributes are not allowed between these keywords. Diagnose, |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3627 | // but then just treat them like they appeared in the right place. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3628 | ProhibitAttributes(attrs); |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3629 | |
| 3630 | // They are allowed afterwards, though. |
| 3631 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3632 | MaybeParseCXX11Attributes(attrs); |
John McCall | beae29a | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3633 | while (Tok.is(tok::kw___declspec)) |
| 3634 | ParseMicrosoftDeclSpec(attrs); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3635 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3636 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3637 | // C++11 [temp.explicit]p12: |
| 3638 | // The usual access controls do not apply to names used to specify |
| 3639 | // explicit instantiations. |
| 3640 | // We extend this to also cover explicit specializations. Note that |
| 3641 | // we don't suppress if this turns out to be an elaborated type |
| 3642 | // specifier. |
| 3643 | bool shouldDelayDiagsInTag = |
| 3644 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 3645 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 3646 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3647 | |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3648 | // Enum definitions should not be parsed in a trailing-return-type. |
| 3649 | bool AllowDeclaration = DSC != DSC_trailing; |
| 3650 | |
| 3651 | bool AllowFixedUnderlyingType = AllowDeclaration && |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3652 | (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt || |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3653 | getLangOpts().ObjC2); |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3654 | |
Abramo Bagnara | d754848 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3655 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3656 | if (getLangOpts().CPlusPlus) { |
John McCall | cb432fa | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3657 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 3658 | // if a fixed underlying type is allowed. |
| 3659 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3660 | |
| 3661 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Richard Smith | 1d4b2e1 | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 3662 | /*EnteringContext=*/true)) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3663 | return; |
| 3664 | |
| 3665 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3666 | Diag(Tok, diag::err_expected_ident); |
| 3667 | if (Tok.isNot(tok::l_brace)) { |
| 3668 | // Has no name and is not a definition. |
| 3669 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3670 | SkipUntil(tok::comma, StopAtSemi); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3671 | return; |
| 3672 | } |
| 3673 | } |
| 3674 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3675 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3676 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3677 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3678 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3679 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3680 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3681 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3682 | SkipUntil(tok::comma, StopAtSemi); |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3683 | return; |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3684 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3685 | |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3686 | // If an identifier is present, consume and remember it. |
| 3687 | IdentifierInfo *Name = 0; |
| 3688 | SourceLocation NameLoc; |
| 3689 | if (Tok.is(tok::identifier)) { |
| 3690 | Name = Tok.getIdentifierInfo(); |
| 3691 | NameLoc = ConsumeToken(); |
| 3692 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3693 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3694 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3695 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 3696 | // declaration of a scoped enumeration. |
| 3697 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3698 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3699 | IsScopedUsingClassTag = false; |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3700 | } |
| 3701 | |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3702 | // Okay, end the suppression area. We'll decide whether to emit the |
| 3703 | // diagnostics in a second. |
| 3704 | if (shouldDelayDiagsInTag) |
| 3705 | diagsFromTag.done(); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3706 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3707 | TypeResult BaseType; |
| 3708 | |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3709 | // Parse the fixed underlying type. |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3710 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3711 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3712 | bool PossibleBitfield = false; |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3713 | if (CanBeBitfield) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3714 | // If we're in class scope, this can either be an enum declaration with |
| 3715 | // an underlying type, or a declaration of a bitfield member. We try to |
| 3716 | // use a simple disambiguation scheme first to catch the common cases |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3717 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 3718 | // anything that's a simple-type-specifier followed by '(' as an |
| 3719 | // expression. This suffices because function types are not valid |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3720 | // underlying types anyway. |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 3721 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 3722 | Sema::ConstantEvaluated); |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3723 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3724 | // If the next token starts an expression, we know we're parsing a |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3725 | // bit-field. This is the common case. |
| 3726 | if (TPR == TPResult::True()) |
| 3727 | PossibleBitfield = true; |
| 3728 | // If the next token starts a type-specifier-seq, it may be either a |
| 3729 | // a fixed underlying type or the start of a function-style cast in C++; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3730 | // lookahead one more token to see if it's obvious that we have a |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3731 | // fixed underlying type. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3732 | else if (TPR == TPResult::False() && |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3733 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 3734 | // Consume the ':'. |
| 3735 | ConsumeToken(); |
| 3736 | } else { |
| 3737 | // We have the start of a type-specifier-seq, so we have to perform |
| 3738 | // tentative parsing to determine whether we have an expression or a |
| 3739 | // type. |
| 3740 | TentativeParsingAction TPA(*this); |
| 3741 | |
| 3742 | // Consume the ':'. |
| 3743 | ConsumeToken(); |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3744 | |
| 3745 | // If we see a type specifier followed by an open-brace, we have an |
| 3746 | // ambiguity between an underlying type and a C++11 braced |
| 3747 | // function-style cast. Resolve this by always treating it as an |
| 3748 | // underlying type. |
| 3749 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 3750 | // this case. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3751 | if ((getLangOpts().CPlusPlus && |
Richard Smith | 1e3b0f0 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3752 | isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3753 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3754 | // We'll parse this as a bitfield later. |
| 3755 | PossibleBitfield = true; |
| 3756 | TPA.Revert(); |
| 3757 | } else { |
| 3758 | // We have a type-specifier-seq. |
| 3759 | TPA.Commit(); |
| 3760 | } |
| 3761 | } |
| 3762 | } else { |
| 3763 | // Consume the ':'. |
| 3764 | ConsumeToken(); |
| 3765 | } |
| 3766 | |
| 3767 | if (!PossibleBitfield) { |
| 3768 | SourceRange Range; |
| 3769 | BaseType = ParseTypeName(&Range); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3770 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3771 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3772 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Eli Friedman | 0d0355ab | 2012-11-02 01:34:28 +0000 | [diff] [blame] | 3773 | } else if (!getLangOpts().ObjC2) { |
| 3774 | if (getLangOpts().CPlusPlus) |
| 3775 | Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range; |
| 3776 | else |
| 3777 | Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range; |
| 3778 | } |
Douglas Gregor | d1f69f6 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3779 | } |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3780 | } |
| 3781 | |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3782 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 3783 | // friend declaration, and cannot have an accompanying definition. If we have |
| 3784 | // 'enum foo;', then this is a forward declaration. If we have |
| 3785 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 3786 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | f01fa82 | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3787 | // |
| 3788 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 3789 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 3790 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 3791 | // |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3792 | Sema::TagUseKind TUK; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3793 | if (!AllowDeclaration) { |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3794 | TUK = Sema::TUK_Reference; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3795 | } else if (Tok.is(tok::l_brace)) { |
| 3796 | if (DS.isFriendSpecified()) { |
| 3797 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
| 3798 | << SourceRange(DS.getFriendSpecLoc()); |
| 3799 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3800 | SkipUntil(tok::r_brace, StopAtSemi); |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3801 | TUK = Sema::TUK_Friend; |
| 3802 | } else { |
| 3803 | TUK = Sema::TUK_Definition; |
| 3804 | } |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3805 | } else if (DSC != DSC_type_specifier && |
| 3806 | (Tok.is(tok::semi) || |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3807 | (Tok.isAtStartOfLine() && |
| 3808 | !isValidAfterTypeSpecifier(CanBeBitfield)))) { |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3809 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
| 3810 | if (Tok.isNot(tok::semi)) { |
| 3811 | // A semicolon was missing after this declaration. Diagnose and recover. |
| 3812 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, |
| 3813 | "enum"); |
| 3814 | PP.EnterToken(Tok); |
| 3815 | Tok.setKind(tok::semi); |
| 3816 | } |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3817 | } else { |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3818 | TUK = Sema::TUK_Reference; |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3819 | } |
| 3820 | |
| 3821 | // If this is an elaborated type specifier, and we delayed |
| 3822 | // diagnostics before, just merge them into the current pool. |
| 3823 | if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) { |
| 3824 | diagsFromTag.redelay(); |
| 3825 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3826 | |
| 3827 | MultiTemplateParamsArg TParams; |
Douglas Gregor | cbbf3e3 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3828 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | faf5fb4 | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3829 | TUK != Sema::TUK_Reference) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3830 | if (!getLangOpts().CPlusPlus11 || !SS.isSet()) { |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3831 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3832 | Diag(Tok, diag::err_enum_template); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3833 | SkipUntil(tok::comma, StopAtSemi); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3834 | return; |
| 3835 | } |
| 3836 | |
| 3837 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 3838 | // Enumerations can't be explicitly instantiated. |
| 3839 | DS.SetTypeSpecError(); |
| 3840 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 3841 | return; |
| 3842 | } |
| 3843 | |
| 3844 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 3845 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 3846 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | cbbf3e3 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3847 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3848 | |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3849 | if (TUK == Sema::TUK_Reference) |
| 3850 | ProhibitAttributes(attrs); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3851 | |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3852 | if (!Name && TUK != Sema::TUK_Definition) { |
| 3853 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3854 | |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3855 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3856 | SkipUntil(tok::comma, StopAtSemi); |
Douglas Gregor | 6cd5ae4 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3857 | return; |
| 3858 | } |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3859 | |
Douglas Gregor | d6ab874 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3860 | bool Owned = false; |
John McCall | 7f41d98 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3861 | bool IsDependent = false; |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3862 | const char *PrevSpec = 0; |
| 3863 | unsigned DiagID; |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3864 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3865 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Richard Smith | 7d137e3 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3866 | AS, DS.getModulePrivateSpecLoc(), TParams, |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3867 | Owned, IsDependent, ScopedEnumKWLoc, |
Abramo Bagnara | 0e05e24 | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3868 | IsScopedUsingClassTag, BaseType); |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3869 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3870 | if (IsDependent) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3871 | // This enum has a dependent nested-name-specifier. Handle it as a |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3872 | // dependent tag. |
| 3873 | if (!Name) { |
| 3874 | DS.SetTypeSpecError(); |
| 3875 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 3876 | return; |
| 3877 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3878 | |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3879 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3880 | TUK, SS, Name, StartLoc, |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3881 | NameLoc); |
| 3882 | if (Type.isInvalid()) { |
| 3883 | DS.SetTypeSpecError(); |
| 3884 | return; |
| 3885 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3886 | |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3887 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 3888 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3889 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3890 | Diag(StartLoc, DiagID) << PrevSpec; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3891 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3892 | return; |
| 3893 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3894 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3895 | if (!TagDecl) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3896 | // The action failed to produce an enumeration tag. If this is a |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3897 | // definition, consume the entire definition. |
Richard Smith | bfdb108 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3898 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3899 | ConsumeBrace(); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3900 | SkipUntil(tok::r_brace, StopAtSemi); |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3901 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3902 | |
Douglas Gregor | ba41d01 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3903 | DS.SetTypeSpecError(); |
| 3904 | return; |
| 3905 | } |
Richard Smith | 0f8ee22 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3906 | |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3907 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) |
John McCall | 6347b68 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3908 | ParseEnumBody(StartLoc, TagDecl); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3909 | |
Abramo Bagnara | 9875a3c | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3910 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3911 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3912 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3913 | Diag(StartLoc, DiagID) << PrevSpec; |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 3914 | } |
| 3915 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3916 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3917 | /// enumerator-list: |
| 3918 | /// enumerator |
| 3919 | /// enumerator-list ',' enumerator |
| 3920 | /// enumerator: |
| 3921 | /// enumeration-constant |
| 3922 | /// enumeration-constant '=' constant-expression |
| 3923 | /// enumeration-constant: |
| 3924 | /// identifier |
| 3925 | /// |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3926 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3927 | // Enter the scope of the enum body and start the definition. |
| 3928 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3929 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 07665a6 | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3930 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3931 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3932 | T.consumeOpen(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3933 | |
Chris Lattner | 37256fb | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3934 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3935 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 6e81492 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3936 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3937 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3938 | SmallVector<Decl *, 32> EnumConstantDecls; |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3939 | |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3940 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3941 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3942 | // Parse the enumerator-list. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3943 | while (Tok.is(tok::identifier)) { |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3944 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3945 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3946 | |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3947 | // If attributes exist after the enumerator, parse them. |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3948 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3949 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3950 | MaybeParseCXX11Attributes(attrs); |
Alexis Hunt | 6aa9bee | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3951 | ProhibitAttributes(attrs); |
John McCall | 811a0f5 | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3952 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3953 | SourceLocation EqualLoc; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3954 | ExprResult AssignedVal; |
John McCall | 2ec8537 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 3955 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3956 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3957 | if (Tok.is(tok::equal)) { |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3958 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3959 | AssignedVal = ParseConstantExpression(); |
| 3960 | if (AssignedVal.isInvalid()) |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3961 | SkipUntil(tok::comma, tok::r_brace, StopAtSemi | StopBeforeMatch); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3962 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3963 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3964 | // Install the enumerator constant into EnumDecl. |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3965 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3966 | LastEnumConstDecl, |
| 3967 | IdentLoc, Ident, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3968 | attrs.getList(), EqualLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3969 | AssignedVal.release()); |
Fariborz Jahanian | 329b351 | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3970 | PD.complete(EnumConstDecl); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3971 | |
Chris Lattner | 4ef4001 | 2007-06-11 01:28:17 +0000 | [diff] [blame] | 3972 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3973 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3974 | |
Douglas Gregor | ce66d02 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3975 | if (Tok.is(tok::identifier)) { |
| 3976 | // We're missing a comma between enumerators. |
| 3977 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3978 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
Douglas Gregor | ce66d02 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3979 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3980 | continue; |
| 3981 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3982 | |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3983 | if (Tok.isNot(tok::comma)) |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3984 | break; |
| 3985 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3986 | |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3987 | if (Tok.isNot(tok::identifier)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3988 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11) |
Richard Smith | 87f5dc5 | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3989 | Diag(CommaLoc, getLangOpts().CPlusPlus ? |
| 3990 | diag::ext_enumerator_list_comma_cxx : |
| 3991 | diag::ext_enumerator_list_comma_c) |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3992 | << FixItHint::CreateRemoval(CommaLoc); |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3993 | else if (getLangOpts().CPlusPlus11) |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3994 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3995 | << FixItHint::CreateRemoval(CommaLoc); |
| 3996 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3997 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3998 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 3999 | // Eat the }. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4000 | T.consumeClose(); |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4001 | |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4002 | // If attributes exist after the identifier list, parse them. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4003 | ParsedAttributes attrs(AttrFactory); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4004 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 4005 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4006 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
Dmitri Gribenko | e5fde99 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 4007 | EnumDecl, EnumConstantDecls, |
| 4008 | getCurScope(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4009 | attrs.getList()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4010 | |
Douglas Gregor | 82ac25e | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 4011 | EnumScope.Exit(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4012 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 4013 | T.getCloseLocation()); |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4014 | |
| 4015 | // The next token must be valid after an enum definition. If not, a ';' |
| 4016 | // was probably forgotten. |
Richard Smith | 200f47c | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 4017 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
| 4018 | if (!isValidAfterTypeSpecifier(CanBeBitfield)) { |
Richard Smith | 369b9f9 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 4019 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum"); |
| 4020 | // Push this token back into the preprocessor and change our current token |
| 4021 | // to ';' so that the rest of the code recovers as though there were an |
| 4022 | // ';' after the definition. |
| 4023 | PP.EnterToken(Tok); |
| 4024 | Tok.setKind(tok::semi); |
| 4025 | } |
Chris Lattner | c1915e2 | 2007-01-25 07:29:02 +0000 | [diff] [blame] | 4026 | } |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 4027 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4028 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 4029 | /// start of a type-qualifier-list. |
| 4030 | bool Parser::isTypeQualifier() const { |
| 4031 | switch (Tok.getKind()) { |
| 4032 | default: return false; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4033 | |
| 4034 | // type-qualifier only in OpenCL |
| 4035 | case tok::kw_private: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4036 | return getLangOpts().OpenCL; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4037 | |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 4038 | // type-qualifier |
| 4039 | case tok::kw_const: |
| 4040 | case tok::kw_volatile: |
| 4041 | case tok::kw_restrict: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4042 | case tok::kw___private: |
| 4043 | case tok::kw___local: |
| 4044 | case tok::kw___global: |
| 4045 | case tok::kw___constant: |
| 4046 | case tok::kw___read_only: |
| 4047 | case tok::kw___read_write: |
| 4048 | case tok::kw___write_only: |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 4049 | return true; |
| 4050 | } |
| 4051 | } |
| 4052 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4053 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 4054 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 4055 | /// specifier or if we're not sure. |
| 4056 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 4057 | switch (Tok.getKind()) { |
| 4058 | default: return false; |
| 4059 | // type-specifiers |
| 4060 | case tok::kw_short: |
| 4061 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4062 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4063 | case tok::kw___int128: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4064 | case tok::kw_signed: |
| 4065 | case tok::kw_unsigned: |
| 4066 | case tok::kw__Complex: |
| 4067 | case tok::kw__Imaginary: |
| 4068 | case tok::kw_void: |
| 4069 | case tok::kw_char: |
| 4070 | case tok::kw_wchar_t: |
| 4071 | case tok::kw_char16_t: |
| 4072 | case tok::kw_char32_t: |
| 4073 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4074 | case tok::kw_half: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4075 | case tok::kw_float: |
| 4076 | case tok::kw_double: |
| 4077 | case tok::kw_bool: |
| 4078 | case tok::kw__Bool: |
| 4079 | case tok::kw__Decimal32: |
| 4080 | case tok::kw__Decimal64: |
| 4081 | case tok::kw__Decimal128: |
| 4082 | case tok::kw___vector: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4083 | |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 4084 | // OpenCL specific types: |
| 4085 | case tok::kw_image1d_t: |
| 4086 | case tok::kw_image1d_array_t: |
| 4087 | case tok::kw_image1d_buffer_t: |
| 4088 | case tok::kw_image2d_t: |
| 4089 | case tok::kw_image2d_array_t: |
| 4090 | case tok::kw_image3d_t: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4091 | case tok::kw_sampler_t: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4092 | case tok::kw_event_t: |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 4093 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4094 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4095 | case tok::kw_class: |
| 4096 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4097 | case tok::kw___interface: |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4098 | case tok::kw_union: |
| 4099 | // enum-specifier |
| 4100 | case tok::kw_enum: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4101 | |
Chris Lattner | fd48afe | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 4102 | // typedef-name |
| 4103 | case tok::annot_typename: |
| 4104 | return true; |
| 4105 | } |
| 4106 | } |
| 4107 | |
Steve Naroff | 69e8f9e | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 4108 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4109 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4110 | bool Parser::isTypeSpecifierQualifier() { |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4111 | switch (Tok.getKind()) { |
| 4112 | default: return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4113 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4114 | case tok::identifier: // foo::bar |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4115 | if (TryAltiVecVectorToken()) |
| 4116 | return true; |
| 4117 | // Fall through. |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4118 | case tok::kw_typename: // typename T::type |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4119 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4120 | // recurse to handle whatever we get. |
| 4121 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4122 | return true; |
| 4123 | if (Tok.is(tok::identifier)) |
| 4124 | return false; |
| 4125 | return isTypeSpecifierQualifier(); |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4126 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4127 | case tok::coloncolon: // ::foo::bar |
| 4128 | if (NextToken().is(tok::kw_new) || // ::new |
| 4129 | NextToken().is(tok::kw_delete)) // ::delete |
| 4130 | return false; |
| 4131 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4132 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4133 | return true; |
| 4134 | return isTypeSpecifierQualifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4135 | |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 4136 | // GNU attributes support. |
| 4137 | case tok::kw___attribute: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4138 | // GNU typeof support. |
| 4139 | case tok::kw_typeof: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4140 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4141 | // type-specifiers |
| 4142 | case tok::kw_short: |
| 4143 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4144 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4145 | case tok::kw___int128: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4146 | case tok::kw_signed: |
| 4147 | case tok::kw_unsigned: |
| 4148 | case tok::kw__Complex: |
| 4149 | case tok::kw__Imaginary: |
| 4150 | case tok::kw_void: |
| 4151 | case tok::kw_char: |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4152 | case tok::kw_wchar_t: |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4153 | case tok::kw_char16_t: |
| 4154 | case tok::kw_char32_t: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4155 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4156 | case tok::kw_half: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4157 | case tok::kw_float: |
| 4158 | case tok::kw_double: |
Chris Lattner | bb31a42 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4159 | case tok::kw_bool: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4160 | case tok::kw__Bool: |
| 4161 | case tok::kw__Decimal32: |
| 4162 | case tok::kw__Decimal64: |
| 4163 | case tok::kw__Decimal128: |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4164 | case tok::kw___vector: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4165 | |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 4166 | // OpenCL specific types: |
| 4167 | case tok::kw_image1d_t: |
| 4168 | case tok::kw_image1d_array_t: |
| 4169 | case tok::kw_image1d_buffer_t: |
| 4170 | case tok::kw_image2d_t: |
| 4171 | case tok::kw_image2d_array_t: |
| 4172 | case tok::kw_image3d_t: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4173 | case tok::kw_sampler_t: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4174 | case tok::kw_event_t: |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 4175 | |
Chris Lattner | 861a226 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4176 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4177 | case tok::kw_class: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4178 | case tok::kw_struct: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4179 | case tok::kw___interface: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4180 | case tok::kw_union: |
| 4181 | // enum-specifier |
| 4182 | case tok::kw_enum: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4183 | |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4184 | // type-qualifier |
| 4185 | case tok::kw_const: |
| 4186 | case tok::kw_volatile: |
| 4187 | case tok::kw_restrict: |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4188 | |
John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4189 | // Debugger support. |
| 4190 | case tok::kw___unknown_anytype: |
| 4191 | |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4192 | // typedef-name |
Chris Lattner | a8a3f73 | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 4193 | case tok::annot_typename: |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4194 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4195 | |
Chris Lattner | 409bf7d | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 4196 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4197 | case tok::less: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4198 | return getLangOpts().ObjC1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4199 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4200 | case tok::kw___cdecl: |
| 4201 | case tok::kw___stdcall: |
| 4202 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4203 | case tok::kw___thiscall: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4204 | case tok::kw___w64: |
| 4205 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4206 | case tok::kw___ptr32: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4207 | case tok::kw___pascal: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4208 | case tok::kw___unaligned: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4209 | |
| 4210 | case tok::kw___private: |
| 4211 | case tok::kw___local: |
| 4212 | case tok::kw___global: |
| 4213 | case tok::kw___constant: |
| 4214 | case tok::kw___read_only: |
| 4215 | case tok::kw___read_write: |
| 4216 | case tok::kw___write_only: |
| 4217 | |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4218 | return true; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4219 | |
| 4220 | case tok::kw_private: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4221 | return getLangOpts().OpenCL; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4222 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4223 | // C11 _Atomic |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4224 | case tok::kw__Atomic: |
| 4225 | return true; |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 4226 | } |
| 4227 | } |
| 4228 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4229 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 4230 | /// declaration specifier. |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4231 | /// |
| 4232 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 4233 | /// this check is to disambiguate between an expression and a declaration. |
| 4234 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4235 | switch (Tok.getKind()) { |
| 4236 | default: return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4237 | |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4238 | case tok::kw_private: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4239 | return getLangOpts().OpenCL; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4240 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4241 | case tok::identifier: // foo::bar |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4242 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4243 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 9527bbf | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4244 | return false; |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4245 | if (TryAltiVecVectorToken()) |
| 4246 | return true; |
| 4247 | // Fall through. |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4248 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | 333489b | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4249 | case tok::kw_typename: // typename T::type |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4250 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4251 | // recurse to handle whatever we get. |
| 4252 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4253 | return true; |
| 4254 | if (Tok.is(tok::identifier)) |
| 4255 | return false; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4256 | |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4257 | // If we're in Objective-C and we have an Objective-C class type followed |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4258 | // by an identifier and then either ':' or ']', in a place where an |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4259 | // expression is permitted, then this is probably a class message send |
| 4260 | // missing the initial '['. In this case, we won't consider this to be |
| 4261 | // the start of a declaration. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4262 | if (DisambiguatingWithExpression && |
Douglas Gregor | abf4a3e | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4263 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 4264 | return false; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4265 | |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4266 | return isDeclarationSpecifier(); |
| 4267 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4268 | case tok::coloncolon: // ::foo::bar |
| 4269 | if (NextToken().is(tok::kw_new) || // ::new |
| 4270 | NextToken().is(tok::kw_delete)) // ::delete |
| 4271 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4272 | |
Chris Lattner | 020bab9 | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4273 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4274 | // recurse to handle whatever we get. |
| 4275 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4276 | return true; |
| 4277 | return isDeclarationSpecifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4278 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4279 | // storage-class-specifier |
| 4280 | case tok::kw_typedef: |
| 4281 | case tok::kw_extern: |
Steve Naroff | 2050b0d | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 4282 | case tok::kw___private_extern__: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4283 | case tok::kw_static: |
| 4284 | case tok::kw_auto: |
| 4285 | case tok::kw_register: |
| 4286 | case tok::kw___thread: |
Richard Smith | b4a9e86 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4287 | case tok::kw_thread_local: |
| 4288 | case tok::kw__Thread_local: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4289 | |
Douglas Gregor | 26701a4 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 4290 | // Modules |
| 4291 | case tok::kw___module_private__: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4292 | |
John McCall | ea0a39e | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4293 | // Debugger support |
| 4294 | case tok::kw___unknown_anytype: |
| 4295 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4296 | // type-specifiers |
| 4297 | case tok::kw_short: |
| 4298 | case tok::kw_long: |
Francois Pichet | 84133e4 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4299 | case tok::kw___int64: |
Richard Smith | f016bbc | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4300 | case tok::kw___int128: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4301 | case tok::kw_signed: |
| 4302 | case tok::kw_unsigned: |
| 4303 | case tok::kw__Complex: |
| 4304 | case tok::kw__Imaginary: |
| 4305 | case tok::kw_void: |
| 4306 | case tok::kw_char: |
Argyrios Kyrtzidis | 40e9e48 | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4307 | case tok::kw_wchar_t: |
Alisdair Meredith | a9ad47d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4308 | case tok::kw_char16_t: |
| 4309 | case tok::kw_char32_t: |
| 4310 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4311 | case tok::kw_int: |
Anton Korobeynikov | f0c267e | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4312 | case tok::kw_half: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4313 | case tok::kw_float: |
| 4314 | case tok::kw_double: |
Chris Lattner | bb31a42 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4315 | case tok::kw_bool: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4316 | case tok::kw__Bool: |
| 4317 | case tok::kw__Decimal32: |
| 4318 | case tok::kw__Decimal64: |
| 4319 | case tok::kw__Decimal128: |
John Thompson | 2233460 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4320 | case tok::kw___vector: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4321 | |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 4322 | // OpenCL specific types: |
| 4323 | case tok::kw_image1d_t: |
| 4324 | case tok::kw_image1d_array_t: |
| 4325 | case tok::kw_image1d_buffer_t: |
| 4326 | case tok::kw_image2d_t: |
| 4327 | case tok::kw_image2d_array_t: |
| 4328 | case tok::kw_image3d_t: |
Guy Benyei | 6105419 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4329 | case tok::kw_sampler_t: |
Guy Benyei | 1b4fb3e | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4330 | case tok::kw_event_t: |
Guy Benyei | d8a08ea | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 4331 | |
Chris Lattner | 861a226 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4332 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4333 | case tok::kw_class: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4334 | case tok::kw_struct: |
| 4335 | case tok::kw_union: |
Joao Matos | dc86f94 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4336 | case tok::kw___interface: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4337 | // enum-specifier |
| 4338 | case tok::kw_enum: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4339 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4340 | // type-qualifier |
| 4341 | case tok::kw_const: |
| 4342 | case tok::kw_volatile: |
| 4343 | case tok::kw_restrict: |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4344 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4345 | // function-specifier |
| 4346 | case tok::kw_inline: |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4347 | case tok::kw_virtual: |
| 4348 | case tok::kw_explicit: |
Richard Smith | 0015f09 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4349 | case tok::kw__Noreturn: |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4350 | |
Richard Smith | 1dba27c | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 4351 | // alignment-specifier |
| 4352 | case tok::kw__Alignas: |
| 4353 | |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4354 | // friend keyword. |
| 4355 | case tok::kw_friend: |
| 4356 | |
Peter Collingbourne | 3d9cbdc | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 4357 | // static_assert-declaration |
| 4358 | case tok::kw__Static_assert: |
| 4359 | |
Chris Lattner | 599e47e | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4360 | // GNU typeof support. |
| 4361 | case tok::kw_typeof: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4362 | |
Chris Lattner | 599e47e | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4363 | // GNU attributes. |
Chris Lattner | 7b20dc7 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4364 | case tok::kw___attribute: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4365 | |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4366 | // C++11 decltype and constexpr. |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4367 | case tok::annot_decltype: |
Richard Smith | d16fe12 | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4368 | case tok::kw_constexpr: |
Francois Pichet | e878cb6 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 4369 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4370 | // C11 _Atomic |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4371 | case tok::kw__Atomic: |
| 4372 | return true; |
| 4373 | |
Chris Lattner | 8b2ec16 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 4374 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4375 | case tok::less: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4376 | return getLangOpts().ObjC1; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4377 | |
Douglas Gregor | 19b7acf | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 4378 | // typedef-name |
| 4379 | case tok::annot_typename: |
| 4380 | return !DisambiguatingWithExpression || |
| 4381 | !isStartOfObjCClassMessageMissingOpenBracket(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4382 | |
Steve Naroff | f192fab | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 4383 | case tok::kw___declspec: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4384 | case tok::kw___cdecl: |
| 4385 | case tok::kw___stdcall: |
| 4386 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4387 | case tok::kw___thiscall: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4388 | case tok::kw___w64: |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4389 | case tok::kw___sptr: |
| 4390 | case tok::kw___uptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4391 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4392 | case tok::kw___ptr32: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4393 | case tok::kw___forceinline: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4394 | case tok::kw___pascal: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4395 | case tok::kw___unaligned: |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4396 | |
| 4397 | case tok::kw___private: |
| 4398 | case tok::kw___local: |
| 4399 | case tok::kw___global: |
| 4400 | case tok::kw___constant: |
| 4401 | case tok::kw___read_only: |
| 4402 | case tok::kw___read_write: |
| 4403 | case tok::kw___write_only: |
| 4404 | |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4405 | return true; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4406 | } |
| 4407 | } |
| 4408 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4409 | bool Parser::isConstructorDeclarator() { |
| 4410 | TentativeParsingAction TPA(*this); |
| 4411 | |
| 4412 | // Parse the C++ scope specifier. |
| 4413 | CXXScopeSpec SS; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4414 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4415 | /*EnteringContext=*/true)) { |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4416 | TPA.Revert(); |
| 4417 | return false; |
| 4418 | } |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4419 | |
| 4420 | // Parse the constructor name. |
| 4421 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 4422 | // We already know that we have a constructor name; just consume |
| 4423 | // the token. |
| 4424 | ConsumeToken(); |
| 4425 | } else { |
| 4426 | TPA.Revert(); |
| 4427 | return false; |
| 4428 | } |
| 4429 | |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4430 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4431 | if (Tok.isNot(tok::l_paren)) { |
| 4432 | TPA.Revert(); |
| 4433 | return false; |
| 4434 | } |
| 4435 | ConsumeParen(); |
| 4436 | |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4437 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 4438 | // that we have a constructor. |
| 4439 | if (Tok.is(tok::r_paren) || |
| 4440 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4441 | TPA.Revert(); |
| 4442 | return true; |
| 4443 | } |
| 4444 | |
Richard Smith | f216366 | 2013-09-06 00:12:20 +0000 | [diff] [blame] | 4445 | // A C++11 attribute here signals that we have a constructor, and is an |
| 4446 | // attribute on the first constructor parameter. |
| 4447 | if (getLangOpts().CPlusPlus11 && |
| 4448 | isCXX11AttributeSpecifier(/*Disambiguate*/ false, |
| 4449 | /*OuterMightBeMessageSend*/ true)) { |
| 4450 | TPA.Revert(); |
| 4451 | return true; |
| 4452 | } |
| 4453 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4454 | // If we need to, enter the specified scope. |
| 4455 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4456 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4457 | DeclScopeObj.EnterDeclaratorScope(); |
| 4458 | |
Francois Pichet | 79f3a87 | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4459 | // Optionally skip Microsoft attributes. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4460 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | 79f3a87 | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4461 | MaybeParseMicrosoftAttributes(Attrs); |
| 4462 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4463 | // Check whether the next token(s) are part of a declaration |
| 4464 | // specifier, in which case we have the start of a parameter and, |
| 4465 | // therefore, we know that this is a constructor. |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4466 | bool IsConstructor = false; |
| 4467 | if (isDeclarationSpecifier()) |
| 4468 | IsConstructor = true; |
| 4469 | else if (Tok.is(tok::identifier) || |
| 4470 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 4471 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 4472 | // This might be a parenthesized member name, but is more likely to |
| 4473 | // be a constructor declaration with an invalid argument type. Keep |
| 4474 | // looking. |
| 4475 | if (Tok.is(tok::annot_cxxscope)) |
| 4476 | ConsumeToken(); |
| 4477 | ConsumeToken(); |
| 4478 | |
| 4479 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 1453e31 | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4480 | // which must have one of the following syntactic forms (see the |
| 4481 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | efd009d | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4482 | switch (Tok.getKind()) { |
| 4483 | case tok::l_paren: |
| 4484 | // C(X ( int)); |
| 4485 | case tok::l_square: |
| 4486 | // C(X [ 5]); |
| 4487 | // C(X [ [attribute]]); |
| 4488 | case tok::coloncolon: |
| 4489 | // C(X :: Y); |
| 4490 | // C(X :: *p); |
| 4491 | case tok::r_paren: |
| 4492 | // C(X ) |
| 4493 | // Assume this isn't a constructor, rather than assuming it's a |
| 4494 | // constructor with an unnamed parameter of an ill-formed type. |
| 4495 | break; |
| 4496 | |
| 4497 | default: |
| 4498 | IsConstructor = true; |
| 4499 | break; |
| 4500 | } |
| 4501 | } |
| 4502 | |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4503 | TPA.Revert(); |
| 4504 | return IsConstructor; |
| 4505 | } |
Chris Lattner | b9093cd | 2006-08-04 04:39:53 +0000 | [diff] [blame] | 4506 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4507 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4508 | /// type-qualifier-list: [C99 6.7.5] |
| 4509 | /// type-qualifier |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4510 | /// [vendor] attributes |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4511 | /// [ only if VendorAttributesAllowed=true ] |
| 4512 | /// type-qualifier-list type-qualifier |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4513 | /// [vendor] type-qualifier-list attributes |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4514 | /// [ only if VendorAttributesAllowed=true ] |
| 4515 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4516 | /// [ only if CXX11AttributesAllowed=true ] |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4517 | /// Note: vendor can be GNU, MS, etc. |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4518 | /// |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4519 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 4520 | bool VendorAttributesAllowed, |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4521 | bool CXX11AttributesAllowed, |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 4522 | bool AtomicAllowed, |
| 4523 | bool IdentifierRequired) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4524 | if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed && |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4525 | isCXX11AttributeSpecifier()) { |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4526 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | 3dff251 | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4527 | ParseCXX11Attributes(attrs); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4528 | DS.takeAttributesFrom(attrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4529 | } |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4530 | |
| 4531 | SourceLocation EndLoc; |
| 4532 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4533 | while (1) { |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4534 | bool isInvalid = false; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4535 | const char *PrevSpec = 0; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4536 | unsigned DiagID = 0; |
Chris Lattner | 60809f5 | 2006-11-28 05:18:46 +0000 | [diff] [blame] | 4537 | SourceLocation Loc = Tok.getLocation(); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4538 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4539 | switch (Tok.getKind()) { |
Douglas Gregor | 28c7843 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4540 | case tok::code_completion: |
| 4541 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 5cec2ae | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 4542 | return cutOffParsing(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4543 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4544 | case tok::kw_const: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4545 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4546 | getLangOpts()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4547 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4548 | case tok::kw_volatile: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4549 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4550 | getLangOpts()); |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4551 | break; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4552 | case tok::kw_restrict: |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4553 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | 87e7951 | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4554 | getLangOpts()); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4555 | break; |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4556 | case tok::kw__Atomic: |
| 4557 | if (!AtomicAllowed) |
| 4558 | goto DoneWithTypeQuals; |
| 4559 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 4560 | getLangOpts()); |
| 4561 | break; |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4562 | |
| 4563 | // OpenCL qualifiers: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4564 | case tok::kw_private: |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4565 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 599cb8e | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4566 | goto DoneWithTypeQuals; |
| 4567 | case tok::kw___private: |
| 4568 | case tok::kw___global: |
| 4569 | case tok::kw___local: |
| 4570 | case tok::kw___constant: |
| 4571 | case tok::kw___read_only: |
| 4572 | case tok::kw___write_only: |
| 4573 | case tok::kw___read_write: |
| 4574 | ParseOpenCLQualifiers(DS); |
| 4575 | break; |
| 4576 | |
Aaron Ballman | 317a77f | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4577 | case tok::kw___uptr: |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 4578 | // GNU libc headers in C mode use '__uptr' as an identifer which conflicts |
| 4579 | // with the MS modifier keyword. |
| 4580 | if (VendorAttributesAllowed && !getLangOpts().CPlusPlus && |
Alp Toker | 47642d2 | 2013-12-03 06:13:01 +0000 | [diff] [blame] | 4581 | IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) { |
| 4582 | if (TryKeywordIdentFallback(false)) |
| 4583 | continue; |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 4584 | } |
| 4585 | case tok::kw___sptr: |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4586 | case tok::kw___w64: |
Steve Naroff | f9c29d4 | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 4587 | case tok::kw___ptr64: |
Francois Pichet | f2fb411 | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4588 | case tok::kw___ptr32: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4589 | case tok::kw___cdecl: |
| 4590 | case tok::kw___stdcall: |
| 4591 | case tok::kw___fastcall: |
Douglas Gregor | a941dca | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4592 | case tok::kw___thiscall: |
Francois Pichet | 17ed020 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4593 | case tok::kw___unaligned: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4594 | if (VendorAttributesAllowed) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4595 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 53339e0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4596 | continue; |
| 4597 | } |
| 4598 | goto DoneWithTypeQuals; |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4599 | case tok::kw___pascal: |
| 4600 | if (VendorAttributesAllowed) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4601 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4602 | continue; |
| 4603 | } |
| 4604 | goto DoneWithTypeQuals; |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 4605 | case tok::kw___attribute: |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4606 | if (VendorAttributesAllowed) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4607 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4608 | continue; // do *not* consume the next token! |
| 4609 | } |
| 4610 | // otherwise, FALL THROUGH! |
| 4611 | default: |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4612 | DoneWithTypeQuals: |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4613 | // If this is not a type-qualifier token, we're done reading type |
| 4614 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | e3e01a2 | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 4615 | DS.Finish(Diags, PP); |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4616 | if (EndLoc.isValid()) |
| 4617 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4618 | return; |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4619 | } |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4620 | |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4621 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 4622 | if (isInvalid) { |
| 4623 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4624 | Diag(Tok, DiagID) << PrevSpec; |
Chris Lattner | d9c3c59 | 2006-08-05 06:26:47 +0000 | [diff] [blame] | 4625 | } |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4626 | EndLoc = ConsumeToken(); |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4627 | } |
| 4628 | } |
| 4629 | |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 4630 | |
| 4631 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 4632 | /// |
| 4633 | void Parser::ParseDeclarator(Declarator &D) { |
| 4634 | /// This implements the 'declarator' production in the C grammar, then checks |
| 4635 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4636 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | d5d0a6c | 2006-08-07 00:58:14 +0000 | [diff] [blame] | 4637 | } |
| 4638 | |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4639 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) { |
| 4640 | if (Kind == tok::star || Kind == tok::caret) |
| 4641 | return true; |
| 4642 | |
| 4643 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 4644 | if (!Lang.CPlusPlus) |
| 4645 | return false; |
| 4646 | |
| 4647 | return Kind == tok::amp || Kind == tok::ampamp; |
| 4648 | } |
| 4649 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4650 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 4651 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 4652 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4653 | /// ptr-operator production. |
| 4654 | /// |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4655 | /// If the grammar of this construct is extended, matching changes must also be |
Richard Smith | 1453e31 | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4656 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 4657 | /// isConstructorDeclarator. |
Richard Smith | 09f76ee | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4658 | /// |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4659 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 4660 | /// [C] pointer[opt] direct-declarator |
| 4661 | /// [C++] direct-declarator |
| 4662 | /// [C++] ptr-operator declarator |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 4663 | /// |
| 4664 | /// pointer: [C99 6.7.5] |
| 4665 | /// '*' type-qualifier-list[opt] |
| 4666 | /// '*' type-qualifier-list[opt] pointer |
| 4667 | /// |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4668 | /// ptr-operator: |
| 4669 | /// '*' cv-qualifier-seq[opt] |
| 4670 | /// '&' |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4671 | /// [C++0x] '&&' |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4672 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4673 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4674 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4675 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 4676 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 66a985d | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 4677 | if (Diags.hasAllExtensionsSilenced()) |
| 4678 | D.setExtension(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4679 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4680 | // C++ member pointers start with a '::' or a nested-name. |
| 4681 | // Member pointers get special handling, since there's no place for the |
| 4682 | // scope spec in the generic path below. |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4683 | if (getLangOpts().CPlusPlus && |
Chris Lattner | 803802d | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 4684 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 4685 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4686 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4687 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4688 | CXXScopeSpec SS; |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4689 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4690 | |
Jeffrey Yasskin | 4e150f8 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 4691 | if (SS.isNotEmpty()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4692 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4693 | // The scope spec really belongs to the direct-declarator. |
Richard Smith | 5f044ad | 2013-01-08 22:43:49 +0000 | [diff] [blame] | 4694 | if (D.mayHaveIdentifier()) |
| 4695 | D.getCXXScopeSpec() = SS; |
| 4696 | else |
| 4697 | AnnotateScopeToken(SS, true); |
| 4698 | |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4699 | if (DirectDeclParser) |
| 4700 | (this->*DirectDeclParser)(D); |
| 4701 | return; |
| 4702 | } |
| 4703 | |
| 4704 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4705 | D.SetRangeEnd(Loc); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4706 | DeclSpec DS(AttrFactory); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4707 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4708 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4709 | |
| 4710 | // Recurse to parse whatever is left. |
| 4711 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 4712 | |
| 4713 | // Sema will have to catch (syntactically invalid) pointers into global |
| 4714 | // scope. It has to catch pointers into namespace scope anyway. |
| 4715 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4716 | Loc), |
| 4717 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4718 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4719 | return; |
| 4720 | } |
| 4721 | } |
| 4722 | |
| 4723 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4724 | // Not a pointer, C++ reference, or block. |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4725 | if (!isPtrOperatorToken(Kind, getLangOpts())) { |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4726 | if (DirectDeclParser) |
| 4727 | (this->*DirectDeclParser)(D); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4728 | return; |
| 4729 | } |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4730 | |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4731 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 4732 | // '&&' -> rvalue reference |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4733 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4734 | D.SetRangeEnd(Loc); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4735 | |
Chris Lattner | 9eac931 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 4736 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 788404f | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4737 | // Is a pointer. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4738 | DeclSpec DS(AttrFactory); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4739 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4740 | // FIXME: GNU attributes are not allowed here in a new-type-id. |
Alp Toker | 62c5b57 | 2013-11-26 01:30:10 +0000 | [diff] [blame] | 4741 | ParseTypeQualifierListOpt(DS, true, true, true, !D.mayOmitIdentifier()); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4742 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | 9ed6efd | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4743 | |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4744 | // Recursively parse the declarator. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4745 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4746 | if (Kind == tok::star) |
| 4747 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 4748 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | e71b378d | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 4749 | DS.getConstSpecLoc(), |
| 4750 | DS.getVolatileSpecLoc(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4751 | DS.getRestrictSpecLoc()), |
| 4752 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4753 | SourceLocation()); |
Steve Naroff | ec33ed9 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4754 | else |
| 4755 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4756 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4757 | Loc), |
| 4758 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4759 | SourceLocation()); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4760 | } else { |
| 4761 | // Is a reference |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4762 | DeclSpec DS(AttrFactory); |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 4763 | |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4764 | // Complain about rvalue references in C++03, but then go on and build |
| 4765 | // the declarator. |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4766 | if (Kind == tok::ampamp) |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4767 | Diag(Loc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4768 | diag::warn_cxx98_compat_rvalue_reference : |
| 4769 | diag::ext_rvalue_reference); |
Sebastian Redl | 3b27be6 | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4770 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4771 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 4772 | ParseTypeQualifierListOpt(DS); |
| 4773 | D.ExtendWithDeclSpec(DS); |
| 4774 | |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 4775 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 4776 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 4777 | // template type argument, in which case the cv-qualifiers are ignored. |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 4778 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 4779 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 4780 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4781 | diag::err_invalid_reference_qualifier_application) << "const"; |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 4782 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 4783 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4784 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4785 | // 'restrict' is permitted as an extension. |
| 4786 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 4787 | Diag(DS.getAtomicSpecLoc(), |
| 4788 | diag::err_invalid_reference_qualifier_application) << "_Atomic"; |
Bill Wendling | 93efb22 | 2007-06-02 23:28:54 +0000 | [diff] [blame] | 4789 | } |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4790 | |
| 4791 | // Recursively parse the declarator. |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4792 | ParseDeclaratorInternal(D, DirectDeclParser); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4793 | |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4794 | if (D.getNumTypeObjects() > 0) { |
| 4795 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 4796 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 4797 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | ebad6a2 | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 4798 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 4799 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4800 | << II; |
| 4801 | else |
| 4802 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4803 | << "type name"; |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4804 | |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4805 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | 66583c5 | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4806 | // can go ahead and build the (technically ill-formed) |
| 4807 | // declarator: reference collapsing will take care of it. |
| 4808 | } |
| 4809 | } |
| 4810 | |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4811 | // Remember that we parsed a reference type. |
Chris Lattner | 788404f | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4812 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | ed0f3b0 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4813 | Kind == tok::amp), |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4814 | DS.getAttributes(), |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4815 | SourceLocation()); |
Bill Wendling | 3708c18 | 2007-05-27 10:15:43 +0000 | [diff] [blame] | 4816 | } |
Chris Lattner | 6c7416c | 2006-08-07 00:19:33 +0000 | [diff] [blame] | 4817 | } |
| 4818 | |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4819 | static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D, |
| 4820 | SourceLocation EllipsisLoc) { |
| 4821 | if (EllipsisLoc.isValid()) { |
| 4822 | FixItHint Insertion; |
| 4823 | if (!D.getEllipsisLoc().isValid()) { |
| 4824 | Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "..."); |
| 4825 | D.setEllipsisLoc(EllipsisLoc); |
| 4826 | } |
| 4827 | P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) |
| 4828 | << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName(); |
| 4829 | } |
| 4830 | } |
| 4831 | |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4832 | /// ParseDirectDeclarator |
| 4833 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4834 | /// [C99] identifier |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4835 | /// '(' declarator ')' |
| 4836 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 4837 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4838 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4839 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4840 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4841 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4842 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 4843 | /// attribute-specifier-seq[opt] |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 4844 | /// direct-declarator '(' parameter-type-list ')' |
| 4845 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4846 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4847 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 22c40fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 4848 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 4849 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4850 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 4851 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 4852 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | 61956c4 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4853 | /// [C++] declarator-id |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4854 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4855 | /// |
| 4856 | /// declarator-id: [C++ 8] |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4857 | /// '...'[opt] id-expression |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4858 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 4859 | /// |
| 4860 | /// id-expression: [C++ 5.1] |
| 4861 | /// unqualified-id |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4862 | /// qualified-id |
Douglas Gregor | 831c93f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4863 | /// |
| 4864 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4865 | /// identifier |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4866 | /// operator-function-id |
Douglas Gregor | d90fd52 | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4867 | /// conversion-function-id |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4868 | /// '~' class-name |
Douglas Gregor | 7f74112 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 4869 | /// template-id |
Argyrios Kyrtzidis | e442635 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 4870 | /// |
Richard Smith | 1453e31 | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4871 | /// Note, any additional constructs added here may need corresponding changes |
| 4872 | /// in isConstructorDeclarator. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4873 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4874 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | 32a0379 | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4875 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4876 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4877 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4878 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4879 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4880 | D.getContext() == Declarator::MemberContext; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4881 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
Douglas Gregor | df593fb | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4882 | EnteringContext); |
John McCall | 1f476a1 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4883 | } |
| 4884 | |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4885 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 0be31a2 | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4886 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | 2b058ef | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 4887 | // Change the declaration context for name lookup, until this function |
| 4888 | // is exited (and the declarator has been parsed). |
| 4889 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4890 | } |
| 4891 | |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4892 | // C++0x [dcl.fct]p14: |
| 4893 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4894 | // of a parameter-declaration-clause without a preceding comma. In |
| 4895 | // this case, the ellipsis is parsed as part of the |
| 4896 | // abstract-declarator if the type of the parameter names a template |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4897 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 4898 | // as part of the parameter-declaration-clause. |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4899 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4900 | !((D.getContext() == Declarator::PrototypeContext || |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4901 | D.getContext() == Declarator::LambdaExprParameterContext || |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4902 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | 27b4c16 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4903 | NextToken().is(tok::r_paren) && |
Richard Smith | b19337f | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4904 | !D.hasGroupingParens() && |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4905 | !Actions.containsUnexpandedParameterPacks(D))) { |
| 4906 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 4907 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) { |
| 4908 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 4909 | // the user what they should have done. |
| 4910 | ParseDeclarator(D); |
| 4911 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4912 | return; |
| 4913 | } else |
| 4914 | D.setEllipsisLoc(EllipsisLoc); |
| 4915 | |
| 4916 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 4917 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 4918 | // the l_paren token. |
| 4919 | } |
| 4920 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4921 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 4922 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 4923 | // We found something that indicates the start of an unqualified-id. |
| 4924 | // Parse that unqualified-id. |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4925 | bool AllowConstructorName; |
| 4926 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 4927 | AllowConstructorName = false; |
| 4928 | else if (D.getCXXScopeSpec().isSet()) |
| 4929 | AllowConstructorName = |
| 4930 | (D.getContext() == Declarator::FileContext || |
Dmitri Gribenko | d1c91f1 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 4931 | D.getContext() == Declarator::MemberContext); |
John McCall | 84821e7 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4932 | else |
| 4933 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 4934 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4935 | SourceLocation TemplateKWLoc; |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4936 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 4937 | /*EnteringContext=*/true, |
| 4938 | /*AllowDestructorName=*/true, |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4939 | AllowConstructorName, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4940 | ParsedType(), |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4941 | TemplateKWLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4942 | D.getName()) || |
| 4943 | // Once we're past the identifier, if the scope was bad, mark the |
| 4944 | // whole declarator bad. |
| 4945 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4946 | D.SetIdentifier(0, Tok.getLocation()); |
| 4947 | D.setInvalidType(true); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4948 | } else { |
| 4949 | // Parsed the unqualified-id; update range information and move along. |
| 4950 | if (D.getSourceRange().getBegin().isInvalid()) |
| 4951 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 4952 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | dbc5daf | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4953 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4954 | goto PastIdentifier; |
Douglas Gregor | 11d0c4c | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4955 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4956 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4957 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4958 | "There's a C++-specific check for tok::identifier above"); |
| 4959 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 4960 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 4961 | ConsumeToken(); |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4962 | goto PastIdentifier; |
Richard Smith | 9ce302e | 2013-07-11 05:10:21 +0000 | [diff] [blame] | 4963 | } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) { |
Richard Smith | f39720b | 2013-10-13 22:12:28 +0000 | [diff] [blame] | 4964 | // A virt-specifier isn't treated as an identifier if it appears after a |
| 4965 | // trailing-return-type. |
| 4966 | if (D.getContext() != Declarator::TrailingReturnContext || |
| 4967 | !isCXX11VirtSpecifier(Tok)) { |
| 4968 | Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id) |
| 4969 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 4970 | D.SetIdentifier(0, Tok.getLocation()); |
| 4971 | ConsumeToken(); |
| 4972 | goto PastIdentifier; |
| 4973 | } |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4974 | } |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4975 | |
Douglas Gregor | 7861a80 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4976 | if (Tok.is(tok::l_paren)) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4977 | // direct-declarator: '(' declarator ')' |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 4978 | // direct-declarator: '(' attributes declarator ')' |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4979 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 4980 | ParseParenDeclarator(D); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4981 | |
| 4982 | // If the declarator was parenthesized, we entered the declarator |
| 4983 | // scope when parsing the parenthesized declarator, then exited |
| 4984 | // the scope already. Re-enter the scope, if we need to. |
| 4985 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4986 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4987 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4988 | if (!D.isInvalidType() && |
| 4989 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4990 | // Change the declaration context for name lookup, until this function |
| 4991 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 358acd5 | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4992 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 9de54ea | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4993 | } |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4994 | } else if (D.mayOmitIdentifier()) { |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 4995 | // This could be something simple like "int" (in which case the declarator |
| 4996 | // portion is empty), if an abstract-declarator is allowed. |
| 4997 | D.SetIdentifier(0, Tok.getLocation()); |
Richard Smith | b19337f | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4998 | |
| 4999 | // The grammar for abstract-pack-declarator does not allow grouping parens. |
| 5000 | // FIXME: Revisit this once core issue 1488 is resolved. |
| 5001 | if (D.hasEllipsis() && D.hasGroupingParens()) |
| 5002 | Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()), |
| 5003 | diag::ext_abstract_pack_declarator_parens); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5004 | } else { |
David Blaikie | 5d577a2 | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 5005 | if (Tok.getKind() == tok::annot_pragma_parser_crash) |
David Blaikie | 5bd4c2a | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 5006 | LLVM_BUILTIN_TRAP; |
Douglas Gregor | d9f92e2 | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 5007 | if (D.getContext() == Declarator::MemberContext) |
| 5008 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 5009 | << D.getDeclSpec().getSourceRange(); |
Richard Trieu | 9c67267 | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 5010 | else if (getLangOpts().CPlusPlus) { |
| 5011 | if (Tok.is(tok::period) || Tok.is(tok::arrow)) |
| 5012 | Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow); |
Richard Trieu | 2f58696 | 2013-09-05 02:31:33 +0000 | [diff] [blame] | 5013 | else { |
| 5014 | SourceLocation Loc = D.getCXXScopeSpec().getEndLoc(); |
| 5015 | if (Tok.isAtStartOfLine() && Loc.isValid()) |
| 5016 | Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id) |
| 5017 | << getLangOpts().CPlusPlus; |
| 5018 | else |
| 5019 | Diag(Tok, diag::err_expected_unqualified_id) |
| 5020 | << getLangOpts().CPlusPlus; |
| 5021 | } |
Richard Trieu | 9c67267 | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 5022 | } else |
Chris Lattner | 6d29c10 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 5023 | Diag(Tok, diag::err_expected_ident_lparen); |
Chris Lattner | eec40f9 | 2006-08-06 21:55:29 +0000 | [diff] [blame] | 5024 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 8c5dd73 | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 5025 | D.setInvalidType(true); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5026 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5027 | |
Argyrios Kyrtzidis | 9323b04 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 5028 | PastIdentifier: |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5029 | assert(D.isPastIdentifier() && |
| 5030 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5031 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5032 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 5033 | if (D.hasName() && !D.getNumTypeObjects()) |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5034 | MaybeParseCXX11Attributes(D); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5035 | |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5036 | while (1) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5037 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5038 | // Enter function-declaration scope, limiting any declarators to the |
| 5039 | // function prototype scope, including parameter declarators. |
| 5040 | ParseScope PrototypeScope(this, |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 5041 | Scope::FunctionPrototypeScope|Scope::DeclScope| |
| 5042 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 5043 | ? Scope::FunctionDeclarationScope : 0)); |
| 5044 | |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5045 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 5046 | // In such a case, check if we actually have a function declarator; if it |
| 5047 | // is not, the declarator has been fully parsed. |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5048 | bool IsAmbiguous = false; |
Richard Smith | 4f605af | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 5049 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 5050 | // The name of the declarator, if any, is tentatively declared within |
| 5051 | // a possible direct initializer. |
| 5052 | TentativelyDeclaredIdentifiers.push_back(D.getIdentifier()); |
| 5053 | bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous); |
| 5054 | TentativelyDeclaredIdentifiers.pop_back(); |
| 5055 | if (!IsFunctionDecl) |
| 5056 | break; |
| 5057 | } |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5058 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5059 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 5060 | T.consumeOpen(); |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5061 | ParseFunctionDeclarator(D, attrs, T, IsAmbiguous); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5062 | PrototypeScope.Exit(); |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5063 | } else if (Tok.is(tok::l_square)) { |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5064 | ParseBracketDeclarator(D); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5065 | } else { |
| 5066 | break; |
| 5067 | } |
| 5068 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5069 | } |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5070 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5071 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 5072 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5073 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5074 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 5075 | /// |
| 5076 | /// direct-declarator: |
| 5077 | /// '(' declarator ')' |
| 5078 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5079 | /// direct-declarator '(' parameter-type-list ')' |
| 5080 | /// direct-declarator '(' identifier-list[opt] ')' |
| 5081 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 5082 | /// parameter-type-list[opt] ')' |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5083 | /// |
| 5084 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5085 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 5086 | T.consumeOpen(); |
| 5087 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5088 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5089 | |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5090 | // Eat any attributes before we look at whether this is a grouping or function |
| 5091 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 5092 | // the type being built up, for example: |
| 5093 | // int (__attribute__(()) *x)(long y) |
| 5094 | // If this ends up not being a grouping paren, the attribute applies to the |
| 5095 | // first argument, for example: |
| 5096 | // int (__attribute__(()) int x) |
| 5097 | // In either case, we need to eat any attributes to be able to determine what |
| 5098 | // sort of paren this is. |
| 5099 | // |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5100 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5101 | bool RequiresArg = false; |
| 5102 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5103 | ParseGNUAttributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5104 | |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5105 | // We require that the argument list (if this is a non-grouping paren) be |
| 5106 | // present even if the attribute list was empty. |
| 5107 | RequiresArg = true; |
| 5108 | } |
Chad Rosier | eea9ca7 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 5109 | |
Steve Naroff | 44ac777 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 5110 | // Eat any Microsoft extensions. |
Chad Rosier | eea9ca7 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 5111 | ParseMicrosoftTypeAttributes(attrs); |
| 5112 | |
Dawn Perchik | 335e16b | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5113 | // Eat any Borland extensions. |
Ted Kremenek | 5eec2b0 | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 5114 | if (Tok.is(tok::kw___pascal)) |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5115 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5116 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5117 | // If we haven't past the identifier yet (or where the identifier would be |
| 5118 | // stored, if this is an abstract declarator), then this is probably just |
| 5119 | // grouping parens. However, if this could be an abstract-declarator, then |
| 5120 | // this could also be the start of function arguments (consider 'void()'). |
| 5121 | bool isGrouping; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5122 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5123 | if (!D.mayOmitIdentifier()) { |
| 5124 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 5125 | // paren, because we haven't seen the identifier yet. |
| 5126 | isGrouping = true; |
| 5127 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 43f340f | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 5128 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 5129 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5130 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 5131 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5132 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 5133 | // considered to be a type, not a K&R identifier-list. |
| 5134 | isGrouping = false; |
| 5135 | } else { |
| 5136 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 5137 | isGrouping = true; |
| 5138 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5139 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5140 | // If this is a grouping paren, handle: |
| 5141 | // direct-declarator: '(' declarator ')' |
| 5142 | // direct-declarator: '(' attributes declarator ')' |
| 5143 | if (isGrouping) { |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5144 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 5145 | D.setEllipsisLoc(SourceLocation()); |
| 5146 | |
Argyrios Kyrtzidis | 8ae3684 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 5147 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 9a1191c | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5148 | D.setGroupingParens(true); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5149 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5150 | // Match the ')'. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5151 | T.consumeClose(); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5152 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5153 | T.getCloseLocation()), |
| 5154 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 8ae3684 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 5155 | |
| 5156 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 0efa75c | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5157 | |
| 5158 | // An ellipsis cannot be placed outside parentheses. |
| 5159 | if (EllipsisLoc.isValid()) |
| 5160 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 5161 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5162 | return; |
| 5163 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5164 | |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5165 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 5166 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5167 | // identifier (and remember where it would have been), then call into |
| 5168 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5169 | D.SetIdentifier(0, Tok.getLocation()); |
| 5170 | |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5171 | // Enter function-declaration scope, limiting any declarators to the |
| 5172 | // function prototype scope, including parameter declarators. |
| 5173 | ParseScope PrototypeScope(this, |
Richard Smith | e233fbf | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 5174 | Scope::FunctionPrototypeScope | Scope::DeclScope | |
| 5175 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 5176 | ? Scope::FunctionDeclarationScope : 0)); |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5177 | ParseFunctionDeclarator(D, attrs, T, false, RequiresArg); |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5178 | PrototypeScope.Exit(); |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5179 | } |
| 5180 | |
| 5181 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 5182 | /// declarator D up to a paren, which indicates that we are parsing function |
| 5183 | /// arguments. |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5184 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5185 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 5186 | /// immediately after the open paren - they should be considered to be the |
| 5187 | /// first argument of a parameter. |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5188 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5189 | /// If RequiresArg is true, then the first argument of the function is required |
| 5190 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5191 | /// |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5192 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 5193 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 5194 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 5195 | /// |
| 5196 | /// [C++11] exception-specification: |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5197 | /// dynamic-exception-specification |
| 5198 | /// noexcept-specification |
| 5199 | /// |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5200 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5201 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5202 | BalancedDelimiterTracker &Tracker, |
Richard Smith | 943c440 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5203 | bool IsAmbiguous, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5204 | bool RequiresArg) { |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5205 | assert(getCurScope()->isFunctionPrototypeScope() && |
David Blaikie | 15a430a | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5206 | "Should call from a Function scope"); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5207 | // lparen is already consumed! |
| 5208 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 5209 | |
| 5210 | // This should be true when the function has typed arguments. |
| 5211 | // Otherwise, it is treated as a K&R-style function. |
| 5212 | bool HasProto = false; |
| 5213 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5214 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5215 | // Remember where we see an ellipsis, if any. |
| 5216 | SourceLocation EllipsisLoc; |
| 5217 | |
| 5218 | DeclSpec DS(AttrFactory); |
| 5219 | bool RefQualifierIsLValueRef = true; |
| 5220 | SourceLocation RefQualifierLoc; |
Douglas Gregor | e248eea | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 5221 | SourceLocation ConstQualifierLoc; |
| 5222 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5223 | ExceptionSpecificationType ESpecType = EST_None; |
| 5224 | SourceRange ESpecRange; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5225 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 5226 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5227 | ExprResult NoexceptExpr; |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5228 | ParsedAttributes FnAttrs(AttrFactory); |
Richard Smith | 700537c | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 5229 | TypeResult TrailingReturnType; |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5230 | |
James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 5231 | Actions.ActOnStartFunctionDeclarator(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5232 | /* LocalEndLoc is the end location for the local FunctionTypeLoc. |
| 5233 | EndLoc is the end location for the function declarator. |
| 5234 | They differ for trailing return types. */ |
| 5235 | SourceLocation StartLoc, LocalEndLoc, EndLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5236 | SourceLocation LParenLoc, RParenLoc; |
| 5237 | LParenLoc = Tracker.getOpenLocation(); |
| 5238 | StartLoc = LParenLoc; |
| 5239 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5240 | if (isFunctionDeclaratorIdentifierList()) { |
| 5241 | if (RequiresArg) |
| 5242 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5243 | |
| 5244 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 5245 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5246 | Tracker.consumeClose(); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5247 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5248 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5249 | EndLoc = RParenLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5250 | } else { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5251 | if (Tok.isNot(tok::r_paren)) |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5252 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, |
| 5253 | EllipsisLoc); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5254 | else if (RequiresArg) |
| 5255 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5256 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5257 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5258 | |
| 5259 | // If we have the closing ')', eat it. |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5260 | Tracker.consumeClose(); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5261 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5262 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5263 | EndLoc = RParenLoc; |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5264 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5265 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5266 | // FIXME: Accept these components in any order, and produce fixits to |
| 5267 | // correct the order if the user gets it wrong. Ideally we should deal |
| 5268 | // with the virt-specifier-seq and pure-specifier in the same way. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5269 | |
| 5270 | // Parse cv-qualifier-seq[opt]. |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5271 | ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false, |
| 5272 | /*CXX11AttributesAllowed*/ false, |
| 5273 | /*AtomicAllowed*/ false); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5274 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 5275 | EndLoc = DS.getSourceRange().getEnd(); |
| 5276 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 5277 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 5278 | } |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5279 | |
| 5280 | // Parse ref-qualifier[opt]. |
| 5281 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5282 | Diag(Tok, getLangOpts().CPlusPlus11 ? |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5283 | diag::warn_cxx98_compat_ref_qualifier : |
| 5284 | diag::ext_ref_qualifier); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5285 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5286 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 5287 | RefQualifierLoc = ConsumeToken(); |
| 5288 | EndLoc = RefQualifierLoc; |
| 5289 | } |
| 5290 | |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5291 | // C++11 [expr.prim.general]p3: |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5292 | // If a declaration declares a member function or member function |
| 5293 | // template of a class X, the expression this is a prvalue of type |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5294 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5295 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5296 | // declarator. |
Richard Smith | ad1bbb9 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5297 | // FIXME: currently, "static" case isn't handled correctly. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5298 | bool IsCXX11MemberFunction = |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5299 | getLangOpts().CPlusPlus11 && |
Richard Smith | ad1bbb9 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5300 | (D.getContext() == Declarator::MemberContext |
| 5301 | ? !D.getDeclSpec().isFriendSpecified() |
| 5302 | : D.getContext() == Declarator::FileContext && |
| 5303 | D.getCXXScopeSpec().isValid() && |
| 5304 | Actions.CurContext->isRecord()); |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5305 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 5306 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
Richard Smith | 01141a9 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 5307 | DS.getTypeQualifiers() | |
Richard Smith | 034185c | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 5308 | (D.getDeclSpec().isConstexprSpecified() && |
| 5309 | !getLangOpts().CPlusPlus1y |
Richard Smith | 01141a9 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 5310 | ? Qualifiers::Const : 0), |
Douglas Gregor | 3024f07 | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5311 | IsCXX11MemberFunction); |
Richard Smith | 2331bbf | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5312 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5313 | // Parse exception-specification[opt]. |
Richard Smith | 2331bbf | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5314 | ESpecType = tryParseExceptionSpecification(ESpecRange, |
Douglas Gregor | 433e053 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 5315 | DynamicExceptions, |
| 5316 | DynamicExceptionRanges, |
Richard Smith | 2331bbf | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5317 | NoexceptExpr); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5318 | if (ESpecType != EST_None) |
| 5319 | EndLoc = ESpecRange.getEnd(); |
| 5320 | |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5321 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 5322 | // after the exception-specification. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5323 | MaybeParseCXX11Attributes(FnAttrs); |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5324 | |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5325 | // Parse trailing-return-type[opt]. |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5326 | LocalEndLoc = EndLoc; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5327 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) { |
Richard Smith | 5d164bc | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5328 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5329 | if (D.getDeclSpec().getTypeSpecType() == TST_auto) |
| 5330 | StartLoc = D.getDeclSpec().getTypeSpecTypeLoc(); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5331 | LocalEndLoc = Tok.getLocation(); |
Douglas Gregor | db0b9f1 | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 5332 | SourceRange Range; |
Richard Smith | 700537c | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 5333 | TrailingReturnType = ParseTrailingReturnType(Range); |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5334 | EndLoc = Range.getEnd(); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5335 | } |
| 5336 | } |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5337 | } |
| 5338 | |
| 5339 | // Remember that we parsed a function type, and remember the attributes. |
| 5340 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5341 | IsAmbiguous, |
| 5342 | LParenLoc, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5343 | ParamInfo.data(), ParamInfo.size(), |
Abramo Bagnara | aeeb989 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5344 | EllipsisLoc, RParenLoc, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5345 | DS.getTypeQualifiers(), |
| 5346 | RefQualifierIsLValueRef, |
Douglas Gregor | e248eea | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 5347 | RefQualifierLoc, ConstQualifierLoc, |
| 5348 | VolatileQualifierLoc, |
Douglas Gregor | ad69e65 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 5349 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5350 | ESpecType, ESpecRange.getBegin(), |
| 5351 | DynamicExceptions.data(), |
| 5352 | DynamicExceptionRanges.data(), |
| 5353 | DynamicExceptions.size(), |
| 5354 | NoexceptExpr.isUsable() ? |
| 5355 | NoexceptExpr.get() : 0, |
Abramo Bagnara | 2fc03ca | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5356 | StartLoc, LocalEndLoc, D, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5357 | TrailingReturnType), |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5358 | FnAttrs, EndLoc); |
James Molloy | 6f8780b | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 5359 | |
| 5360 | Actions.ActOnEndFunctionDeclarator(); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5361 | } |
| 5362 | |
| 5363 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 5364 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 5365 | /// |
| 5366 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 5367 | /// abstract-declarators. |
| 5368 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5369 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5370 | && Tok.is(tok::identifier) |
| 5371 | && !TryAltiVecVectorToken() |
| 5372 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 5373 | // 6.7.5.3p11. |
| 5374 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 5375 | // Identifier lists follow a really simple grammar: the identifiers can |
| 5376 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 5377 | // identifier lists are really rare in the brave new modern world, and |
| 5378 | // it is very common for someone to typo a type in a non-K&R style |
| 5379 | // list. If we are presented with something like: "void foo(intptr x, |
| 5380 | // float y)", we don't want to start parsing the function declarator as |
| 5381 | // though it is a K&R style declarator just because intptr is an |
| 5382 | // invalid type. |
| 5383 | // |
| 5384 | // To handle this, we check to see if the token after the first |
| 5385 | // identifier is a "," or ")". Only then do we parse it as an |
| 5386 | // identifier list. |
| 5387 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 5388 | } |
| 5389 | |
| 5390 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 5391 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 5392 | /// |
| 5393 | /// After returning, ParamInfo will hold the parsed parameters. |
| 5394 | /// |
| 5395 | /// identifier-list: [C99 6.7.5] |
| 5396 | /// identifier |
| 5397 | /// identifier-list ',' identifier |
| 5398 | /// |
| 5399 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 5400 | Declarator &D, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5401 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) { |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5402 | // If there was no identifier specified for the declarator, either we are in |
| 5403 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 5404 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 5405 | // diagnose this. |
| 5406 | if (!D.getIdentifier()) |
| 5407 | Diag(Tok, diag::ext_ident_list_in_param); |
| 5408 | |
| 5409 | // Maintain an efficient lookup of params we have seen so far. |
| 5410 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 5411 | |
| 5412 | while (1) { |
| 5413 | // If this isn't an identifier, report the error and skip until ')'. |
| 5414 | if (Tok.isNot(tok::identifier)) { |
| 5415 | Diag(Tok, diag::err_expected_ident); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5416 | SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5417 | // Forget we parsed anything. |
| 5418 | ParamInfo.clear(); |
| 5419 | return; |
| 5420 | } |
| 5421 | |
| 5422 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 5423 | |
| 5424 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 5425 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 5426 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 5427 | |
| 5428 | // Verify that the argument identifier has not already been mentioned. |
| 5429 | if (!ParamsSoFar.insert(ParmII)) { |
| 5430 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 5431 | } else { |
| 5432 | // Remember this identifier in ParamInfo. |
| 5433 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 5434 | Tok.getLocation(), |
| 5435 | 0)); |
| 5436 | } |
| 5437 | |
| 5438 | // Eat the identifier. |
| 5439 | ConsumeToken(); |
| 5440 | |
| 5441 | // The list continues if we see a comma. |
| 5442 | if (Tok.isNot(tok::comma)) |
| 5443 | break; |
| 5444 | ConsumeToken(); |
| 5445 | } |
| 5446 | } |
| 5447 | |
| 5448 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 5449 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 5450 | /// identifier list. |
| 5451 | /// |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5452 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 5453 | /// caller parsed those arguments immediately after the open paren - they should |
| 5454 | /// be considered to be part of the first parameter. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5455 | /// |
| 5456 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 5457 | /// be the location of the ellipsis, if any was parsed. |
| 5458 | /// |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5459 | /// parameter-type-list: [C99 6.7.5] |
| 5460 | /// parameter-list |
| 5461 | /// parameter-list ',' '...' |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5462 | /// [C++] parameter-list '...' |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5463 | /// |
| 5464 | /// parameter-list: [C99 6.7.5] |
| 5465 | /// parameter-declaration |
| 5466 | /// parameter-list ',' parameter-declaration |
| 5467 | /// |
| 5468 | /// parameter-declaration: [C99 6.7.5] |
| 5469 | /// declaration-specifiers declarator |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5470 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5471 | /// [C++11] initializer-clause |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 5472 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | f769df5 | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 5473 | /// declaration-specifiers abstract-declarator[opt] |
| 5474 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 5475 | /// '=' assignment-expression |
Chris Lattner | e37e233 | 2006-08-15 04:50:22 +0000 | [diff] [blame] | 5476 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5477 | /// [C++11] attribute-specifier-seq parameter-declaration |
Chris Lattner | c0acd3d | 2006-07-31 05:13:43 +0000 | [diff] [blame] | 5478 | /// |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5479 | void Parser::ParseParameterDeclarationClause( |
| 5480 | Declarator &D, |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5481 | ParsedAttributes &FirstArgAttrs, |
Craig Topper | 5603df4 | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5482 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo, |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5483 | SourceLocation &EllipsisLoc) { |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5484 | while (1) { |
| 5485 | if (Tok.is(tok::ellipsis)) { |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5486 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 5487 | // before deciding this was a parameter-declaration-clause. |
Douglas Gregor | 94349fd | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 5488 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5489 | break; |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5490 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5491 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5492 | // Parse the declaration-specifiers. |
John McCall | 28a6aea | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5493 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5494 | DeclSpec DS(AttrFactory); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5495 | |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5496 | // Parse any C++11 attributes. |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5497 | MaybeParseCXX11Attributes(DS.getAttributes()); |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5498 | |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5499 | // Skip any Microsoft attributes before a param. |
Chad Rosier | f8a2e70 | 2012-12-20 20:37:53 +0000 | [diff] [blame] | 5500 | MaybeParseMicrosoftAttributes(DS.getAttributes()); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5501 | |
| 5502 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 8ff2c6c | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5503 | |
| 5504 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5505 | // Take them so that we only apply the attributes to the first parameter. |
Douglas Gregor | 9e66af4 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5506 | // FIXME: If we can leave the attributes in the token stream somehow, we can |
Richard Smith | 2620cd9 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5507 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 5508 | // too much hassle. |
| 5509 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5510 | |
Chris Lattner | de39c3e | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 5511 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5512 | |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5513 | |
| 5514 | // Parse the declarator. This is "PrototypeContext" or |
| 5515 | // "LambdaExprParameterContext", because we must accept either |
| 5516 | // 'declarator' or 'abstract-declarator' here. |
| 5517 | Declarator ParmDeclarator(DS, |
| 5518 | D.getContext() == Declarator::LambdaExprContext ? |
| 5519 | Declarator::LambdaExprParameterContext : |
| 5520 | Declarator::PrototypeContext); |
| 5521 | ParseDeclarator(ParmDeclarator); |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5522 | |
| 5523 | // Parse GNU attributes, if present. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5524 | MaybeParseGNUAttributes(ParmDeclarator); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5525 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5526 | // Remember this parsed parameter in ParamInfo. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5527 | IdentifierInfo *ParmII = ParmDeclarator.getIdentifier(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5528 | |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5529 | // DefArgToks is used when the parsing of default arguments needs |
| 5530 | // to be delayed. |
| 5531 | CachedTokens *DefArgToks = 0; |
| 5532 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5533 | // If no parameter was specified, verify that *something* was specified, |
| 5534 | // otherwise we have a missing type and identifier. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5535 | if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 && |
| 5536 | ParmDeclarator.getNumTypeObjects() == 0) { |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5537 | // Completely missing, emit error. |
| 5538 | Diag(DSStart, diag::err_missing_param); |
| 5539 | } else { |
| 5540 | // Otherwise, we have something. Add it and let semantic analysis try |
| 5541 | // to grok it and add the result to the ParamInfo we are building. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5542 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5543 | // Inform the actions module about the parameter declarator, so it gets |
| 5544 | // added to the current scope. |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5545 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), |
| 5546 | ParmDeclarator); |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5547 | // Parse the default argument, if any. We parse the default |
| 5548 | // arguments in all dialects; the semantic analysis in |
| 5549 | // ActOnParamDefaultArgument will reject the default argument in |
| 5550 | // C. |
| 5551 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5552 | SourceLocation EqualLoc = Tok.getLocation(); |
| 5553 | |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5554 | // Parse the default argument |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5555 | if (D.getContext() == Declarator::MemberContext) { |
| 5556 | // If we're inside a class definition, cache the tokens |
| 5557 | // corresponding to the default argument. We'll actually parse |
| 5558 | // them when we see the end of the class definition. |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5559 | // FIXME: Can we use a smart pointer for Toks? |
| 5560 | DefArgToks = new CachedTokens; |
| 5561 | |
Richard Smith | 1fff95c | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 5562 | if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5563 | delete DefArgToks; |
| 5564 | DefArgToks = 0; |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5565 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 249179c | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5566 | } else { |
| 5567 | // Mark the end of the default argument so that we know when to |
| 5568 | // stop when we parse it later on. |
| 5569 | Token DefArgEnd; |
| 5570 | DefArgEnd.startToken(); |
| 5571 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 5572 | DefArgEnd.setLocation(Tok.getLocation()); |
| 5573 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5574 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 5575 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 249179c | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5576 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5577 | } else { |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5578 | // Consume the '='. |
Douglas Gregor | 5835403 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5579 | ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5580 | |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5581 | // The argument isn't actually potentially evaluated unless it is |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5582 | // used. |
| 5583 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | 7fcbd90 | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 5584 | Sema::PotentiallyEvaluatedIfUsed, |
| 5585 | Param); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5586 | |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5587 | ExprResult DefArgResult; |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5588 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Sebastian Redl | 1678d5f | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5589 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5590 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 1678d5f | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5591 | } else |
Sebastian Redl | db63af2 | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5592 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5593 | if (DefArgResult.isInvalid()) { |
| 5594 | Actions.ActOnParamDefaultArgumentError(Param); |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5595 | SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5596 | } else { |
| 5597 | // Inform the actions module about the default argument |
| 5598 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5599 | DefArgResult.take()); |
Douglas Gregor | 4d87df5 | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5600 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5601 | } |
| 5602 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5603 | |
| 5604 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Faisal Vali | 2b391ab | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5605 | ParmDeclarator.getIdentifierLoc(), |
| 5606 | Param, DefArgToks)); |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5607 | } |
| 5608 | |
| 5609 | // If the next token is a comma, consume it and keep reading arguments. |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5610 | if (Tok.isNot(tok::comma)) { |
| 5611 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5612 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5613 | |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5614 | if (!getLangOpts().CPlusPlus) { |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5615 | // We have ellipsis without a preceding ',', which is ill-formed |
| 5616 | // in C. Complain and provide the fix. |
| 5617 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5618 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5619 | } |
| 5620 | } |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5621 | |
Douglas Gregor | 9bfc2e5 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5622 | break; |
| 5623 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5624 | |
Chris Lattner | 371ed4e | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5625 | // Consume the comma. |
| 5626 | ConsumeToken(); |
Chris Lattner | acd58a3 | 2006-08-06 17:24:14 +0000 | [diff] [blame] | 5627 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5628 | |
Chris Lattner | 6c940e6 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 5629 | } |
Chris Lattner | c0a1c7d | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5630 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5631 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 5632 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 5633 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 5634 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 5635 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5636 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 5637 | /// attribute-specifier-seq[opt] |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5638 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 7bdcc4a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5639 | if (CheckProhibitedCXX11Attribute()) |
| 5640 | return; |
| 5641 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5642 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 5643 | T.consumeOpen(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5644 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5645 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 5646 | // This code does a fast path to handle some of the most obvious cases. |
| 5647 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5648 | T.consumeClose(); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5649 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5650 | MaybeParseCXX11Attributes(attrs); |
Chad Rosier | c118395 | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5651 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5652 | // Remember that we parsed the empty array type. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5653 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5654 | T.getOpenLocation(), |
| 5655 | T.getCloseLocation()), |
| 5656 | attrs, T.getCloseLocation()); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5657 | return; |
| 5658 | } else if (Tok.getKind() == tok::numeric_constant && |
| 5659 | GetLookAheadToken(1).is(tok::r_square)) { |
| 5660 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | bcc22fc | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5661 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5662 | ConsumeToken(); |
| 5663 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5664 | T.consumeClose(); |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5665 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5666 | MaybeParseCXX11Attributes(attrs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5667 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5668 | // Remember that we parsed a array type, and remember its features. |
Nikola Smiljanic | c531dcc | 2013-01-11 08:33:05 +0000 | [diff] [blame] | 5669 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, |
John McCall | 53fa714 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5670 | ExprRes.release(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5671 | T.getOpenLocation(), |
| 5672 | T.getCloseLocation()), |
| 5673 | attrs, T.getCloseLocation()); |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5674 | return; |
| 5675 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5676 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5677 | // If valid, this location is the position where we read the 'static' keyword. |
| 5678 | SourceLocation StaticLoc; |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5679 | if (Tok.is(tok::kw_static)) |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 5680 | StaticLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5681 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5682 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5683 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5684 | DeclSpec DS(AttrFactory); |
Chris Lattner | cf0bab2 | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5685 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5686 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5687 | // If we haven't already read 'static', check to see if there is one after the |
| 5688 | // type-qualifier-list. |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5689 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 5690 | StaticLoc = ConsumeToken(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5691 | |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5692 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5693 | bool isStar = false; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5694 | ExprResult NumElements; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5695 | |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5696 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 5697 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
Sylvestre Ledru | 830885c | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 5698 | // the token after the star is a ']'. Since stars in arrays are |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5699 | // infrequent, use of lookahead is not costly here. |
| 5700 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | c439f0d | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 5701 | ConsumeToken(); // Eat the '*'. |
Chris Lattner | 1906f80 | 2006-08-06 19:14:46 +0000 | [diff] [blame] | 5702 | |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5703 | if (StaticLoc.isValid()) { |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5704 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | b6ec4e7 | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5705 | StaticLoc = SourceLocation(); // Drop the static. |
| 5706 | } |
Chris Lattner | 521ff2b | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5707 | isStar = true; |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5708 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5709 | // Note, in C89, this production uses the constant-expr production instead |
| 5710 | // of assignment-expr. The only difference is that assignment-expr allows |
| 5711 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 5712 | // are not i-c-e's, so we don't need to distinguish between the two here. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5713 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5714 | // Parse the constant-expression or assignment-expression now (depending |
| 5715 | // on dialect). |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5716 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5717 | NumElements = ParseConstantExpression(); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5718 | } else { |
| 5719 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 5720 | Sema::ConstantEvaluated); |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5721 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5722 | } |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 5723 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5724 | |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 5725 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 17f2c7d | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 5726 | if (NumElements.isInvalid()) { |
Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 5727 | D.setInvalidType(true); |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 5728 | // If the expression was invalid, skip it. |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5729 | SkipUntil(tok::r_square, StopAtSemi); |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 5730 | return; |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5731 | } |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5732 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5733 | T.consumeClose(); |
Sebastian Redl | f6591ca | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5734 | |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5735 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5736 | MaybeParseCXX11Attributes(attrs); |
Alexis Hunt | 96d5c76 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5737 | |
Chris Lattner | 84a1162 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5738 | // Remember that we parsed a array type, and remember its features. |
John McCall | 084e83d | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5739 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Chris Lattner | cbc426d | 2006-12-02 06:43:02 +0000 | [diff] [blame] | 5740 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 0431825 | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5741 | NumElements.release(), |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5742 | T.getOpenLocation(), |
| 5743 | T.getCloseLocation()), |
| 5744 | attrs, T.getCloseLocation()); |
Chris Lattner | e8074e6 | 2006-08-06 18:30:15 +0000 | [diff] [blame] | 5745 | } |
| 5746 | |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5747 | /// [GNU] typeof-specifier: |
| 5748 | /// typeof ( expressions ) |
| 5749 | /// typeof ( type-name ) |
| 5750 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5751 | /// |
| 5752 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 76c7228 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5753 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5754 | Token OpTok = Tok; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5755 | SourceLocation StartLoc = ConsumeToken(); |
| 5756 | |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5757 | const bool hasParens = Tok.is(tok::l_paren); |
| 5758 | |
Eli Friedman | 15681d6 | 2012-09-26 04:34:21 +0000 | [diff] [blame] | 5759 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated, |
| 5760 | Sema::ReuseLambdaContextDecl); |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5761 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5762 | bool isCastExpr; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5763 | ParsedType CastTy; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5764 | SourceRange CastRange; |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5765 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 5766 | CastTy, CastRange); |
John McCall | e859503 | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5767 | if (hasParens) |
| 5768 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5769 | |
| 5770 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5771 | // FIXME: Not accurate, the range gets one token more than it should. |
| 5772 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5773 | else |
| 5774 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5775 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5776 | if (isCastExpr) { |
| 5777 | if (!CastTy) { |
| 5778 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5779 | return; |
Douglas Gregor | 220cac5 | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 5780 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5781 | |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5782 | const char *PrevSpec = 0; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5783 | unsigned DiagID; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5784 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5785 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5786 | DiagID, CastTy)) |
| 5787 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 7bd9844 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5788 | return; |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5789 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5790 | |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5791 | // If we get here, the operand to the typeof was an expresion. |
| 5792 | if (Operand.isInvalid()) { |
| 5793 | DS.SetTypeSpecError(); |
Steve Naroff | 4bd2f71 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 5794 | return; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5795 | } |
Argyrios Kyrtzidis | 2545aeb | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5796 | |
Eli Friedman | e0afc98 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5797 | // We might need to transform the operand if it is potentially evaluated. |
| 5798 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 5799 | if (Operand.isInvalid()) { |
| 5800 | DS.SetTypeSpecError(); |
| 5801 | return; |
| 5802 | } |
| 5803 | |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5804 | const char *PrevSpec = 0; |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5805 | unsigned DiagID; |
Argyrios Kyrtzidis | f5cc7ac | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5806 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5807 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5808 | DiagID, Operand.get())) |
John McCall | 49bfce4 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5809 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5810 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5811 | |
Benjamin Kramer | e56f393 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 5812 | /// [C11] atomic-specifier: |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5813 | /// _Atomic ( type-name ) |
| 5814 | /// |
| 5815 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5816 | assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) && |
| 5817 | "Not an atomic specifier"); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5818 | |
| 5819 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5820 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Richard Smith | 8e1ac33 | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5821 | if (T.consumeOpen()) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5822 | return; |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5823 | |
| 5824 | TypeResult Result = ParseTypeName(); |
| 5825 | if (Result.isInvalid()) { |
Alexey Bataev | ee6507d | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5826 | SkipUntil(tok::r_paren, StopAtSemi); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5827 | return; |
| 5828 | } |
| 5829 | |
| 5830 | // Match the ')' |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5831 | T.consumeClose(); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5832 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5833 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5834 | return; |
| 5835 | |
Douglas Gregor | e7a8e3b | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5836 | DS.setTypeofParensRange(T.getRange()); |
| 5837 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | 0dfb889 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5838 | |
| 5839 | const char *PrevSpec = 0; |
| 5840 | unsigned DiagID; |
| 5841 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
| 5842 | DiagID, Result.release())) |
| 5843 | Diag(StartLoc, DiagID) << PrevSpec; |
| 5844 | } |
| 5845 | |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5846 | |
| 5847 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 5848 | /// from TryAltiVecVectorToken. |
| 5849 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 5850 | Token Next = NextToken(); |
| 5851 | switch (Next.getKind()) { |
| 5852 | default: return false; |
| 5853 | case tok::kw_short: |
| 5854 | case tok::kw_long: |
| 5855 | case tok::kw_signed: |
| 5856 | case tok::kw_unsigned: |
| 5857 | case tok::kw_void: |
| 5858 | case tok::kw_char: |
| 5859 | case tok::kw_int: |
| 5860 | case tok::kw_float: |
| 5861 | case tok::kw_double: |
| 5862 | case tok::kw_bool: |
| 5863 | case tok::kw___pixel: |
| 5864 | Tok.setKind(tok::kw___vector); |
| 5865 | return true; |
| 5866 | case tok::identifier: |
| 5867 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5868 | Tok.setKind(tok::kw___vector); |
| 5869 | return true; |
| 5870 | } |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5871 | if (Next.getIdentifierInfo() == Ident_bool) { |
| 5872 | Tok.setKind(tok::kw___vector); |
| 5873 | return true; |
| 5874 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5875 | return false; |
| 5876 | } |
| 5877 | } |
| 5878 | |
| 5879 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 5880 | const char *&PrevSpec, unsigned &DiagID, |
| 5881 | bool &isInvalid) { |
| 5882 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 5883 | Token Next = NextToken(); |
| 5884 | switch (Next.getKind()) { |
| 5885 | case tok::kw_short: |
| 5886 | case tok::kw_long: |
| 5887 | case tok::kw_signed: |
| 5888 | case tok::kw_unsigned: |
| 5889 | case tok::kw_void: |
| 5890 | case tok::kw_char: |
| 5891 | case tok::kw_int: |
| 5892 | case tok::kw_float: |
| 5893 | case tok::kw_double: |
| 5894 | case tok::kw_bool: |
| 5895 | case tok::kw___pixel: |
| 5896 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5897 | return true; |
| 5898 | case tok::identifier: |
| 5899 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5900 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5901 | return true; |
| 5902 | } |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5903 | if (Next.getIdentifierInfo() == Ident_bool) { |
| 5904 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5905 | return true; |
| 5906 | } |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5907 | break; |
| 5908 | default: |
| 5909 | break; |
| 5910 | } |
Douglas Gregor | 9938e3b | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 5911 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5912 | DS.isTypeAltiVecVector()) { |
| 5913 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 5914 | return true; |
Bill Schmidt | 99a084b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5915 | } else if ((Tok.getIdentifierInfo() == Ident_bool) && |
| 5916 | DS.isTypeAltiVecVector()) { |
| 5917 | isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID); |
| 5918 | return true; |
Chris Lattner | 73a9c7d | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5919 | } |
| 5920 | return false; |
| 5921 | } |