Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ParseDecl.cpp - Declaration Parsing ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Declaration portions of the Parser interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Parse/Parser.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 15 | #include "RAIIObjectsForParser.h" |
Larisse Voufo | 7c64ef0 | 2013-06-21 00:08:46 +0000 | [diff] [blame] | 16 | #include "clang/AST/DeclTemplate.h" |
Benjamin Kramer | 9852f58 | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 17 | #include "clang/Basic/AddressSpaces.h" |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 18 | #include "clang/Basic/CharInfo.h" |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 19 | #include "clang/Basic/OpenCL.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 20 | #include "clang/Parse/ParseDiagnostic.h" |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 21 | #include "clang/Sema/Lookup.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 22 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 23 | #include "clang/Sema/PrettyDeclStackTrace.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 24 | #include "clang/Sema/Scope.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallSet.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/SmallString.h" |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/StringSwitch.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | using namespace clang; |
| 29 | |
| 30 | //===----------------------------------------------------------------------===// |
| 31 | // C99 6.7: Declarations. |
| 32 | //===----------------------------------------------------------------------===// |
| 33 | |
| 34 | /// ParseTypeName |
| 35 | /// type-name: [C99 6.7.6] |
| 36 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 37 | /// |
| 38 | /// Called type-id in C++. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 39 | TypeResult Parser::ParseTypeName(SourceRange *Range, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 40 | Declarator::TheContext Context, |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 41 | AccessSpecifier AS, |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 42 | Decl **OwnedType, |
| 43 | ParsedAttributes *Attrs) { |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 44 | DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context); |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 45 | if (DSC == DSC_normal) |
| 46 | DSC = DSC_type_specifier; |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 47 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | // Parse the common declaration-specifiers piece. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 49 | DeclSpec DS(AttrFactory); |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 50 | if (Attrs) |
| 51 | DS.addAttributes(Attrs->getList()); |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 52 | ParseSpecifierQualifierList(DS, AS, DSC); |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 53 | if (OwnedType) |
| 54 | *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0; |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 55 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 56 | // Parse the abstract-declarator, if present. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 57 | Declarator DeclaratorInfo(DS, Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 58 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 59 | if (Range) |
| 60 | *Range = DeclaratorInfo.getSourceRange(); |
| 61 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 62 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 63 | return true; |
| 64 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 65 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Caitlin Sadowski | eff98fc | 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 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 77 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 |
| 92 | /// attrib-name |
| 93 | /// attrib-name '(' identifier ')' |
| 94 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 95 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 96 | /// |
| 97 | /// [GNU] attrib-name: |
| 98 | /// identifier |
| 99 | /// typespec |
| 100 | /// typequal |
| 101 | /// storageclass |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | /// |
Richard Smith | 4c6c411 | 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: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 105 | /// |
Richard Smith | 4c6c411 | 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 | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 116 | /// |
Richard Smith | 4c6c411 | 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 | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 118 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 119 | SourceLocation *endLoc, |
| 120 | LateParsedAttrList *LateAttrs) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 121 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 123 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 124 | ConsumeToken(); |
| 125 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 126 | "attribute")) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 127 | SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 128 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 129 | } |
| 130 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 131 | SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 132 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 133 | } |
| 134 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 135 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 136 | Tok.is(tok::comma)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 145 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 146 | if (Tok.is(tok::l_paren)) { |
| 147 | // handle "parameterized" attributes |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 148 | if (LateAttrs && isAttributeLateParsed(*AttrName)) { |
Caitlin Sadowski | eff98fc | 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 | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 152 | |
Bill Wendling | ad017fa | 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 | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 154 | // with other late-parsed declarations. |
DeLesley Hutchins | 161db02 | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 155 | if (!ClassStack.empty() && !LateAttrs->parseSoon()) |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 156 | getCurrentClass().LateParsedDeclarations.push_back(LA); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 157 | |
Caitlin Sadowski | eff98fc | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | |
Caitlin Sadowski | eff98fc | 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 | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 166 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 167 | 0, SourceLocation(), AttributeList::AS_GNU); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 168 | } |
| 169 | } else { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 170 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 171 | AttributeList::AS_GNU); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 175 | SkipUntil(tok::r_paren, StopAtSemi); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 176 | SourceLocation Loc = Tok.getLocation(); |
Richard Smith | d92aa2d | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 177 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 178 | SkipUntil(tok::r_paren, StopAtSemi); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 179 | if (endLoc) |
| 180 | *endLoc = Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 181 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Aaron Ballman | 9feedb8 | 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 | d92aa2d | 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 | 9feedb8 | 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 | d92aa2d | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 194 | #include "clang/Parse/AttrIdentifierArg.inc" |
Douglas Gregor | 92eb7d8 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 195 | .Default(false); |
| 196 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 197 | |
Aaron Ballman | 9feedb8 | 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 | 8edabd9 | 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 | d386fef | 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 | 6880f49 | 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 | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 242 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 243 | SourceLocation AttrNameLoc, |
| 244 | ParsedAttributes &Attrs, |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 245 | SourceLocation *EndLoc, |
| 246 | IdentifierInfo *ScopeName, |
| 247 | SourceLocation ScopeLoc, |
| 248 | AttributeList::Syntax Syntax) { |
Caitlin Sadowski | eff98fc | 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 | d92aa2d | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 252 | AttributeList::Kind AttrKind = |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 253 | AttributeList::getKind(AttrName, ScopeName, Syntax); |
Richard Smith | d92aa2d | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 254 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 255 | // Availability attributes have their own grammar. |
Richard Smith | d386fef | 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 | d92aa2d | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 258 | if (AttrKind == AttributeList::AT_Availability) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 259 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 260 | return; |
| 261 | } |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 262 | // Thread safety attributes are parsed in an unevaluated context. |
| 263 | // FIXME: Share the bulk of the parsing code here and just pull out |
| 264 | // the unevaluated context. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 265 | if (IsThreadSafetyAttribute(AttrName->getName())) { |
| 266 | ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 267 | return; |
| 268 | } |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 269 | // Type safety attributes have their own grammar. |
Richard Smith | d92aa2d | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 270 | if (AttrKind == AttributeList::AT_TypeTagForDatatype) { |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 271 | ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 272 | return; |
| 273 | } |
Aaron Ballman | 9feedb8 | 2013-11-04 12:55:56 +0000 | [diff] [blame] | 274 | // Some attributes expect solely a type parameter. |
| 275 | if (attributeIsTypeArgAttr(*AttrName)) { |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 276 | ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 277 | return; |
| 278 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 279 | |
Richard Smith | d92aa2d | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 280 | // Ignore the left paren location for now. |
| 281 | ConsumeParen(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 282 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 283 | ArgsVector ArgExprs; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 284 | |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 285 | if (Tok.is(tok::identifier)) { |
Richard Smith | d92aa2d | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 286 | // If this attribute wants an 'identifier' argument, make it so. |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 287 | bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName); |
Richard Smith | d92aa2d | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 288 | |
| 289 | // If we don't know how to parse this attribute, but this is the only |
| 290 | // token in this argument, assume it's meant to be an identifier. |
Bill Wendling | 307c92e | 2013-12-05 18:35:37 +0000 | [diff] [blame^] | 291 | if (AttrKind == AttributeList::UnknownAttribute || |
| 292 | AttrKind == AttributeList::IgnoredAttribute) { |
Richard Smith | d92aa2d | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 293 | const Token &Next = NextToken(); |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 294 | IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma); |
Richard Smith | d92aa2d | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 295 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 296 | |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 297 | if (IsIdentifierArg) |
| 298 | ArgExprs.push_back(ParseIdentifierLoc()); |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 301 | if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) { |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 302 | // Eat the comma. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 303 | if (!ArgExprs.empty()) |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 304 | ConsumeToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 305 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 306 | // Parse the non-empty comma-separated list of expressions. |
| 307 | while (1) { |
| 308 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 309 | if (ArgExpr.isInvalid()) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 310 | SkipUntil(tok::r_paren, StopAtSemi); |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 311 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 312 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 313 | ArgExprs.push_back(ArgExpr.release()); |
| 314 | if (Tok.isNot(tok::comma)) |
| 315 | break; |
| 316 | ConsumeToken(); // Eat the comma, move to the next argument |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 317 | } |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 318 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 319 | |
| 320 | SourceLocation RParen = Tok.getLocation(); |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 321 | if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 322 | SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc; |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 323 | Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc, |
| 324 | ArgExprs.data(), ArgExprs.size(), Syntax); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 325 | } |
| 326 | } |
| 327 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 328 | /// \brief Parses a single argument for a declspec, including the |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 329 | /// surrounding parens. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 330 | void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 331 | SourceLocation AttrNameLoc, |
| 332 | ParsedAttributes &Attrs) |
| 333 | { |
| 334 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 335 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 336 | AttrName->getNameStart(), tok::r_paren)) |
| 337 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 338 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 339 | ExprResult ArgExpr(ParseConstantExpression()); |
| 340 | if (ArgExpr.isInvalid()) { |
| 341 | T.skipToEnd(); |
| 342 | return; |
| 343 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 344 | ArgsUnion ExprList = ArgExpr.take(); |
| 345 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1, |
| 346 | AttributeList::AS_Declspec); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 347 | |
| 348 | T.consumeClose(); |
| 349 | } |
| 350 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 351 | /// \brief Determines whether a declspec is a "simple" one requiring no |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 352 | /// arguments. |
| 353 | bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) { |
| 354 | return llvm::StringSwitch<bool>(Ident->getName()) |
| 355 | .Case("dllimport", true) |
| 356 | .Case("dllexport", true) |
| 357 | .Case("noreturn", true) |
| 358 | .Case("nothrow", true) |
| 359 | .Case("noinline", true) |
| 360 | .Case("naked", true) |
| 361 | .Case("appdomain", true) |
| 362 | .Case("process", true) |
| 363 | .Case("jitintrinsic", true) |
| 364 | .Case("noalias", true) |
| 365 | .Case("restrict", true) |
| 366 | .Case("novtable", true) |
| 367 | .Case("selectany", true) |
| 368 | .Case("thread", true) |
Aaron Ballman | 3ce0de6 | 2013-05-04 16:58:37 +0000 | [diff] [blame] | 369 | .Case("safebuffers", true ) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 370 | .Default(false); |
| 371 | } |
| 372 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 373 | /// \brief Attempts to parse a declspec which is not simple (one that takes |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 374 | /// parameters). Will return false if we properly handled the declspec, or |
| 375 | /// true if it is an unknown declspec. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 376 | void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 377 | SourceLocation Loc, |
| 378 | ParsedAttributes &Attrs) { |
| 379 | // Try to handle the easy case first -- these declspecs all take a single |
| 380 | // parameter as their argument. |
| 381 | if (llvm::StringSwitch<bool>(Ident->getName()) |
| 382 | .Case("uuid", true) |
| 383 | .Case("align", true) |
| 384 | .Case("allocate", true) |
| 385 | .Default(false)) { |
| 386 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 387 | } else if (Ident->getName() == "deprecated") { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 388 | // The deprecated declspec has an optional single argument, so we will |
| 389 | // check for a l-paren to decide whether we should parse an argument or |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 390 | // not. |
| 391 | if (Tok.getKind() == tok::l_paren) |
| 392 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 393 | else |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 394 | Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 395 | } else if (Ident->getName() == "property") { |
| 396 | // The property declspec is more complex in that it can take one or two |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 397 | // assignment expressions as a parameter, but the lhs of the assignment |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 398 | // must be named get or put. |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 399 | if (Tok.isNot(tok::l_paren)) { |
| 400 | Diag(Tok.getLocation(), diag::err_expected_lparen_after) |
| 401 | << Ident->getNameStart(); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 402 | return; |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 403 | } |
| 404 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 405 | T.expectAndConsume(diag::err_expected_lparen_after, |
| 406 | Ident->getNameStart(), tok::r_paren); |
| 407 | |
| 408 | enum AccessorKind { |
| 409 | AK_Invalid = -1, |
| 410 | AK_Put = 0, AK_Get = 1 // indices into AccessorNames |
| 411 | }; |
| 412 | IdentifierInfo *AccessorNames[] = { 0, 0 }; |
| 413 | bool HasInvalidAccessor = false; |
| 414 | |
| 415 | // Parse the accessor specifications. |
| 416 | while (true) { |
| 417 | // Stop if this doesn't look like an accessor spec. |
| 418 | if (!Tok.is(tok::identifier)) { |
| 419 | // If the user wrote a completely empty list, use a special diagnostic. |
| 420 | if (Tok.is(tok::r_paren) && !HasInvalidAccessor && |
| 421 | AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) { |
| 422 | Diag(Loc, diag::err_ms_property_no_getter_or_putter); |
| 423 | break; |
| 424 | } |
| 425 | |
| 426 | Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor); |
| 427 | break; |
| 428 | } |
| 429 | |
| 430 | AccessorKind Kind; |
| 431 | SourceLocation KindLoc = Tok.getLocation(); |
| 432 | StringRef KindStr = Tok.getIdentifierInfo()->getName(); |
| 433 | if (KindStr == "get") { |
| 434 | Kind = AK_Get; |
| 435 | } else if (KindStr == "put") { |
| 436 | Kind = AK_Put; |
| 437 | |
| 438 | // Recover from the common mistake of using 'set' instead of 'put'. |
| 439 | } else if (KindStr == "set") { |
| 440 | Diag(KindLoc, diag::err_ms_property_has_set_accessor) |
| 441 | << FixItHint::CreateReplacement(KindLoc, "put"); |
| 442 | Kind = AK_Put; |
| 443 | |
| 444 | // Handle the mistake of forgetting the accessor kind by skipping |
| 445 | // this accessor. |
| 446 | } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) { |
| 447 | Diag(KindLoc, diag::err_ms_property_missing_accessor_kind); |
| 448 | ConsumeToken(); |
| 449 | HasInvalidAccessor = true; |
| 450 | goto next_property_accessor; |
| 451 | |
| 452 | // Otherwise, complain about the unknown accessor kind. |
| 453 | } else { |
| 454 | Diag(KindLoc, diag::err_ms_property_unknown_accessor); |
| 455 | HasInvalidAccessor = true; |
| 456 | Kind = AK_Invalid; |
| 457 | |
| 458 | // Try to keep parsing unless it doesn't look like an accessor spec. |
| 459 | if (!NextToken().is(tok::equal)) break; |
| 460 | } |
| 461 | |
| 462 | // Consume the identifier. |
| 463 | ConsumeToken(); |
| 464 | |
| 465 | // Consume the '='. |
| 466 | if (Tok.is(tok::equal)) { |
| 467 | ConsumeToken(); |
| 468 | } else { |
| 469 | Diag(Tok.getLocation(), diag::err_ms_property_expected_equal) |
| 470 | << KindStr; |
| 471 | break; |
| 472 | } |
| 473 | |
| 474 | // Expect the method name. |
| 475 | if (!Tok.is(tok::identifier)) { |
| 476 | Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name); |
| 477 | break; |
| 478 | } |
| 479 | |
| 480 | if (Kind == AK_Invalid) { |
| 481 | // Just drop invalid accessors. |
| 482 | } else if (AccessorNames[Kind] != NULL) { |
| 483 | // Complain about the repeated accessor, ignore it, and keep parsing. |
| 484 | Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr; |
| 485 | } else { |
| 486 | AccessorNames[Kind] = Tok.getIdentifierInfo(); |
| 487 | } |
| 488 | ConsumeToken(); |
| 489 | |
| 490 | next_property_accessor: |
| 491 | // Keep processing accessors until we run out. |
| 492 | if (Tok.is(tok::comma)) { |
| 493 | ConsumeAnyToken(); |
| 494 | continue; |
| 495 | |
| 496 | // If we run into the ')', stop without consuming it. |
| 497 | } else if (Tok.is(tok::r_paren)) { |
| 498 | break; |
| 499 | } else { |
| 500 | Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen); |
| 501 | break; |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | // Only add the property attribute if it was well-formed. |
| 506 | if (!HasInvalidAccessor) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 507 | Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(), |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 508 | AccessorNames[AK_Get], AccessorNames[AK_Put], |
| 509 | AttributeList::AS_Declspec); |
| 510 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 511 | T.skipToEnd(); |
| 512 | } else { |
| 513 | // We don't recognize this as a valid declspec, but instead of creating the |
| 514 | // attribute and allowing sema to warn about it, we will warn here instead. |
| 515 | // This is because some attributes have multiple spellings, but we need to |
| 516 | // disallow that for declspecs (such as align vs aligned). If we made the |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 517 | // attribute, we'd have to split the valid declspec spelling logic into |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 518 | // both locations. |
| 519 | Diag(Loc, diag::warn_ms_declspec_unknown) << Ident; |
| 520 | |
| 521 | // If there's an open paren, we should eat the open and close parens under |
| 522 | // the assumption that this unknown declspec has parameters. |
| 523 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 524 | if (!T.consumeOpen()) |
| 525 | T.skipToEnd(); |
| 526 | } |
| 527 | } |
| 528 | |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 529 | /// [MS] decl-specifier: |
| 530 | /// __declspec ( extended-decl-modifier-seq ) |
| 531 | /// |
| 532 | /// [MS] extended-decl-modifier-seq: |
| 533 | /// extended-decl-modifier[opt] |
| 534 | /// extended-decl-modifier extended-decl-modifier-seq |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 535 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) { |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 536 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 537 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 538 | ConsumeToken(); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 539 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 540 | if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec", |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 541 | tok::r_paren)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 542 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 543 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 544 | // An empty declspec is perfectly legal and should not warn. Additionally, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 545 | // you can specify multiple attributes per declspec. |
| 546 | while (Tok.getKind() != tok::r_paren) { |
| 547 | // We expect either a well-known identifier or a generic string. Anything |
| 548 | // else is a malformed declspec. |
| 549 | bool IsString = Tok.getKind() == tok::string_literal ? true : false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 550 | if (!IsString && Tok.getKind() != tok::identifier && |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 551 | Tok.getKind() != tok::kw_restrict) { |
| 552 | Diag(Tok, diag::err_ms_declspec_type); |
| 553 | T.skipToEnd(); |
| 554 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 555 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 556 | |
| 557 | IdentifierInfo *AttrName; |
| 558 | SourceLocation AttrNameLoc; |
| 559 | if (IsString) { |
| 560 | SmallString<8> StrBuffer; |
| 561 | bool Invalid = false; |
| 562 | StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid); |
| 563 | if (Invalid) { |
| 564 | T.skipToEnd(); |
| 565 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 566 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 567 | AttrName = PP.getIdentifierInfo(Str); |
| 568 | AttrNameLoc = ConsumeStringToken(); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 569 | } else { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 570 | AttrName = Tok.getIdentifierInfo(); |
| 571 | AttrNameLoc = ConsumeToken(); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 572 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 573 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 574 | if (IsString || IsSimpleMicrosoftDeclSpec(AttrName)) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 575 | // If we have a generic string, we will allow it because there is no |
| 576 | // documented list of allowable string declspecs, but we know they exist |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 577 | // (for instance, SAL declspecs in older versions of MSVC). |
| 578 | // |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 579 | // Alternatively, if the identifier is a simple one, then it requires no |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 580 | // arguments and can be turned into an attribute directly. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 581 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 582 | AttributeList::AS_Declspec); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 583 | else |
| 584 | ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 585 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 586 | T.consumeClose(); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 587 | } |
| 588 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 589 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 590 | // Treat these like attributes |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 591 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 592 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 593 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 594 | Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) || |
| 595 | Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 596 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 597 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 598 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 599 | AttributeList::AS_Keyword); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 600 | } |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 601 | } |
| 602 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 603 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 604 | // Treat these like attributes |
| 605 | while (Tok.is(tok::kw___pascal)) { |
| 606 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 607 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 608 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 609 | AttributeList::AS_Keyword); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 610 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 613 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 614 | // Treat these like attributes |
| 615 | while (Tok.is(tok::kw___kernel)) { |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 616 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 617 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 618 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 619 | AttributeList::AS_Keyword); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 623 | void Parser::ParseOpenCLQualifiers(DeclSpec &DS) { |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 624 | // FIXME: The mapping from attribute spelling to semantics should be |
| 625 | // performed in Sema, not here. |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 626 | SourceLocation Loc = Tok.getLocation(); |
| 627 | switch(Tok.getKind()) { |
| 628 | // OpenCL qualifiers: |
| 629 | case tok::kw___private: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 630 | case tok::kw_private: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 631 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 632 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 633 | PP.getIdentifierInfo("address_space"), Loc, 0); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 634 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 635 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 636 | case tok::kw___global: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 637 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 638 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 639 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 640 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 641 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 642 | case tok::kw___local: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 643 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 644 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 645 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 646 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 647 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 648 | case tok::kw___constant: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 649 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 650 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 651 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 652 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 653 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 654 | case tok::kw___read_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 655 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 656 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 657 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 658 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 659 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 660 | case tok::kw___write_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 661 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 662 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 663 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 664 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 665 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 666 | case tok::kw___read_write: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 667 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 668 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 669 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 670 | break; |
| 671 | default: break; |
| 672 | } |
| 673 | } |
| 674 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 675 | /// \brief Parse a version number. |
| 676 | /// |
| 677 | /// version: |
| 678 | /// simple-integer |
| 679 | /// simple-integer ',' simple-integer |
| 680 | /// simple-integer ',' simple-integer ',' simple-integer |
| 681 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 682 | Range = Tok.getLocation(); |
| 683 | |
| 684 | if (!Tok.is(tok::numeric_constant)) { |
| 685 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 686 | SkipUntil(tok::comma, tok::r_paren, |
| 687 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 688 | return VersionTuple(); |
| 689 | } |
| 690 | |
| 691 | // Parse the major (and possibly minor and subminor) versions, which |
| 692 | // are stored in the numeric constant. We utilize a quirk of the |
| 693 | // lexer, which is that it handles something like 1.2.3 as a single |
| 694 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 695 | SmallString<512> Buffer; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 696 | Buffer.resize(Tok.getLength()+1); |
| 697 | const char *ThisTokBegin = &Buffer[0]; |
| 698 | |
| 699 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 700 | bool Invalid = false; |
| 701 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 702 | if (Invalid) |
| 703 | return VersionTuple(); |
| 704 | |
| 705 | // Parse the major version. |
| 706 | unsigned AfterMajor = 0; |
| 707 | unsigned Major = 0; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 708 | while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 709 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 710 | ++AfterMajor; |
| 711 | } |
| 712 | |
| 713 | if (AfterMajor == 0) { |
| 714 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 715 | SkipUntil(tok::comma, tok::r_paren, |
| 716 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 717 | return VersionTuple(); |
| 718 | } |
| 719 | |
| 720 | if (AfterMajor == ActualLength) { |
| 721 | ConsumeToken(); |
| 722 | |
| 723 | // We only had a single version component. |
| 724 | if (Major == 0) { |
| 725 | Diag(Tok, diag::err_zero_version); |
| 726 | return VersionTuple(); |
| 727 | } |
| 728 | |
| 729 | return VersionTuple(Major); |
| 730 | } |
| 731 | |
| 732 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 733 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 734 | SkipUntil(tok::comma, tok::r_paren, |
| 735 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 736 | return VersionTuple(); |
| 737 | } |
| 738 | |
| 739 | // Parse the minor version. |
| 740 | unsigned AfterMinor = AfterMajor + 1; |
| 741 | unsigned Minor = 0; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 742 | while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 743 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 744 | ++AfterMinor; |
| 745 | } |
| 746 | |
| 747 | if (AfterMinor == ActualLength) { |
| 748 | ConsumeToken(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 749 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 750 | // We had major.minor. |
| 751 | if (Major == 0 && Minor == 0) { |
| 752 | Diag(Tok, diag::err_zero_version); |
| 753 | return VersionTuple(); |
| 754 | } |
| 755 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 756 | return VersionTuple(Major, Minor); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | // If what follows is not a '.', we have a problem. |
| 760 | if (ThisTokBegin[AfterMinor] != '.') { |
| 761 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 762 | SkipUntil(tok::comma, tok::r_paren, |
| 763 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 764 | return VersionTuple(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | // Parse the subminor version. |
| 768 | unsigned AfterSubminor = AfterMinor + 1; |
| 769 | unsigned Subminor = 0; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 770 | while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 771 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 772 | ++AfterSubminor; |
| 773 | } |
| 774 | |
| 775 | if (AfterSubminor != ActualLength) { |
| 776 | Diag(Tok, diag::err_expected_version); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 777 | SkipUntil(tok::comma, tok::r_paren, |
| 778 | StopAtSemi | StopBeforeMatch | StopAtCodeCompletion); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 779 | return VersionTuple(); |
| 780 | } |
| 781 | ConsumeToken(); |
| 782 | return VersionTuple(Major, Minor, Subminor); |
| 783 | } |
| 784 | |
| 785 | /// \brief Parse the contents of the "availability" attribute. |
| 786 | /// |
| 787 | /// availability-attribute: |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 788 | /// 'availability' '(' platform ',' version-arg-list, opt-message')' |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 789 | /// |
| 790 | /// platform: |
| 791 | /// identifier |
| 792 | /// |
| 793 | /// version-arg-list: |
| 794 | /// version-arg |
| 795 | /// version-arg ',' version-arg-list |
| 796 | /// |
| 797 | /// version-arg: |
| 798 | /// 'introduced' '=' version |
| 799 | /// 'deprecated' '=' version |
Douglas Gregor | 93a7067 | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 800 | /// 'obsoleted' = version |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 801 | /// 'unavailable' |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 802 | /// opt-message: |
| 803 | /// 'message' '=' <string> |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 804 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 805 | SourceLocation AvailabilityLoc, |
| 806 | ParsedAttributes &attrs, |
| 807 | SourceLocation *endLoc) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 808 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 809 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 810 | ExprResult MessageExpr; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 811 | |
| 812 | // Opening '('. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 813 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 814 | if (T.consumeOpen()) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 815 | Diag(Tok, diag::err_expected_lparen); |
| 816 | return; |
| 817 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 818 | |
| 819 | // Parse the platform name, |
| 820 | if (Tok.isNot(tok::identifier)) { |
| 821 | Diag(Tok, diag::err_availability_expected_platform); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 822 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 823 | return; |
| 824 | } |
Richard Smith | 8edabd9 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 825 | IdentifierLoc *Platform = ParseIdentifierLoc(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 826 | |
| 827 | // Parse the ',' following the platform name. |
| 828 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren)) |
| 829 | return; |
| 830 | |
| 831 | // If we haven't grabbed the pointers for the identifiers |
| 832 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 833 | if (!Ident_introduced) { |
| 834 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 835 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 836 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 837 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 838 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 842 | SourceLocation UnavailableLoc; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 843 | do { |
| 844 | if (Tok.isNot(tok::identifier)) { |
| 845 | Diag(Tok, diag::err_availability_expected_change); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 846 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 847 | return; |
| 848 | } |
| 849 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 850 | SourceLocation KeywordLoc = ConsumeToken(); |
| 851 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 852 | if (Keyword == Ident_unavailable) { |
| 853 | if (UnavailableLoc.isValid()) { |
| 854 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 855 | << Keyword << SourceRange(UnavailableLoc); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 856 | } |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 857 | UnavailableLoc = KeywordLoc; |
| 858 | |
| 859 | if (Tok.isNot(tok::comma)) |
| 860 | break; |
| 861 | |
| 862 | ConsumeToken(); |
| 863 | continue; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 866 | if (Tok.isNot(tok::equal)) { |
| 867 | Diag(Tok, diag::err_expected_equal_after) |
| 868 | << Keyword; |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 869 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 870 | return; |
| 871 | } |
| 872 | ConsumeToken(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 873 | if (Keyword == Ident_message) { |
Benjamin Kramer | c561714 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 874 | if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals. |
Andy Gibbs | 97f8461 | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 875 | Diag(Tok, diag::err_expected_string_literal) |
| 876 | << /*Source='availability attribute'*/2; |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 877 | SkipUntil(tok::r_paren, StopAtSemi); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 878 | return; |
| 879 | } |
| 880 | MessageExpr = ParseStringLiteralExpression(); |
| 881 | break; |
| 882 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 883 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 884 | SourceRange VersionRange; |
| 885 | VersionTuple Version = ParseVersionTuple(VersionRange); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 886 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 887 | if (Version.empty()) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 888 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 889 | return; |
| 890 | } |
| 891 | |
| 892 | unsigned Index; |
| 893 | if (Keyword == Ident_introduced) |
| 894 | Index = Introduced; |
| 895 | else if (Keyword == Ident_deprecated) |
| 896 | Index = Deprecated; |
| 897 | else if (Keyword == Ident_obsoleted) |
| 898 | Index = Obsoleted; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 899 | else |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 900 | Index = Unknown; |
| 901 | |
| 902 | if (Index < Unknown) { |
| 903 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 904 | Diag(KeywordLoc, diag::err_availability_redundant) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 905 | << Keyword |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 906 | << SourceRange(Changes[Index].KeywordLoc, |
| 907 | Changes[Index].VersionRange.getEnd()); |
| 908 | } |
| 909 | |
| 910 | Changes[Index].KeywordLoc = KeywordLoc; |
| 911 | Changes[Index].Version = Version; |
| 912 | Changes[Index].VersionRange = VersionRange; |
| 913 | } else { |
| 914 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 915 | << Keyword << VersionRange; |
| 916 | } |
| 917 | |
| 918 | if (Tok.isNot(tok::comma)) |
| 919 | break; |
| 920 | |
| 921 | ConsumeToken(); |
| 922 | } while (true); |
| 923 | |
| 924 | // Closing ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 925 | if (T.consumeClose()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 926 | return; |
| 927 | |
| 928 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 929 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 930 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 931 | // The 'unavailable' availability cannot be combined with any other |
| 932 | // availability changes. Make sure that hasn't happened. |
| 933 | if (UnavailableLoc.isValid()) { |
| 934 | bool Complained = false; |
| 935 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 936 | if (Changes[Index].KeywordLoc.isValid()) { |
| 937 | if (!Complained) { |
| 938 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 939 | << SourceRange(Changes[Index].KeywordLoc, |
| 940 | Changes[Index].VersionRange.getEnd()); |
| 941 | Complained = true; |
| 942 | } |
| 943 | |
| 944 | // Clear out the availability. |
| 945 | Changes[Index] = AvailabilityChange(); |
| 946 | } |
| 947 | } |
| 948 | } |
| 949 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 950 | // Record this attribute |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 951 | attrs.addNew(&Availability, |
| 952 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Fariborz Jahanian | f96708d | 2012-01-23 23:38:32 +0000 | [diff] [blame] | 953 | 0, AvailabilityLoc, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 954 | Platform, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 955 | Changes[Introduced], |
| 956 | Changes[Deprecated], |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 957 | Changes[Obsoleted], |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 958 | UnavailableLoc, MessageExpr.take(), |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 959 | AttributeList::AS_GNU); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 960 | } |
| 961 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 962 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 963 | // Late Parsed Attributes: |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 964 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 965 | |
| 966 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 967 | |
| 968 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 969 | Self->ParseLexedAttributes(*Class); |
| 970 | } |
| 971 | |
| 972 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 973 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 977 | /// scope appropriately. |
| 978 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 979 | // Deal with templates |
| 980 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 981 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 982 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 983 | HasTemplateScope); |
| 984 | if (HasTemplateScope) |
| 985 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 986 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 987 | // Set or update the scope flags. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 988 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 989 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 990 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 991 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 992 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 993 | // Enter the scope of nested classes |
| 994 | if (!AlreadyHasClassScope) |
| 995 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 996 | Class.TagOrTemplate); |
Benjamin Kramer | 268efba | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 997 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 998 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 999 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 1000 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1001 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1002 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1003 | if (!AlreadyHasClassScope) |
| 1004 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 1005 | Class.TagOrTemplate); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1008 | |
| 1009 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 1010 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 1011 | bool EnterScope, bool OnDefinition) { |
DeLesley Hutchins | 161db02 | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1012 | assert(LAs.parseSoon() && |
| 1013 | "Attribute list should be marked for immediate parsing."); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1014 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 95526a4 | 2012-08-15 22:41:04 +0000 | [diff] [blame] | 1015 | if (D) |
| 1016 | LAs[i]->addDecl(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1017 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | d306cf7 | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 1018 | delete LAs[i]; |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1019 | } |
| 1020 | LAs.clear(); |
| 1021 | } |
| 1022 | |
| 1023 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1024 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 1025 | /// This will be called at the end of parsing a class declaration |
| 1026 | /// for each LateParsedAttribute. We consume the saved tokens and |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1027 | /// create an attribute with the arguments filled in. We add this |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1028 | /// to the Attribute list for the decl. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1029 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 1030 | bool EnterScope, bool OnDefinition) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1031 | // Save the current token position. |
| 1032 | SourceLocation OrigLoc = Tok.getLocation(); |
| 1033 | |
| 1034 | // Append the current token at the end of the new token stream so that it |
| 1035 | // doesn't get lost. |
| 1036 | LA.Toks.push_back(Tok); |
| 1037 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 1038 | // Consume the previously pushed token. |
Argyrios Kyrtzidis | ab2d09b | 2013-03-27 23:58:17 +0000 | [diff] [blame] | 1039 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1040 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1041 | if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) { |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1042 | // FIXME: Do not warn on C++11 attributes, once we start supporting |
| 1043 | // them here. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1044 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 1045 | << LA.AttrName.getName(); |
| 1046 | } |
| 1047 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1048 | ParsedAttributes Attrs(AttrFactory); |
| 1049 | SourceLocation endLoc; |
| 1050 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1051 | if (LA.Decls.size() > 0) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1052 | Decl *D = LA.Decls[0]; |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1053 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1054 | RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1055 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1056 | // Allow 'this' within late-parsed attributes. |
Richard Smith | cafeb94 | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 1057 | Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0, |
| 1058 | ND && ND->isCXXInstanceMember()); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1059 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1060 | if (LA.Decls.size() == 1) { |
| 1061 | // If the Decl is templatized, add template parameters to scope. |
| 1062 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 1063 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 1064 | if (HasTemplateScope) |
| 1065 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1066 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1067 | // If the Decl is on a function, add function parameters to the scope. |
| 1068 | bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
| 1069 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope); |
| 1070 | if (HasFunScope) |
| 1071 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1072 | |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1073 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 1074 | 0, SourceLocation(), AttributeList::AS_GNU); |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1075 | |
| 1076 | if (HasFunScope) { |
| 1077 | Actions.ActOnExitFunctionContext(); |
| 1078 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 1079 | } |
| 1080 | if (HasTemplateScope) { |
| 1081 | TempScope.Exit(); |
| 1082 | } |
| 1083 | } else { |
| 1084 | // If there are multiple decls, then the decl cannot be within the |
| 1085 | // function scope. |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1086 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 1087 | 0, SourceLocation(), AttributeList::AS_GNU); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1088 | } |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 1089 | } else { |
| 1090 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1093 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) { |
| 1094 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
| 1095 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1096 | |
| 1097 | if (Tok.getLocation() != OrigLoc) { |
| 1098 | // Due to a parsing error, we either went over the cached tokens or |
| 1099 | // there are still cached tokens left, so we skip the leftover tokens. |
| 1100 | // Since this is an uncommon situation that should be avoided, use the |
| 1101 | // expensive isBeforeInTranslationUnit call. |
| 1102 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 1103 | OrigLoc)) |
| 1104 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
Douglas Gregor | d78ef5b | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 1105 | ConsumeAnyToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1109 | /// \brief Wrapper around a case statement checking if AttrName is |
| 1110 | /// one of the thread safety attributes |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1111 | bool Parser::IsThreadSafetyAttribute(StringRef AttrName) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1112 | return llvm::StringSwitch<bool>(AttrName) |
| 1113 | .Case("guarded_by", true) |
| 1114 | .Case("guarded_var", true) |
| 1115 | .Case("pt_guarded_by", true) |
| 1116 | .Case("pt_guarded_var", true) |
| 1117 | .Case("lockable", true) |
| 1118 | .Case("scoped_lockable", true) |
| 1119 | .Case("no_thread_safety_analysis", true) |
| 1120 | .Case("acquired_after", true) |
| 1121 | .Case("acquired_before", true) |
| 1122 | .Case("exclusive_lock_function", true) |
| 1123 | .Case("shared_lock_function", true) |
| 1124 | .Case("exclusive_trylock_function", true) |
| 1125 | .Case("shared_trylock_function", true) |
| 1126 | .Case("unlock_function", true) |
| 1127 | .Case("lock_returned", true) |
| 1128 | .Case("locks_excluded", true) |
| 1129 | .Case("exclusive_locks_required", true) |
| 1130 | .Case("shared_locks_required", true) |
| 1131 | .Default(false); |
| 1132 | } |
| 1133 | |
| 1134 | /// \brief Parse the contents of thread safety attributes. These |
| 1135 | /// should always be parsed as an expression list. |
| 1136 | /// |
| 1137 | /// We need to special case the parsing due to the fact that if the first token |
| 1138 | /// of the first argument is an identifier, the main parse loop will store |
| 1139 | /// that token as a "parameter" and the rest of |
| 1140 | /// the arguments will be added to a list of "arguments". However, |
| 1141 | /// subsequent tokens in the first argument are lost. We instead parse each |
| 1142 | /// argument as an expression and add all arguments to the list of "arguments". |
| 1143 | /// In future, we will take advantage of this special case to also |
| 1144 | /// deal with some argument scoping issues here (for example, referring to a |
| 1145 | /// function parameter in the attribute on that function). |
| 1146 | void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName, |
| 1147 | SourceLocation AttrNameLoc, |
| 1148 | ParsedAttributes &Attrs, |
| 1149 | SourceLocation *EndLoc) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1150 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1151 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1152 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1153 | T.consumeOpen(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1154 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1155 | ArgsVector ArgExprs; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1156 | bool ArgExprsOk = true; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1157 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1158 | // now parse the list of expressions |
DeLesley Hutchins | 4805f15 | 2011-12-14 19:36:06 +0000 | [diff] [blame] | 1159 | while (Tok.isNot(tok::r_paren)) { |
DeLesley Hutchins | ed4330b | 2013-02-07 19:01:07 +0000 | [diff] [blame] | 1160 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1161 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 1162 | if (ArgExpr.isInvalid()) { |
| 1163 | ArgExprsOk = false; |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1164 | T.consumeClose(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1165 | break; |
| 1166 | } else { |
| 1167 | ArgExprs.push_back(ArgExpr.release()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1168 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1169 | if (Tok.isNot(tok::comma)) |
| 1170 | break; |
| 1171 | ConsumeToken(); // Eat the comma, move to the next argument |
| 1172 | } |
| 1173 | // Match the ')'. |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 1174 | if (ArgExprsOk && !T.consumeClose()) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1175 | Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, ArgExprs.data(), |
| 1176 | ArgExprs.size(), AttributeList::AS_GNU); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1177 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1178 | if (EndLoc) |
| 1179 | *EndLoc = T.getCloseLocation(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1182 | void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName, |
| 1183 | SourceLocation AttrNameLoc, |
| 1184 | ParsedAttributes &Attrs, |
| 1185 | SourceLocation *EndLoc) { |
| 1186 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 1187 | |
| 1188 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1189 | T.consumeOpen(); |
| 1190 | |
| 1191 | if (Tok.isNot(tok::identifier)) { |
| 1192 | Diag(Tok, diag::err_expected_ident); |
| 1193 | T.skipToEnd(); |
| 1194 | return; |
| 1195 | } |
Richard Smith | 8edabd9 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 1196 | IdentifierLoc *ArgumentKind = ParseIdentifierLoc(); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1197 | |
| 1198 | if (Tok.isNot(tok::comma)) { |
| 1199 | Diag(Tok, diag::err_expected_comma); |
| 1200 | T.skipToEnd(); |
| 1201 | return; |
| 1202 | } |
| 1203 | ConsumeToken(); |
| 1204 | |
| 1205 | SourceRange MatchingCTypeRange; |
| 1206 | TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange); |
| 1207 | if (MatchingCType.isInvalid()) { |
| 1208 | T.skipToEnd(); |
| 1209 | return; |
| 1210 | } |
| 1211 | |
| 1212 | bool LayoutCompatible = false; |
| 1213 | bool MustBeNull = false; |
| 1214 | while (Tok.is(tok::comma)) { |
| 1215 | ConsumeToken(); |
| 1216 | if (Tok.isNot(tok::identifier)) { |
| 1217 | Diag(Tok, diag::err_expected_ident); |
| 1218 | T.skipToEnd(); |
| 1219 | return; |
| 1220 | } |
| 1221 | IdentifierInfo *Flag = Tok.getIdentifierInfo(); |
| 1222 | if (Flag->isStr("layout_compatible")) |
| 1223 | LayoutCompatible = true; |
| 1224 | else if (Flag->isStr("must_be_null")) |
| 1225 | MustBeNull = true; |
| 1226 | else { |
| 1227 | Diag(Tok, diag::err_type_safety_unknown_flag) << Flag; |
| 1228 | T.skipToEnd(); |
| 1229 | return; |
| 1230 | } |
| 1231 | ConsumeToken(); // consume flag |
| 1232 | } |
| 1233 | |
| 1234 | if (!T.consumeClose()) { |
| 1235 | Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1236 | ArgumentKind, MatchingCType.release(), |
| 1237 | LayoutCompatible, MustBeNull, |
| 1238 | AttributeList::AS_GNU); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | if (EndLoc) |
| 1242 | *EndLoc = T.getCloseLocation(); |
| 1243 | } |
| 1244 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1245 | /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets |
| 1246 | /// of a C++11 attribute-specifier in a location where an attribute is not |
| 1247 | /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this |
| 1248 | /// situation. |
| 1249 | /// |
| 1250 | /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if |
| 1251 | /// this doesn't appear to actually be an attribute-specifier, and the caller |
| 1252 | /// should try to parse it. |
| 1253 | bool Parser::DiagnoseProhibitedCXX11Attribute() { |
| 1254 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)); |
| 1255 | |
| 1256 | switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) { |
| 1257 | case CAK_NotAttributeSpecifier: |
| 1258 | // No diagnostic: we're in Obj-C++11 and this is not actually an attribute. |
| 1259 | return false; |
| 1260 | |
| 1261 | case CAK_InvalidAttributeSpecifier: |
| 1262 | Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute); |
| 1263 | return false; |
| 1264 | |
| 1265 | case CAK_AttributeSpecifier: |
| 1266 | // Parse and discard the attributes. |
| 1267 | SourceLocation BeginLoc = ConsumeBracket(); |
| 1268 | ConsumeBracket(); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1269 | SkipUntil(tok::r_square); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1270 | assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied"); |
| 1271 | SourceLocation EndLoc = ConsumeBracket(); |
| 1272 | Diag(BeginLoc, diag::err_attributes_not_allowed) |
| 1273 | << SourceRange(BeginLoc, EndLoc); |
| 1274 | return true; |
| 1275 | } |
Chandler Carruth | 2c6dbd7 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 1276 | llvm_unreachable("All cases handled above."); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
Richard Smith | 975d52c | 2013-02-20 01:17:14 +0000 | [diff] [blame] | 1279 | /// \brief We have found the opening square brackets of a C++11 |
| 1280 | /// attribute-specifier in a location where an attribute is not permitted, but |
| 1281 | /// we know where the attributes ought to be written. Parse them anyway, and |
| 1282 | /// provide a fixit moving them to the right place. |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1283 | void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs, |
| 1284 | SourceLocation CorrectLocation) { |
| 1285 | assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) || |
| 1286 | Tok.is(tok::kw_alignas)); |
| 1287 | |
| 1288 | // Consume the attributes. |
| 1289 | SourceLocation Loc = Tok.getLocation(); |
| 1290 | ParseCXX11Attributes(Attrs); |
| 1291 | CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true); |
| 1292 | |
| 1293 | Diag(Loc, diag::err_attributes_not_allowed) |
| 1294 | << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange) |
| 1295 | << FixItHint::CreateRemoval(AttrRange); |
| 1296 | } |
| 1297 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1298 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 1299 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 1300 | << attrs.Range; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1303 | void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) { |
| 1304 | AttributeList *AttrList = attrs.getList(); |
| 1305 | while (AttrList) { |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1306 | if (AttrList->isCXX11Attribute()) { |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 1307 | Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr) |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1308 | << AttrList->getName(); |
| 1309 | AttrList->setInvalid(); |
| 1310 | } |
| 1311 | AttrList = AttrList->getNext(); |
| 1312 | } |
| 1313 | } |
| 1314 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1315 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 1316 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1317 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 1318 | /// location of the semicolon in DeclEnd. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1319 | /// |
| 1320 | /// declaration: [C99 6.7] |
| 1321 | /// block-declaration -> |
| 1322 | /// simple-declaration |
| 1323 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1324 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1325 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1326 | /// [C++] using-directive |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1327 | /// [C++] using-declaration |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1328 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1329 | /// others... [FIXME] |
| 1330 | /// |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1331 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 1332 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1333 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1334 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 1335 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | e8cff36 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 1336 | // Must temporarily exit the objective-c container scope for |
| 1337 | // parsing c none objective-c decls. |
| 1338 | ObjCDeclContextSwitch ObjCDC(*this); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1339 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1340 | Decl *SingleDecl = 0; |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1341 | Decl *OwnedType = 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1342 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1343 | case tok::kw_template: |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1344 | case tok::kw_export: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1345 | ProhibitAttributes(attrs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1346 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1347 | break; |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1348 | case tok::kw_inline: |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 1349 | // Could be the start of an inline namespace. Allowed as an ext in C++03. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1350 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1351 | ProhibitAttributes(attrs); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1352 | SourceLocation InlineLoc = ConsumeToken(); |
| 1353 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 1354 | break; |
| 1355 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1356 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1357 | true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1358 | case tok::kw_namespace: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1359 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1360 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1361 | break; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1362 | case tok::kw_using: |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1363 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1364 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1365 | break; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1366 | case tok::kw_static_assert: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1367 | case tok::kw__Static_assert: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1368 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1369 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1370 | break; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1371 | default: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1372 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1373 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1374 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1375 | // This routine returns a DeclGroup, if the thing we parsed only contains a |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1376 | // single decl, convert it now. Alias declarations can also declare a type; |
| 1377 | // include that too if it is present. |
| 1378 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1382 | /// declaration-specifiers init-declarator-list[opt] ';' |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1383 | /// [C++11] attribute-specifier-seq decl-specifier-seq[opt] |
| 1384 | /// init-declarator-list ';' |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1385 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 1386 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1387 | /// |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1388 | /// for-range-declaration: [C++11 6.5p1: stmt.ranged] |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1389 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1390 | /// |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1391 | /// If RequireSemi is false, this does not check for a ';' at the end of the |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1392 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1393 | /// |
| 1394 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 1395 | /// of a simple-declaration. If we find that we are, we also parse the |
| 1396 | /// for-range-initializer, and place it here. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1397 | Parser::DeclGroupPtrTy |
| 1398 | Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context, |
| 1399 | SourceLocation &DeclEnd, |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1400 | ParsedAttributesWithRange &Attrs, |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1401 | bool RequireSemi, ForRangeInit *FRI) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1402 | // Parse the common declaration-specifiers piece. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1403 | ParsingDeclSpec DS(*this); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1404 | |
Bill Wendling | f0cc19f | 2013-11-19 22:56:43 +0000 | [diff] [blame] | 1405 | DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context); |
| 1406 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext); |
| 1407 | |
| 1408 | // If we had a free-standing type definition with a missing semicolon, we |
| 1409 | // may get this far before the problem becomes obvious. |
| 1410 | if (DS.hasTagDefinition() && |
| 1411 | DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext)) |
| 1412 | return DeclGroupPtrTy(); |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1413 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1414 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1415 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1416 | if (Tok.is(tok::semi)) { |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1417 | ProhibitAttributes(Attrs); |
Argyrios Kyrtzidis | 5641b0d | 2012-05-16 23:49:15 +0000 | [diff] [blame] | 1418 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1419 | if (RequireSemi) ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1420 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1421 | DS); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1422 | DS.complete(TheDecl); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1423 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1424 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1425 | |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1426 | DS.takeAttributesFrom(Attrs); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1427 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1428 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1429 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1430 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1431 | /// for a declarator. |
| 1432 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1433 | switch (Tok.getKind()) { |
| 1434 | case tok::annot_cxxscope: |
| 1435 | case tok::annot_template_id: |
| 1436 | case tok::caret: |
| 1437 | case tok::code_completion: |
| 1438 | case tok::coloncolon: |
| 1439 | case tok::ellipsis: |
| 1440 | case tok::kw___attribute: |
| 1441 | case tok::kw_operator: |
| 1442 | case tok::l_paren: |
| 1443 | case tok::star: |
| 1444 | return true; |
| 1445 | |
| 1446 | case tok::amp: |
| 1447 | case tok::ampamp: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1448 | return getLangOpts().CPlusPlus; |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1449 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1450 | case tok::l_square: // Might be an attribute on an unnamed bit-field. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1451 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 && |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1452 | NextToken().is(tok::l_square); |
| 1453 | |
| 1454 | case tok::colon: // Might be a typo for '::' or an unnamed bit-field. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1455 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1456 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1457 | case tok::identifier: |
| 1458 | switch (NextToken().getKind()) { |
| 1459 | case tok::code_completion: |
| 1460 | case tok::coloncolon: |
| 1461 | case tok::comma: |
| 1462 | case tok::equal: |
| 1463 | case tok::equalequal: // Might be a typo for '='. |
| 1464 | case tok::kw_alignas: |
| 1465 | case tok::kw_asm: |
| 1466 | case tok::kw___attribute: |
| 1467 | case tok::l_brace: |
| 1468 | case tok::l_paren: |
| 1469 | case tok::l_square: |
| 1470 | case tok::less: |
| 1471 | case tok::r_brace: |
| 1472 | case tok::r_paren: |
| 1473 | case tok::r_square: |
| 1474 | case tok::semi: |
| 1475 | return true; |
| 1476 | |
| 1477 | case tok::colon: |
| 1478 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1479 | // and in block scope it's probably a label. Inside a class definition, |
| 1480 | // this is a bit-field. |
| 1481 | return Context == Declarator::MemberContext || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1482 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1483 | |
| 1484 | case tok::identifier: // Possible virt-specifier. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1485 | return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken()); |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1486 | |
| 1487 | default: |
| 1488 | return false; |
| 1489 | } |
| 1490 | |
| 1491 | default: |
| 1492 | return false; |
| 1493 | } |
| 1494 | } |
| 1495 | |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1496 | /// Skip until we reach something which seems like a sensible place to pick |
| 1497 | /// up parsing after a malformed declaration. This will sometimes stop sooner |
| 1498 | /// than SkipUntil(tok::r_brace) would, but will never stop later. |
| 1499 | void Parser::SkipMalformedDecl() { |
| 1500 | while (true) { |
| 1501 | switch (Tok.getKind()) { |
| 1502 | case tok::l_brace: |
| 1503 | // Skip until matching }, then stop. We've probably skipped over |
| 1504 | // a malformed class or function definition or similar. |
| 1505 | ConsumeBrace(); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1506 | SkipUntil(tok::r_brace); |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1507 | if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) { |
| 1508 | // This declaration isn't over yet. Keep skipping. |
| 1509 | continue; |
| 1510 | } |
| 1511 | if (Tok.is(tok::semi)) |
| 1512 | ConsumeToken(); |
| 1513 | return; |
| 1514 | |
| 1515 | case tok::l_square: |
| 1516 | ConsumeBracket(); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1517 | SkipUntil(tok::r_square); |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1518 | continue; |
| 1519 | |
| 1520 | case tok::l_paren: |
| 1521 | ConsumeParen(); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1522 | SkipUntil(tok::r_paren); |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1523 | continue; |
| 1524 | |
| 1525 | case tok::r_brace: |
| 1526 | return; |
| 1527 | |
| 1528 | case tok::semi: |
| 1529 | ConsumeToken(); |
| 1530 | return; |
| 1531 | |
| 1532 | case tok::kw_inline: |
| 1533 | // 'inline namespace' at the start of a line is almost certainly |
Jordan Rose | 94f29f4 | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1534 | // a good place to pick back up parsing, except in an Objective-C |
| 1535 | // @interface context. |
| 1536 | if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) && |
| 1537 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1538 | return; |
| 1539 | break; |
| 1540 | |
| 1541 | case tok::kw_namespace: |
| 1542 | // 'namespace' at the start of a line is almost certainly a good |
Jordan Rose | 94f29f4 | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1543 | // place to pick back up parsing, except in an Objective-C |
| 1544 | // @interface context. |
| 1545 | if (Tok.isAtStartOfLine() && |
| 1546 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
| 1547 | return; |
| 1548 | break; |
| 1549 | |
| 1550 | case tok::at: |
| 1551 | // @end is very much like } in Objective-C contexts. |
| 1552 | if (NextToken().isObjCAtKeyword(tok::objc_end) && |
| 1553 | ParsingInObjCContainer) |
| 1554 | return; |
| 1555 | break; |
| 1556 | |
| 1557 | case tok::minus: |
| 1558 | case tok::plus: |
| 1559 | // - and + probably start new method declarations in Objective-C contexts. |
| 1560 | if (Tok.isAtStartOfLine() && ParsingInObjCContainer) |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1561 | return; |
| 1562 | break; |
| 1563 | |
| 1564 | case tok::eof: |
| 1565 | return; |
| 1566 | |
| 1567 | default: |
| 1568 | break; |
| 1569 | } |
| 1570 | |
| 1571 | ConsumeAnyToken(); |
| 1572 | } |
| 1573 | } |
| 1574 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1575 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1576 | /// definition or a group of object declarations, actually parse the |
| 1577 | /// result. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1578 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1579 | unsigned Context, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1580 | bool AllowFunctionDefinitions, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1581 | SourceLocation *DeclEnd, |
| 1582 | ForRangeInit *FRI) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1583 | // Parse the first declarator. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1584 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1585 | ParseDeclarator(D); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1586 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1587 | // Bail out if the first declarator didn't seem well-formed. |
| 1588 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1589 | SkipMalformedDecl(); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1590 | return DeclGroupPtrTy(); |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1591 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1592 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1593 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1594 | // appropriate function scope after the function Decl has been constructed. |
DeLesley Hutchins | 161db02 | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1595 | // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList. |
| 1596 | LateParsedAttrList LateParsedAttrs(true); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1597 | if (D.isFunctionDeclarator()) |
| 1598 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1599 | |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1600 | // Check to see if we have a function *definition* which must have a body. |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1601 | if (D.isFunctionDeclarator() && |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1602 | // Look at the next token to make sure that this isn't a function |
| 1603 | // declaration. We have to check this because __attribute__ might be the |
| 1604 | // start of a function definition in GCC-extended K&R C. |
Fariborz Jahanian | be1d4ec | 2012-08-10 15:54:40 +0000 | [diff] [blame] | 1605 | !isDeclarationAfterDeclarator()) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1606 | |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1607 | if (AllowFunctionDefinitions) { |
| 1608 | if (isStartOfFunctionDefinition(D)) { |
| 1609 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1610 | Diag(Tok, diag::err_function_declared_typedef); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1611 | |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1612 | // Recover by treating the 'typedef' as spurious. |
| 1613 | DS.ClearStorageClassSpecs(); |
| 1614 | } |
| 1615 | |
| 1616 | Decl *TheDecl = |
| 1617 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
| 1618 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1621 | if (isDeclarationSpecifier()) { |
| 1622 | // If there is an invalid declaration specifier right after the function |
| 1623 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1624 | // actually a body. Just fall through into the code that handles it as a |
| 1625 | // prototype, and let the top-level code handle the erroneous declspec |
| 1626 | // where it would otherwise expect a comma or semicolon. |
| 1627 | } else { |
| 1628 | Diag(Tok, diag::err_expected_fn_body); |
| 1629 | SkipUntil(tok::semi); |
| 1630 | return DeclGroupPtrTy(); |
| 1631 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1632 | } else { |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1633 | if (Tok.is(tok::l_brace)) { |
| 1634 | Diag(Tok, diag::err_function_definition_not_allowed); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1635 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1636 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1637 | } |
| 1638 | } |
| 1639 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1640 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1641 | return DeclGroupPtrTy(); |
| 1642 | |
| 1643 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1644 | // must parse and analyze the for-range-initializer before the declaration is |
| 1645 | // analyzed. |
Douglas Gregor | 12849d0 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1646 | // |
| 1647 | // Handle the Objective-C for-in loop variable similarly, although we |
| 1648 | // don't need to parse the container in advance. |
| 1649 | if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) { |
| 1650 | bool IsForRangeLoop = false; |
| 1651 | if (Tok.is(tok::colon)) { |
| 1652 | IsForRangeLoop = true; |
| 1653 | FRI->ColonLoc = ConsumeToken(); |
| 1654 | if (Tok.is(tok::l_brace)) |
| 1655 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1656 | else |
| 1657 | FRI->RangeExpr = ParseExpression(); |
| 1658 | } |
| 1659 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1660 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | 12849d0 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1661 | if (IsForRangeLoop) |
| 1662 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1663 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | 6895a64 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 1664 | D.complete(ThisDecl); |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1665 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1666 | } |
| 1667 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1668 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1669 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1670 | if (LateParsedAttrs.size() > 0) |
| 1671 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1672 | D.complete(FirstDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1673 | if (FirstDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1674 | DeclsInGroup.push_back(FirstDecl); |
| 1675 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1676 | bool ExpectSemi = Context != Declarator::ForContext; |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 1677 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1678 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1679 | // error, bail out. |
| 1680 | while (Tok.is(tok::comma)) { |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1681 | SourceLocation CommaLoc = ConsumeToken(); |
| 1682 | |
| 1683 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1684 | // This comma was followed by a line-break and something which can't be |
| 1685 | // the start of a declarator. The comma was probably a typo for a |
| 1686 | // semicolon. |
| 1687 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1688 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1689 | ExpectSemi = false; |
| 1690 | break; |
| 1691 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1692 | |
| 1693 | // Parse the next declarator. |
| 1694 | D.clear(); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1695 | D.setCommaLoc(CommaLoc); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1696 | |
| 1697 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1698 | // declaration, these would be part of the declspec. In subsequent |
| 1699 | // declarators, they become part of the declarator itself, so that they |
| 1700 | // don't apply to declarators after *this* one. Examples: |
| 1701 | // short __attribute__((common)) var; -> declspec |
| 1702 | // short var __attribute__((common)); -> declarator |
| 1703 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1704 | MaybeParseGNUAttributes(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1705 | |
| 1706 | ParseDeclarator(D); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1707 | if (!D.isInvalidType()) { |
| 1708 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1709 | D.complete(ThisDecl); |
| 1710 | if (ThisDecl) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1711 | DeclsInGroup.push_back(ThisDecl); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1712 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1713 | } |
| 1714 | |
| 1715 | if (DeclEnd) |
| 1716 | *DeclEnd = Tok.getLocation(); |
| 1717 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1718 | if (ExpectSemi && |
Chris Lattner | 8bb21d3 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 1719 | ExpectAndConsumeSemi(Context == Declarator::FileContext |
| 1720 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1721 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1722 | // Okay, there was no semicolon and one was expected. If we see a |
| 1723 | // declaration specifier, just assume it was missing and continue parsing. |
| 1724 | // Otherwise things are very confused and we skip to recover. |
| 1725 | if (!isDeclarationSpecifier()) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1726 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1727 | if (Tok.is(tok::semi)) |
| 1728 | ConsumeToken(); |
| 1729 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1732 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1735 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1736 | /// declarator. Returns true on an error. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1737 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1738 | // If a simple-asm-expr is present, parse it. |
| 1739 | if (Tok.is(tok::kw_asm)) { |
| 1740 | SourceLocation Loc; |
| 1741 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1742 | if (AsmLabel.isInvalid()) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1743 | SkipUntil(tok::semi, StopBeforeMatch); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1744 | return true; |
| 1745 | } |
| 1746 | |
| 1747 | D.setAsmLabel(AsmLabel.release()); |
| 1748 | D.SetRangeEnd(Loc); |
| 1749 | } |
| 1750 | |
| 1751 | MaybeParseGNUAttributes(D); |
| 1752 | return false; |
| 1753 | } |
| 1754 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1755 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1756 | /// declarator'. This method parses the remainder of the declaration |
| 1757 | /// (including any attributes or initializer, among other things) and |
| 1758 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1759 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1760 | /// init-declarator: [C99 6.7] |
| 1761 | /// declarator |
| 1762 | /// declarator '=' initializer |
| 1763 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1764 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1765 | /// [C++] declarator initializer[opt] |
| 1766 | /// |
| 1767 | /// [C++] initializer: |
| 1768 | /// [C++] '=' initializer-clause |
| 1769 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1770 | /// [C++0x] '=' 'default' [TODO] |
| 1771 | /// [C++0x] '=' 'delete' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1772 | /// [C++0x] braced-init-list |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1773 | /// |
| 1774 | /// According to the standard grammar, =default and =delete are function |
| 1775 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1776 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1777 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1778 | const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1779 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1780 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1781 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1782 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1783 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1784 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1785 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1786 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1787 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1788 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1789 | switch (TemplateInfo.Kind) { |
| 1790 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1791 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1792 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1793 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1794 | case ParsedTemplateInfo::Template: |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1795 | case ParsedTemplateInfo::ExplicitSpecialization: { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1796 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1797 | *TemplateInfo.TemplateParams, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1798 | D); |
Larisse Voufo | 2521813 | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 1799 | if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl)) |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1800 | // Re-direct this decl to refer to the templated decl so that we can |
| 1801 | // initialize it. |
| 1802 | ThisDecl = VT->getTemplatedDecl(); |
| 1803 | break; |
| 1804 | } |
| 1805 | case ParsedTemplateInfo::ExplicitInstantiation: { |
| 1806 | if (Tok.is(tok::semi)) { |
| 1807 | DeclResult ThisRes = Actions.ActOnExplicitInstantiation( |
| 1808 | getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D); |
| 1809 | if (ThisRes.isInvalid()) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1810 | SkipUntil(tok::semi, StopBeforeMatch); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1811 | return 0; |
| 1812 | } |
| 1813 | ThisDecl = ThisRes.get(); |
| 1814 | } else { |
| 1815 | // FIXME: This check should be for a variable template instantiation only. |
| 1816 | |
| 1817 | // Check that this is a valid instantiation |
| 1818 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 1819 | // If the declarator-id is not a template-id, issue a diagnostic and |
| 1820 | // recover by ignoring the 'template' keyword. |
| 1821 | Diag(Tok, diag::err_template_defn_explicit_instantiation) |
| 1822 | << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc); |
| 1823 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1824 | } else { |
| 1825 | SourceLocation LAngleLoc = |
| 1826 | PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
| 1827 | Diag(D.getIdentifierLoc(), |
| 1828 | diag::err_explicit_instantiation_with_definition) |
| 1829 | << SourceRange(TemplateInfo.TemplateLoc) |
| 1830 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
| 1831 | |
| 1832 | // Recover as if it were an explicit specialization. |
| 1833 | TemplateParameterLists FakedParamLists; |
| 1834 | FakedParamLists.push_back(Actions.ActOnTemplateParameterList( |
| 1835 | 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0, |
| 1836 | LAngleLoc)); |
| 1837 | |
| 1838 | ThisDecl = |
| 1839 | Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D); |
| 1840 | } |
| 1841 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1842 | break; |
| 1843 | } |
| 1844 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1845 | |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1846 | bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType(); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1847 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1848 | // Parse declarator '=' initializer. |
Richard Trieu | d6c7c67 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1849 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | fcaf27e | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1850 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1851 | ConsumeToken(); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1852 | |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1853 | if (Tok.is(tok::kw_delete)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1854 | if (D.isFunctionDeclarator()) |
| 1855 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1856 | << 1 /* delete */; |
| 1857 | else |
| 1858 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Sean Hunt | fe2695e | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1859 | } else if (Tok.is(tok::kw_default)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1860 | if (D.isFunctionDeclarator()) |
Sebastian Redl | ecfcd56 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1861 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1862 | << 0 /* default */; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1863 | else |
| 1864 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1865 | } else { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1866 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1867 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1868 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1869 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1870 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1871 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1872 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Peter Collingbourne | ec98f2f | 2012-07-27 12:56:09 +0000 | [diff] [blame] | 1873 | Actions.FinalizeDeclaration(ThisDecl); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1874 | cutOffParsing(); |
| 1875 | return 0; |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1876 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1877 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1878 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1879 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1880 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1881 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1882 | ExitScope(); |
| 1883 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1884 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1885 | if (Init.isInvalid()) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1886 | SkipUntil(tok::comma, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1887 | Actions.ActOnInitializerError(ThisDecl); |
| 1888 | } else |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1889 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1890 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1891 | } |
| 1892 | } else if (Tok.is(tok::l_paren)) { |
| 1893 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1894 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1895 | T.consumeOpen(); |
| 1896 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1897 | ExprVector Exprs; |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1898 | CommaLocsTy CommaLocs; |
| 1899 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1900 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1901 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1902 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1905 | if (ParseExpressionList(Exprs, CommaLocs)) { |
David Blaikie | 3ea19c8 | 2012-10-10 23:15:05 +0000 | [diff] [blame] | 1906 | Actions.ActOnInitializerError(ThisDecl); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 1907 | SkipUntil(tok::r_paren, StopAtSemi); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1908 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1909 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1910 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1911 | ExitScope(); |
| 1912 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1913 | } else { |
| 1914 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1915 | T.consumeClose(); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1916 | |
| 1917 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1918 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1919 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1920 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1921 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1922 | ExitScope(); |
| 1923 | } |
| 1924 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1925 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 1926 | T.getCloseLocation(), |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1927 | Exprs); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1928 | Actions.AddInitializerToDecl(ThisDecl, Initializer.take(), |
| 1929 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1930 | } |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1931 | } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) && |
Fariborz Jahanian | b0ed95c | 2012-07-03 23:22:13 +0000 | [diff] [blame] | 1932 | (!CurParsedObjCImpl || !D.isFunctionDeclarator())) { |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1933 | // Parse C++0x braced-init-list. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1934 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1935 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1936 | if (D.getCXXScopeSpec().isSet()) { |
| 1937 | EnterScope(0); |
| 1938 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1939 | } |
| 1940 | |
| 1941 | ExprResult Init(ParseBraceInitializer()); |
| 1942 | |
| 1943 | if (D.getCXXScopeSpec().isSet()) { |
| 1944 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1945 | ExitScope(); |
| 1946 | } |
| 1947 | |
| 1948 | if (Init.isInvalid()) { |
| 1949 | Actions.ActOnInitializerError(ThisDecl); |
| 1950 | } else |
| 1951 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1952 | /*DirectInit=*/true, TypeContainsAuto); |
| 1953 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1954 | } else { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1955 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1958 | Actions.FinalizeDeclaration(ThisDecl); |
| 1959 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1960 | return ThisDecl; |
| 1961 | } |
| 1962 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1963 | /// ParseSpecifierQualifierList |
| 1964 | /// specifier-qualifier-list: |
| 1965 | /// type-specifier specifier-qualifier-list[opt] |
| 1966 | /// type-qualifier specifier-qualifier-list[opt] |
| 1967 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 1968 | /// |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1969 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 1970 | DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1971 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1972 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1973 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1974 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1975 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1976 | // Validate declspec for type-name. |
| 1977 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 1978 | if ((DSC == DSC_type_specifier || DSC == DSC_trailing) && |
| 1979 | !DS.hasTypeSpecifier()) { |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1980 | Diag(Tok, diag::err_expected_type); |
| 1981 | DS.SetTypeSpecError(); |
| 1982 | } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 1983 | !DS.hasAttributes()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1984 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1985 | if (!DS.hasTypeSpecifier()) |
| 1986 | DS.SetTypeSpecError(); |
| 1987 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1988 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1989 | // Issue diagnostic and remove storage class if present. |
| 1990 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 1991 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1992 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 1993 | else |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 1994 | Diag(DS.getThreadStorageClassSpecLoc(), |
| 1995 | diag::err_typename_invalid_storageclass); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1996 | DS.ClearStorageClassSpecs(); |
| 1997 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1998 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1999 | // Issue diagnostic and remove function specfier if present. |
| 2000 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2001 | if (DS.isInlineSpecified()) |
| 2002 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 2003 | if (DS.isVirtualSpecified()) |
| 2004 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 2005 | if (DS.isExplicitSpecified()) |
| 2006 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2007 | DS.ClearFunctionSpecs(); |
| 2008 | } |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2009 | |
| 2010 | // Issue diagnostic and remove constexpr specfier if present. |
| 2011 | if (DS.isConstexprSpecified()) { |
| 2012 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 2013 | DS.ClearConstexprSpec(); |
| 2014 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2017 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 2018 | /// specified token is valid after the identifier in a declarator which |
| 2019 | /// immediately follows the declspec. For example, these things are valid: |
| 2020 | /// |
| 2021 | /// int x [ 4]; // direct-declarator |
| 2022 | /// int x ( int y); // direct-declarator |
| 2023 | /// int(int x ) // direct-declarator |
| 2024 | /// int x ; // simple-declaration |
| 2025 | /// int x = 17; // init-declarator-list |
| 2026 | /// int x , y; // init-declarator-list |
| 2027 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2028 | /// int x : 4; // struct-declarator |
Chris Lattner | c83c27a | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 2029 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2030 | /// |
| 2031 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 2032 | /// ')' happens to be valid anyway). |
| 2033 | /// int (x) |
| 2034 | /// |
| 2035 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 2036 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 2037 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2038 | T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon); |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2039 | } |
| 2040 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2041 | |
| 2042 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 2043 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 2044 | /// the declspec has no type specifier. In this case, the declspec is either |
| 2045 | /// malformed or is "implicit int" (in K&R and C89). |
| 2046 | /// |
| 2047 | /// This method handles diagnosing this prettily and returns false if the |
| 2048 | /// declspec is done being processed. If it recovers and thinks there may be |
| 2049 | /// other pieces of declspec after it, it returns true. |
| 2050 | /// |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2051 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2052 | const ParsedTemplateInfo &TemplateInfo, |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2053 | AccessSpecifier AS, DeclSpecContext DSC, |
| 2054 | ParsedAttributesWithRange &Attrs) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2055 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2056 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2057 | SourceLocation Loc = Tok.getLocation(); |
| 2058 | // If we see an identifier that is not a type name, we normally would |
| 2059 | // parse it as the identifer being declared. However, when a typename |
| 2060 | // is typo'd or the definition is not included, this will incorrectly |
| 2061 | // parse the typename as the identifier name and fall over misparsing |
| 2062 | // later parts of the diagnostic. |
| 2063 | // |
| 2064 | // As such, we try to do some look-ahead in cases where this would |
| 2065 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 2066 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 2067 | // an identifier with implicit int, we'd get a parse error because the |
| 2068 | // next token is obviously invalid for a type. Parse these as a case |
| 2069 | // with an invalid type specifier. |
| 2070 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2071 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2072 | // Since we know that this either implicit int (which is rare) or an |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2073 | // error, do lookahead to try to do better recovery. This never applies |
| 2074 | // within a type specifier. Outside of C++, we allow this even if the |
| 2075 | // language doesn't "officially" support implicit int -- we support |
Richard Smith | 58eb370 | 2013-04-30 22:43:51 +0000 | [diff] [blame] | 2076 | // implicit int as an extension in C99 and C11. |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 2077 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
Richard Smith | 58eb370 | 2013-04-30 22:43:51 +0000 | [diff] [blame] | 2078 | !getLangOpts().CPlusPlus && |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2079 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2080 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 2081 | // we just avoid eating the identifier, so it will be parsed as the |
| 2082 | // identifier in the declarator. |
| 2083 | return false; |
| 2084 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2085 | |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2086 | if (getLangOpts().CPlusPlus && |
| 2087 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 2088 | // Don't require a type specifier if we have the 'auto' storage class |
| 2089 | // specifier in C++98 -- we'll promote it to a type specifier. |
Richard Smith | b79b17b | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2090 | if (SS) |
| 2091 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2092 | return false; |
| 2093 | } |
| 2094 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2095 | // Otherwise, if we don't consume this token, we are going to emit an |
| 2096 | // error anyway. Try to recover from various common problems. Check |
| 2097 | // to see if this was a reference to a tag name without a tag specified. |
| 2098 | // This is a common problem in C (saying 'foo' instead of 'struct foo'). |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2099 | // |
| 2100 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 2101 | if (SS == 0) { |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2102 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2103 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2104 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2105 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2106 | default: break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2107 | case DeclSpec::TST_enum: |
| 2108 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 2109 | case DeclSpec::TST_union: |
| 2110 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 2111 | case DeclSpec::TST_struct: |
| 2112 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2113 | case DeclSpec::TST_interface: |
| 2114 | TagName="__interface"; FixitTagName = "__interface "; |
| 2115 | TagKind=tok::kw___interface;break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2116 | case DeclSpec::TST_class: |
| 2117 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2118 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2119 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2120 | if (TagName) { |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2121 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 2122 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 2123 | Sema::LookupOrdinaryName); |
| 2124 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2125 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2126 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 2127 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 2128 | |
| 2129 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 2130 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 2131 | I != IEnd; ++I) |
Kaelyn Uhrain | 392b3f5 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 2132 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2133 | << TokenName << TagName; |
| 2134 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2135 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2136 | // Parse this as a tag as if the missing tag were present. |
| 2137 | if (TagKind == tok::kw_enum) |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2138 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2139 | else |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2140 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2141 | /*EnteringContext*/ false, DSC_normal, Attrs); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2142 | return true; |
| 2143 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2144 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2145 | |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2146 | // Determine whether this identifier could plausibly be the name of something |
Richard Smith | 7514db2 | 2012-05-15 21:42:17 +0000 | [diff] [blame] | 2147 | // being declared (with a missing type). |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2148 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
| 2149 | (!SS || DSC == DSC_top_level || DSC == DSC_class)) { |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2150 | // Look ahead to the next token to try to figure out what this declaration |
| 2151 | // was supposed to be. |
| 2152 | switch (NextToken().getKind()) { |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2153 | case tok::l_paren: { |
| 2154 | // static x(4); // 'x' is not a type |
| 2155 | // x(int n); // 'x' is not a type |
| 2156 | // x (*p)[]; // 'x' is a type |
| 2157 | // |
| 2158 | // Since we're in an error case (or the rare 'implicit int in C++' MS |
| 2159 | // extension), we can afford to perform a tentative parse to determine |
| 2160 | // which case we're in. |
| 2161 | TentativeParsingAction PA(*this); |
| 2162 | ConsumeToken(); |
| 2163 | TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false); |
| 2164 | PA.Revert(); |
Richard Smith | b79b17b | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2165 | |
| 2166 | if (TPR != TPResult::False()) { |
| 2167 | // The identifier is followed by a parenthesized declarator. |
| 2168 | // It's supposed to be a type. |
| 2169 | break; |
| 2170 | } |
| 2171 | |
| 2172 | // If we're in a context where we could be declaring a constructor, |
| 2173 | // check whether this is a constructor declaration with a bogus name. |
| 2174 | if (DSC == DSC_class || (DSC == DSC_top_level && SS)) { |
| 2175 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2176 | if (Actions.isCurrentClassNameTypo(II, SS)) { |
| 2177 | Diag(Loc, diag::err_constructor_bad_name) |
| 2178 | << Tok.getIdentifierInfo() << II |
| 2179 | << FixItHint::CreateReplacement(Tok.getLocation(), II->getName()); |
| 2180 | Tok.setIdentifierInfo(II); |
| 2181 | } |
| 2182 | } |
| 2183 | // Fall through. |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2184 | } |
Richard Smith | b79b17b | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2185 | case tok::comma: |
| 2186 | case tok::equal: |
| 2187 | case tok::kw_asm: |
| 2188 | case tok::l_brace: |
| 2189 | case tok::l_square: |
| 2190 | case tok::semi: |
| 2191 | // This looks like a variable or function declaration. The type is |
| 2192 | // probably missing. We're done parsing decl-specifiers. |
| 2193 | if (SS) |
| 2194 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
| 2195 | return false; |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2196 | |
| 2197 | default: |
| 2198 | // This is probably supposed to be a type. This includes cases like: |
| 2199 | // int f(itn); |
| 2200 | // struct S { unsinged : 4; }; |
| 2201 | break; |
| 2202 | } |
| 2203 | } |
| 2204 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2205 | // This is almost certainly an invalid type name. Let the action emit a |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2206 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2207 | ParsedType T; |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2208 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2209 | if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2210 | // The action emitted a diagnostic, so we don't have to. |
| 2211 | if (T) { |
| 2212 | // The action has suggested that the type T could be used. Set that as |
| 2213 | // the type in the declaration specifiers, consume the would-be type |
| 2214 | // name token, and we're done. |
| 2215 | const char *PrevSpec; |
| 2216 | unsigned DiagID; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2217 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2218 | DS.SetRangeEnd(Tok.getLocation()); |
| 2219 | ConsumeToken(); |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2220 | // There may be other declaration specifiers after this. |
| 2221 | return true; |
| 2222 | } else if (II != Tok.getIdentifierInfo()) { |
| 2223 | // If no type was suggested, the correction is to a keyword |
| 2224 | Tok.setKind(II->getTokenID()); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2225 | // There may be other declaration specifiers after this. |
| 2226 | return true; |
| 2227 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2228 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2229 | // Fall through; the action had no suggestion for us. |
| 2230 | } else { |
| 2231 | // The action did not emit a diagnostic, so emit one now. |
| 2232 | SourceRange R; |
| 2233 | if (SS) R = SS->getRange(); |
| 2234 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 2235 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2236 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2237 | // Mark this as an error. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2238 | DS.SetTypeSpecError(); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2239 | DS.SetRangeEnd(Tok.getLocation()); |
| 2240 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2241 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2242 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 2243 | // avoid rippling error messages on subsequent uses of the same type, |
| 2244 | // could be useful if #include was forgotten. |
| 2245 | return false; |
| 2246 | } |
| 2247 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2248 | /// \brief Determine the declaration specifier context from the declarator |
| 2249 | /// context. |
| 2250 | /// |
| 2251 | /// \param Context the declarator context, which is one of the |
| 2252 | /// Declarator::TheContext enumerator values. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2253 | Parser::DeclSpecContext |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2254 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 2255 | if (Context == Declarator::MemberContext) |
| 2256 | return DSC_class; |
| 2257 | if (Context == Declarator::FileContext) |
| 2258 | return DSC_top_level; |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 2259 | if (Context == Declarator::TrailingReturnContext) |
| 2260 | return DSC_trailing; |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2261 | return DSC_normal; |
| 2262 | } |
| 2263 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2264 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 2265 | /// |
| 2266 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 2267 | /// type. Ideally, the type should be propagated directly into Sema. |
| 2268 | /// |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2269 | /// [C11] type-id |
| 2270 | /// [C11] constant-expression |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2271 | /// [C++0x] type-id ...[opt] |
| 2272 | /// [C++0x] assignment-expression ...[opt] |
| 2273 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 2274 | SourceLocation &EllipsisLoc) { |
| 2275 | ExprResult ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2276 | if (isTypeIdInParens()) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2277 | SourceLocation TypeLoc = Tok.getLocation(); |
| 2278 | ParsedType Ty = ParseTypeName().get(); |
| 2279 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2280 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 2281 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2282 | } else |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2283 | ER = ParseConstantExpression(); |
| 2284 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2285 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis)) |
Peter Collingbourne | fe9b2a8 | 2011-10-24 17:56:00 +0000 | [diff] [blame] | 2286 | EllipsisLoc = ConsumeToken(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2287 | |
| 2288 | return ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2289 | } |
| 2290 | |
| 2291 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 2292 | /// attribute to Attrs. |
| 2293 | /// |
| 2294 | /// alignment-specifier: |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2295 | /// [C11] '_Alignas' '(' type-id ')' |
| 2296 | /// [C11] '_Alignas' '(' constant-expression ')' |
Richard Smith | 33f04a2 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2297 | /// [C++11] 'alignas' '(' type-id ...[opt] ')' |
| 2298 | /// [C++11] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2299 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2300 | SourceLocation *EndLoc) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2301 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 2302 | "Not an alignment-specifier!"); |
| 2303 | |
Richard Smith | 33f04a2 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2304 | IdentifierInfo *KWName = Tok.getIdentifierInfo(); |
| 2305 | SourceLocation KWLoc = ConsumeToken(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2306 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2307 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2308 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2309 | return; |
| 2310 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2311 | SourceLocation EllipsisLoc; |
| 2312 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2313 | if (ArgExpr.isInvalid()) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 2314 | T.skipToEnd(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2315 | return; |
| 2316 | } |
| 2317 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2318 | T.consumeClose(); |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2319 | if (EndLoc) |
| 2320 | *EndLoc = T.getCloseLocation(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2321 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2322 | ArgsVector ArgExprs; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2323 | ArgExprs.push_back(ArgExpr.release()); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2324 | Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1, |
| 2325 | AttributeList::AS_Keyword, EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2326 | } |
| 2327 | |
Bill Wendling | f0cc19f | 2013-11-19 22:56:43 +0000 | [diff] [blame] | 2328 | /// Determine whether we're looking at something that might be a declarator |
| 2329 | /// in a simple-declaration. If it can't possibly be a declarator, maybe |
| 2330 | /// diagnose a missing semicolon after a prior tag definition in the decl |
| 2331 | /// specifier. |
| 2332 | /// |
| 2333 | /// \return \c true if an error occurred and this can't be any kind of |
| 2334 | /// declaration. |
| 2335 | bool |
| 2336 | Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS, |
| 2337 | DeclSpecContext DSContext, |
| 2338 | LateParsedAttrList *LateAttrs) { |
| 2339 | assert(DS.hasTagDefinition() && "shouldn't call this"); |
| 2340 | |
| 2341 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
| 2342 | bool HasMissingSemi = false; |
| 2343 | |
| 2344 | if (getLangOpts().CPlusPlus && |
| 2345 | (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) || |
| 2346 | Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) && |
| 2347 | TryAnnotateCXXScopeToken(EnteringContext)) { |
| 2348 | SkipMalformedDecl(); |
| 2349 | return true; |
| 2350 | } |
| 2351 | |
| 2352 | // Determine whether the following tokens could possibly be a |
| 2353 | // declarator. |
| 2354 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 2355 | const Token &Next = NextToken(); |
| 2356 | // These tokens cannot come after the declarator-id in a |
| 2357 | // simple-declaration, and are likely to come after a type-specifier. |
| 2358 | HasMissingSemi = Next.is(tok::star) || Next.is(tok::amp) || |
| 2359 | Next.is(tok::ampamp) || Next.is(tok::identifier) || |
| 2360 | Next.is(tok::annot_cxxscope) || |
| 2361 | Next.is(tok::coloncolon); |
| 2362 | } else if (Tok.is(tok::annot_cxxscope) && |
| 2363 | NextToken().is(tok::identifier) && |
| 2364 | DS.getStorageClassSpec() != DeclSpec::SCS_typedef) { |
| 2365 | // We almost certainly have a missing semicolon. Look up the name and |
| 2366 | // check; if it names a type, we're missing a semicolon. |
| 2367 | CXXScopeSpec SS; |
| 2368 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 2369 | Tok.getAnnotationRange(), SS); |
| 2370 | const Token &Next = NextToken(); |
| 2371 | IdentifierInfo *Name = Next.getIdentifierInfo(); |
| 2372 | Sema::NameClassification Classification = |
| 2373 | Actions.ClassifyName(getCurScope(), SS, Name, Next.getLocation(), |
| 2374 | NextToken(), /*IsAddressOfOperand*/false); |
| 2375 | switch (Classification.getKind()) { |
| 2376 | case Sema::NC_Error: |
| 2377 | SkipMalformedDecl(); |
| 2378 | return true; |
| 2379 | |
| 2380 | case Sema::NC_Keyword: |
| 2381 | case Sema::NC_NestedNameSpecifier: |
| 2382 | llvm_unreachable("typo correction and nested name specifiers not " |
| 2383 | "possible here"); |
| 2384 | |
| 2385 | case Sema::NC_Type: |
| 2386 | case Sema::NC_TypeTemplate: |
| 2387 | // Not a previously-declared non-type entity. |
| 2388 | HasMissingSemi = true; |
| 2389 | break; |
| 2390 | |
| 2391 | case Sema::NC_Unknown: |
| 2392 | case Sema::NC_Expression: |
| 2393 | case Sema::NC_VarTemplate: |
| 2394 | case Sema::NC_FunctionTemplate: |
| 2395 | // Might be a redeclaration of a prior entity. |
| 2396 | HasMissingSemi = false; |
| 2397 | break; |
| 2398 | } |
| 2399 | } else if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) { |
| 2400 | HasMissingSemi = true; |
| 2401 | } |
| 2402 | |
| 2403 | if (!HasMissingSemi) |
| 2404 | return false; |
| 2405 | |
| 2406 | Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()), |
| 2407 | diag::err_expected_semi_after_tagdecl) |
| 2408 | << DeclSpec::getSpecifierName(DS.getTypeSpecType()); |
| 2409 | |
| 2410 | // Try to recover from the typo, by dropping the tag definition and parsing |
| 2411 | // the problematic tokens as a type. |
| 2412 | // |
| 2413 | // FIXME: Split the DeclSpec into pieces for the standalone |
| 2414 | // declaration and pieces for the following declaration, instead |
| 2415 | // of assuming that all the other pieces attach to new declaration, |
| 2416 | // and call ParsedFreeStandingDeclSpec as appropriate. |
| 2417 | DS.ClearTypeSpecType(); |
| 2418 | ParsedTemplateInfo NotATemplate; |
| 2419 | ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs); |
| 2420 | return false; |
| 2421 | } |
| 2422 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2423 | /// ParseDeclarationSpecifiers |
| 2424 | /// declaration-specifiers: [C99 6.7] |
| 2425 | /// storage-class-specifier declaration-specifiers[opt] |
| 2426 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2427 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2428 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2429 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2430 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2431 | /// |
| 2432 | /// storage-class-specifier: [C99 6.7.1] |
| 2433 | /// 'typedef' |
| 2434 | /// 'extern' |
| 2435 | /// 'static' |
| 2436 | /// 'auto' |
| 2437 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2438 | /// [C++] 'mutable' |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2439 | /// [C++11] 'thread_local' |
| 2440 | /// [C11] '_Thread_local' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2441 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2442 | /// function-specifier: [C99 6.7.4] |
| 2443 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2444 | /// [C++] 'virtual' |
| 2445 | /// [C++] 'explicit' |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2446 | /// [OpenCL] '__kernel' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2447 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2448 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2449 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2450 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 2451 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2452 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2453 | AccessSpecifier AS, |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2454 | DeclSpecContext DSContext, |
| 2455 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2456 | if (DS.getSourceRange().isInvalid()) { |
| 2457 | DS.SetRangeStart(Tok.getLocation()); |
| 2458 | DS.SetRangeEnd(Tok.getLocation()); |
| 2459 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2460 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2461 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2462 | bool AttrsLastTime = false; |
| 2463 | ParsedAttributesWithRange attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2464 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2465 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2466 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2467 | unsigned DiagID = 0; |
| 2468 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2469 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2470 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2471 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2472 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2473 | DoneWithDeclSpec: |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2474 | if (!AttrsLastTime) |
| 2475 | ProhibitAttributes(attrs); |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2476 | else { |
| 2477 | // Reject C++11 attributes that appertain to decl specifiers as |
| 2478 | // we don't support any C++11 attributes that appertain to decl |
| 2479 | // specifiers. This also conforms to what g++ 4.8 is doing. |
| 2480 | ProhibitCXX11Attributes(attrs); |
| 2481 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2482 | DS.takeAttributesFrom(attrs); |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2483 | } |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 2484 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2485 | // If this is not a declaration specifier token, we're done reading decl |
| 2486 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2487 | DS.Finish(Diags, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2488 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2489 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2490 | case tok::l_square: |
| 2491 | case tok::kw_alignas: |
Richard Smith | 672edb0 | 2013-02-22 09:15:49 +0000 | [diff] [blame] | 2492 | if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier()) |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2493 | goto DoneWithDeclSpec; |
| 2494 | |
| 2495 | ProhibitAttributes(attrs); |
| 2496 | // FIXME: It would be good to recover by accepting the attributes, |
| 2497 | // but attempting to do that now would cause serious |
| 2498 | // madness in terms of diagnostics. |
| 2499 | attrs.clear(); |
| 2500 | attrs.Range = SourceRange(); |
| 2501 | |
| 2502 | ParseCXX11Attributes(attrs); |
| 2503 | AttrsLastTime = true; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2504 | continue; |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2505 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2506 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2507 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2508 | if (DS.hasTypeSpecifier()) { |
| 2509 | bool AllowNonIdentifiers |
| 2510 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 2511 | Scope::BlockScope | |
| 2512 | Scope::TemplateParamScope | |
| 2513 | Scope::FunctionPrototypeScope | |
| 2514 | Scope::AtCatchScope)) == 0; |
| 2515 | bool AllowNestedNameSpecifiers |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2516 | = DSContext == DSC_top_level || |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2517 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 2518 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2519 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2520 | AllowNonIdentifiers, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2521 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2522 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2523 | } |
| 2524 | |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2525 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 2526 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 2527 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2528 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2529 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2530 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2531 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 2532 | else if (CurParsedObjCImpl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2533 | CCC = Sema::PCC_ObjCImplementation; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2534 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2535 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2536 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2539 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2540 | // C++ scope specifier. Annotate and loop, or bail out on error. |
Eli Friedman | 5a42820 | 2013-08-15 23:59:20 +0000 | [diff] [blame] | 2541 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2542 | if (!DS.hasTypeSpecifier()) |
| 2543 | DS.SetTypeSpecError(); |
| 2544 | goto DoneWithDeclSpec; |
| 2545 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 2546 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 2547 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2548 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2549 | |
| 2550 | case tok::annot_cxxscope: { |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2551 | if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector()) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2552 | goto DoneWithDeclSpec; |
| 2553 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2554 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 2555 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 2556 | Tok.getAnnotationRange(), |
| 2557 | SS); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2558 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2559 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2560 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2561 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2562 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2563 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2564 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2565 | |
| 2566 | // C++ [class.qual]p2: |
| 2567 | // In a lookup in which the constructor is an acceptable lookup |
| 2568 | // result and the nested-name-specifier nominates a class C: |
| 2569 | // |
| 2570 | // - if the name specified after the |
| 2571 | // nested-name-specifier, when looked up in C, is the |
| 2572 | // injected-class-name of C (Clause 9), or |
| 2573 | // |
| 2574 | // - if the name specified after the nested-name-specifier |
| 2575 | // is the same as the identifier or the |
| 2576 | // simple-template-id's template-name in the last |
| 2577 | // component of the nested-name-specifier, |
| 2578 | // |
| 2579 | // the name is instead considered to name the constructor of |
| 2580 | // class C. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2581 | // |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2582 | // Thus, if the template-name is actually the constructor |
| 2583 | // name, then the code is ill-formed; this interpretation is |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2584 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2585 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2586 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2587 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2588 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2589 | if (isConstructorDeclarator()) { |
| 2590 | // The user meant this to be an out-of-line constructor |
| 2591 | // definition, but template arguments are not allowed |
| 2592 | // there. Just allow this as a constructor; we'll |
| 2593 | // complain about it later. |
| 2594 | goto DoneWithDeclSpec; |
| 2595 | } |
| 2596 | |
| 2597 | // The user meant this to name a type, but it actually names |
| 2598 | // a constructor with some extraneous template |
| 2599 | // arguments. Complain, then parse it as a type as the user |
| 2600 | // intended. |
| 2601 | Diag(TemplateId->TemplateNameLoc, |
| 2602 | diag::err_out_of_line_template_id_names_constructor) |
| 2603 | << TemplateId->Name; |
| 2604 | } |
| 2605 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2606 | DS.getTypeSpecScope() = SS; |
| 2607 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2608 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2609 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2610 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2611 | continue; |
| 2612 | } |
| 2613 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2614 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2615 | DS.getTypeSpecScope() = SS; |
| 2616 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2617 | if (Tok.getAnnotationValue()) { |
| 2618 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2619 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2620 | Tok.getAnnotationEndLoc(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2621 | PrevSpec, DiagID, T); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2622 | if (isInvalid) |
| 2623 | break; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2624 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2625 | else |
| 2626 | DS.SetTypeSpecError(); |
| 2627 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2628 | ConsumeToken(); // The typename |
| 2629 | } |
| 2630 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2631 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2632 | goto DoneWithDeclSpec; |
| 2633 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2634 | // If we're in a context where the identifier could be a class name, |
| 2635 | // check whether this is a constructor declaration. |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2636 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2637 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2638 | &SS)) { |
| 2639 | if (isConstructorDeclarator()) |
| 2640 | goto DoneWithDeclSpec; |
| 2641 | |
| 2642 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 2643 | // of the class is qualified in a context where it could name |
| 2644 | // a constructor, its a constructor name. However, we've |
| 2645 | // looked at the declarator, and the user probably meant this |
| 2646 | // to be a type. Complain that it isn't supposed to be treated |
| 2647 | // as a type, then proceed to parse it as a type. |
| 2648 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 2649 | << Next.getIdentifierInfo(); |
| 2650 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2651 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2652 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 2653 | Next.getLocation(), |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2654 | getCurScope(), &SS, |
| 2655 | false, false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2656 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2657 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2658 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2659 | // If the referenced identifier is not a type, then this declspec is |
| 2660 | // erroneous: We already checked about that it has no type specifier, and |
| 2661 | // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2662 | // typename. |
David Blaikie | 7247c88 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 2663 | if (!TypeRep) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2664 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2665 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2666 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) { |
| 2667 | if (!Attrs.empty()) { |
| 2668 | AttrsLastTime = true; |
| 2669 | attrs.takeAllFrom(Attrs); |
| 2670 | } |
| 2671 | continue; |
| 2672 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2673 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2674 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2675 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2676 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2677 | ConsumeToken(); // The C++ scope. |
| 2678 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2679 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2680 | DiagID, TypeRep); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2681 | if (isInvalid) |
| 2682 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2683 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2684 | DS.SetRangeEnd(Tok.getLocation()); |
| 2685 | ConsumeToken(); // The typename. |
| 2686 | |
| 2687 | continue; |
| 2688 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2689 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2690 | case tok::annot_typename: { |
Bill Wendling | f0cc19f | 2013-11-19 22:56:43 +0000 | [diff] [blame] | 2691 | // If we've previously seen a tag definition, we were almost surely |
| 2692 | // missing a semicolon after it. |
| 2693 | if (DS.hasTypeSpecifier() && DS.hasTagDefinition()) |
| 2694 | goto DoneWithDeclSpec; |
| 2695 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2696 | if (Tok.getAnnotationValue()) { |
| 2697 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | c43271e | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 2698 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2699 | DiagID, T); |
| 2700 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2701 | DS.SetTypeSpecError(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2702 | |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 2703 | if (isInvalid) |
| 2704 | break; |
| 2705 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2706 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2707 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2708 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2709 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2710 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2711 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2712 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2713 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2714 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2715 | continue; |
| 2716 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2717 | |
Douglas Gregor | bfad915 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2718 | case tok::kw___is_signed: |
| 2719 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 2720 | // typically treats it as a trait. If we see __is_signed as it appears |
| 2721 | // in libstdc++, e.g., |
| 2722 | // |
| 2723 | // static const bool __is_signed; |
| 2724 | // |
| 2725 | // then treat __is_signed as an identifier rather than as a keyword. |
| 2726 | if (DS.getTypeSpecType() == TST_bool && |
| 2727 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
| 2728 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 2729 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
| 2730 | Tok.setKind(tok::identifier); |
| 2731 | } |
| 2732 | |
| 2733 | // We're done with the declaration-specifiers. |
| 2734 | goto DoneWithDeclSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2735 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2736 | // typedef-name |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2737 | case tok::kw_decltype: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2738 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2739 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 2740 | // so handle it as such. This is important for ctor parsing. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2741 | if (getLangOpts().CPlusPlus) { |
Eli Friedman | 5a42820 | 2013-08-15 23:59:20 +0000 | [diff] [blame] | 2742 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2743 | if (!DS.hasTypeSpecifier()) |
| 2744 | DS.SetTypeSpecError(); |
| 2745 | goto DoneWithDeclSpec; |
| 2746 | } |
| 2747 | if (!Tok.is(tok::identifier)) |
| 2748 | continue; |
| 2749 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2750 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2751 | // This identifier can only be a typedef name if we haven't already seen |
| 2752 | // a type-specifier. Without this check we misparse: |
| 2753 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 2754 | if (DS.hasTypeSpecifier()) |
| 2755 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2756 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2757 | // Check for need to substitute AltiVec keyword tokens. |
| 2758 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2759 | break; |
| 2760 | |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2761 | // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not |
| 2762 | // allow the use of a typedef name as a type specifier. |
| 2763 | if (DS.isTypeAltiVecVector()) |
| 2764 | goto DoneWithDeclSpec; |
| 2765 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2766 | ParsedType TypeRep = |
| 2767 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 2768 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2769 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2770 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 2771 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2772 | if (!TypeRep) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2773 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2774 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) { |
| 2775 | if (!Attrs.empty()) { |
| 2776 | AttrsLastTime = true; |
| 2777 | attrs.takeAllFrom(Attrs); |
| 2778 | } |
| 2779 | continue; |
| 2780 | } |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2781 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2782 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2783 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2784 | // If we're in a context where the identifier could be a class name, |
| 2785 | // check whether this is a constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2786 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2787 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2788 | isConstructorDeclarator()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2789 | goto DoneWithDeclSpec; |
| 2790 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2791 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2792 | DiagID, TypeRep); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2793 | if (isInvalid) |
| 2794 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2795 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2796 | DS.SetRangeEnd(Tok.getLocation()); |
| 2797 | ConsumeToken(); // The identifier |
| 2798 | |
| 2799 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2800 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2801 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2802 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2803 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2804 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2805 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2806 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2807 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2808 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2809 | |
| 2810 | // type-name |
| 2811 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2812 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2813 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2814 | // This template-id does not refer to a type name, so we're |
| 2815 | // done with the type-specifiers. |
| 2816 | goto DoneWithDeclSpec; |
| 2817 | } |
| 2818 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2819 | // If we're in a context where the template-id could be a |
| 2820 | // constructor name or specialization, check whether this is a |
| 2821 | // constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2822 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2823 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2824 | isConstructorDeclarator()) |
| 2825 | goto DoneWithDeclSpec; |
| 2826 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2827 | // Turn the template-id annotation token into a type annotation |
| 2828 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2829 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2830 | continue; |
| 2831 | } |
| 2832 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2833 | // GNU attributes support. |
| 2834 | case tok::kw___attribute: |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2835 | ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2836 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2837 | |
| 2838 | // Microsoft declspec support. |
| 2839 | case tok::kw___declspec: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2840 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2841 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2842 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2843 | // Microsoft single token adornments. |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2844 | case tok::kw___forceinline: { |
Serge Pavlov | d1fa81c | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2845 | isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID); |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2846 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2847 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 2848 | // FIXME: This does not work correctly if it is set to be a declspec |
| 2849 | // attribute, and a GNU attribute is simply incorrect. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2850 | DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 2851 | AttributeList::AS_GNU); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2852 | break; |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2853 | } |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2854 | |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 2855 | case tok::kw___sptr: |
| 2856 | case tok::kw___uptr: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2857 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2858 | case tok::kw___ptr32: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2859 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2860 | case tok::kw___cdecl: |
| 2861 | case tok::kw___stdcall: |
| 2862 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2863 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2864 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2865 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2866 | continue; |
| 2867 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2868 | // Borland single token adornments. |
| 2869 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2870 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2871 | continue; |
| 2872 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2873 | // OpenCL single token adornments. |
| 2874 | case tok::kw___kernel: |
| 2875 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2876 | continue; |
| 2877 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2878 | // storage-class-specifier |
| 2879 | case tok::kw_typedef: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2880 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
| 2881 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2882 | break; |
| 2883 | case tok::kw_extern: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2884 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2885 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2886 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
| 2887 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2888 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2889 | case tok::kw___private_extern__: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2890 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
| 2891 | Loc, PrevSpec, DiagID); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2892 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2893 | case tok::kw_static: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2894 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2895 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2896 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
| 2897 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2898 | break; |
| 2899 | case tok::kw_auto: |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2900 | if (getLangOpts().CPlusPlus11) { |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2901 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2902 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2903 | PrevSpec, DiagID); |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2904 | if (!isInvalid) |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2905 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2906 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2907 | } else |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2908 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 2909 | DiagID); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2910 | } else |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2911 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2912 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2913 | break; |
| 2914 | case tok::kw_register: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2915 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
| 2916 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2917 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2918 | case tok::kw_mutable: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2919 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
| 2920 | PrevSpec, DiagID); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2921 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2922 | case tok::kw___thread: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2923 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc, |
| 2924 | PrevSpec, DiagID); |
| 2925 | break; |
| 2926 | case tok::kw_thread_local: |
| 2927 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc, |
| 2928 | PrevSpec, DiagID); |
| 2929 | break; |
| 2930 | case tok::kw__Thread_local: |
| 2931 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local, |
| 2932 | Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2933 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2934 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2935 | // function-specifier |
| 2936 | case tok::kw_inline: |
Serge Pavlov | d1fa81c | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2937 | isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2938 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2939 | case tok::kw_virtual: |
Serge Pavlov | d1fa81c | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2940 | isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2941 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2942 | case tok::kw_explicit: |
Serge Pavlov | d1fa81c | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2943 | isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2944 | break; |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 2945 | case tok::kw__Noreturn: |
| 2946 | if (!getLangOpts().C11) |
| 2947 | Diag(Loc, diag::ext_c11_noreturn); |
Serge Pavlov | d1fa81c | 2013-11-13 06:57:53 +0000 | [diff] [blame] | 2948 | isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID); |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 2949 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2950 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2951 | // alignment-specifier |
| 2952 | case tok::kw__Alignas: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2953 | if (!getLangOpts().C11) |
Jordan Rose | f70a886 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2954 | Diag(Tok, diag::ext_c11_alignment) << Tok.getName(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2955 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2956 | continue; |
| 2957 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2958 | // friend |
| 2959 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2960 | if (DSContext == DSC_class) |
| 2961 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 2962 | else { |
| 2963 | PrevSpec = ""; // not actually used by the diagnostic |
| 2964 | DiagID = diag::err_friend_invalid_in_context; |
| 2965 | isInvalid = true; |
| 2966 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2967 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2968 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2969 | // Modules |
| 2970 | case tok::kw___module_private__: |
| 2971 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 2972 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2973 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2974 | // constexpr |
| 2975 | case tok::kw_constexpr: |
| 2976 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 2977 | break; |
| 2978 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2979 | // type-specifier |
| 2980 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2981 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 2982 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2983 | break; |
| 2984 | case tok::kw_long: |
| 2985 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2986 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2987 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2988 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2989 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2990 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2991 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2992 | case tok::kw___int64: |
| 2993 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2994 | DiagID); |
| 2995 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2996 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2997 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 2998 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2999 | break; |
| 3000 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3001 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 3002 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3003 | break; |
| 3004 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3005 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 3006 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3007 | break; |
| 3008 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3009 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 3010 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3011 | break; |
| 3012 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3013 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 3014 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3015 | break; |
| 3016 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3017 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 3018 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3019 | break; |
| 3020 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3021 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 3022 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3023 | break; |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3024 | case tok::kw___int128: |
| 3025 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
| 3026 | DiagID); |
| 3027 | break; |
| 3028 | case tok::kw_half: |
| 3029 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
| 3030 | DiagID); |
| 3031 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3032 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3033 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 3034 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3035 | break; |
| 3036 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3037 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 3038 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3039 | break; |
| 3040 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3041 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 3042 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3043 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3044 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3045 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 3046 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3047 | break; |
| 3048 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3049 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 3050 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3051 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3052 | case tok::kw_bool: |
| 3053 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3054 | if (Tok.is(tok::kw_bool) && |
| 3055 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 3056 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 3057 | PrevSpec = ""; // Not used by the diagnostic. |
| 3058 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3059 | // For better error recovery. |
| 3060 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 3061 | isInvalid = true; |
| 3062 | } else { |
| 3063 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 3064 | DiagID); |
| 3065 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3066 | break; |
| 3067 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3068 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 3069 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3070 | break; |
| 3071 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3072 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 3073 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3074 | break; |
| 3075 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3076 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 3077 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3078 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3079 | case tok::kw___vector: |
| 3080 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 3081 | break; |
| 3082 | case tok::kw___pixel: |
| 3083 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 3084 | break; |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3085 | case tok::kw_image1d_t: |
| 3086 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc, |
| 3087 | PrevSpec, DiagID); |
| 3088 | break; |
| 3089 | case tok::kw_image1d_array_t: |
| 3090 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc, |
| 3091 | PrevSpec, DiagID); |
| 3092 | break; |
| 3093 | case tok::kw_image1d_buffer_t: |
| 3094 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc, |
| 3095 | PrevSpec, DiagID); |
| 3096 | break; |
| 3097 | case tok::kw_image2d_t: |
| 3098 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc, |
| 3099 | PrevSpec, DiagID); |
| 3100 | break; |
| 3101 | case tok::kw_image2d_array_t: |
| 3102 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc, |
| 3103 | PrevSpec, DiagID); |
| 3104 | break; |
| 3105 | case tok::kw_image3d_t: |
| 3106 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc, |
| 3107 | PrevSpec, DiagID); |
| 3108 | break; |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3109 | case tok::kw_sampler_t: |
| 3110 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc, |
| 3111 | PrevSpec, DiagID); |
| 3112 | break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3113 | case tok::kw_event_t: |
| 3114 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc, |
| 3115 | PrevSpec, DiagID); |
| 3116 | break; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3117 | case tok::kw___unknown_anytype: |
| 3118 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 3119 | PrevSpec, DiagID); |
| 3120 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3121 | |
| 3122 | // class-specifier: |
| 3123 | case tok::kw_class: |
| 3124 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3125 | case tok::kw___interface: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3126 | case tok::kw_union: { |
| 3127 | tok::TokenKind Kind = Tok.getKind(); |
| 3128 | ConsumeToken(); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3129 | |
| 3130 | // These are attributes following class specifiers. |
| 3131 | // To produce better diagnostic, we parse them when |
| 3132 | // parsing class specifier. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3133 | ParsedAttributesWithRange Attributes(AttrFactory); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3134 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3135 | EnteringContext, DSContext, Attributes); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3136 | |
| 3137 | // If there are attributes following class specifier, |
| 3138 | // take them over and handle them here. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3139 | if (!Attributes.empty()) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3140 | AttrsLastTime = true; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3141 | attrs.takeAllFrom(Attributes); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3142 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3143 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3144 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3145 | |
| 3146 | // enum-specifier: |
| 3147 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3148 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3149 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3150 | continue; |
| 3151 | |
| 3152 | // cv-qualifier: |
| 3153 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3154 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3155 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3156 | break; |
| 3157 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3158 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3159 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3160 | break; |
| 3161 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3162 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3163 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3164 | break; |
| 3165 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3166 | // C++ typename-specifier: |
| 3167 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3168 | if (TryAnnotateTypeOrScopeToken()) { |
| 3169 | DS.SetTypeSpecError(); |
| 3170 | goto DoneWithDeclSpec; |
| 3171 | } |
| 3172 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3173 | continue; |
| 3174 | break; |
| 3175 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3176 | // GNU typeof support. |
| 3177 | case tok::kw_typeof: |
| 3178 | ParseTypeofSpecifier(DS); |
| 3179 | continue; |
| 3180 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3181 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 3182 | ParseDecltypeSpecifier(DS); |
| 3183 | continue; |
| 3184 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3185 | case tok::kw___underlying_type: |
| 3186 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3187 | continue; |
| 3188 | |
| 3189 | case tok::kw__Atomic: |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3190 | // C11 6.7.2.4/4: |
| 3191 | // If the _Atomic keyword is immediately followed by a left parenthesis, |
| 3192 | // it is interpreted as a type specifier (with a type name), not as a |
| 3193 | // type qualifier. |
| 3194 | if (NextToken().is(tok::l_paren)) { |
| 3195 | ParseAtomicSpecifier(DS); |
| 3196 | continue; |
| 3197 | } |
| 3198 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 3199 | getLangOpts()); |
| 3200 | break; |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3201 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3202 | // OpenCL qualifiers: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3203 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3204 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3205 | goto DoneWithDeclSpec; |
| 3206 | case tok::kw___private: |
| 3207 | case tok::kw___global: |
| 3208 | case tok::kw___local: |
| 3209 | case tok::kw___constant: |
| 3210 | case tok::kw___read_only: |
| 3211 | case tok::kw___write_only: |
| 3212 | case tok::kw___read_write: |
| 3213 | ParseOpenCLQualifiers(DS); |
| 3214 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3215 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 3216 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3217 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3218 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 3219 | // but we support it. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3220 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3221 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3222 | |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 3223 | if (!ParseObjCProtocolQualifiers(DS)) |
| 3224 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 3225 | << FixItHint::CreateInsertion(Loc, "id") |
| 3226 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3227 | |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 3228 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 3229 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 3230 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3231 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3232 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3233 | if (isInvalid) { |
| 3234 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3235 | assert(DiagID); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3236 | |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 3237 | if (DiagID == diag::ext_duplicate_declspec) |
| 3238 | Diag(Tok, DiagID) |
| 3239 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 3240 | else |
| 3241 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3242 | } |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3243 | |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 3244 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3245 | if (DiagID != diag::err_bool_redeclaration) |
| 3246 | ConsumeToken(); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3247 | |
| 3248 | AttrsLastTime = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3249 | } |
| 3250 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 3251 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3252 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 3253 | /// semicolon. |
| 3254 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3255 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3256 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3257 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3258 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3259 | /// struct-declarator-list: |
| 3260 | /// struct-declarator |
| 3261 | /// struct-declarator-list ',' struct-declarator |
| 3262 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 3263 | /// struct-declarator: |
| 3264 | /// declarator |
| 3265 | /// [GNU] declarator attributes[opt] |
| 3266 | /// declarator[opt] ':' constant-expression |
| 3267 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 3268 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3269 | void Parser:: |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3270 | ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3271 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3272 | if (Tok.is(tok::kw___extension__)) { |
| 3273 | // __extension__ silences extension warnings in the subexpression. |
| 3274 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3275 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3276 | return ParseStructDeclaration(DS, Fields); |
| 3277 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3278 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3279 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3280 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3281 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3282 | // If there are no declarators, this is a free-standing declaration |
| 3283 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3284 | if (Tok.is(tok::semi)) { |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3285 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
| 3286 | DS); |
| 3287 | DS.complete(TheDecl); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3288 | return; |
| 3289 | } |
| 3290 | |
| 3291 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3292 | bool FirstDeclarator = true; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3293 | SourceLocation CommaLoc; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3294 | while (1) { |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3295 | ParsingFieldDeclarator DeclaratorInfo(*this, DS); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3296 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3297 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3298 | // Attributes are only allowed here on successive declarators. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3299 | if (!FirstDeclarator) |
| 3300 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3301 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3302 | /// struct-declarator: declarator |
| 3303 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3304 | if (Tok.isNot(tok::colon)) { |
| 3305 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 3306 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3307 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3308 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3309 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3310 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3311 | ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3312 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3313 | if (Res.isInvalid()) |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3314 | SkipUntil(tok::semi, StopBeforeMatch); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 3315 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 3316 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3317 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3318 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3319 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3320 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3321 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3322 | // We're done with this declarator; invoke the callback. |
Eli Friedman | 817a886 | 2012-08-08 23:35:12 +0000 | [diff] [blame] | 3323 | Fields.invoke(DeclaratorInfo); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3324 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3325 | // If we don't have a comma, it is either the end of the list (a ';') |
| 3326 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3327 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3328 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3329 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3330 | // Consume the comma. |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3331 | CommaLoc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3332 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3333 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3334 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3335 | } |
| 3336 | |
| 3337 | /// ParseStructUnionBody |
| 3338 | /// struct-contents: |
| 3339 | /// struct-declaration-list |
| 3340 | /// [EXT] empty |
| 3341 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 3342 | /// struct-declaration-list: |
| 3343 | /// struct-declaration |
| 3344 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3345 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3346 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3347 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3348 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3349 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 3350 | "parsing struct/union body"); |
Andy Gibbs | f50f3f7 | 2013-04-03 09:31:19 +0000 | [diff] [blame] | 3351 | assert(!getLangOpts().CPlusPlus && "C++ declarations not supported"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3352 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3353 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3354 | if (T.consumeOpen()) |
| 3355 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3356 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3357 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3358 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3359 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3360 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3361 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3362 | // While we still have something to read, read the declarations in the struct. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3363 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3364 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3365 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3366 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3367 | if (Tok.is(tok::semi)) { |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3368 | ConsumeExtraSemi(InsideStruct, TagType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3369 | continue; |
| 3370 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3371 | |
Andy Gibbs | 74b9fa1 | 2013-04-03 09:46:04 +0000 | [diff] [blame] | 3372 | // Parse _Static_assert declaration. |
| 3373 | if (Tok.is(tok::kw__Static_assert)) { |
| 3374 | SourceLocation DeclEnd; |
| 3375 | ParseStaticAssertDeclaration(DeclEnd); |
| 3376 | continue; |
| 3377 | } |
| 3378 | |
Argyrios Kyrtzidis | bd95745 | 2013-04-18 01:42:35 +0000 | [diff] [blame] | 3379 | if (Tok.is(tok::annot_pragma_pack)) { |
| 3380 | HandlePragmaPack(); |
| 3381 | continue; |
| 3382 | } |
| 3383 | |
| 3384 | if (Tok.is(tok::annot_pragma_align)) { |
| 3385 | HandlePragmaAlign(); |
| 3386 | continue; |
| 3387 | } |
| 3388 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3389 | if (!Tok.is(tok::at)) { |
| 3390 | struct CFieldCallback : FieldCallback { |
| 3391 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3392 | Decl *TagDecl; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3393 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3394 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3395 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3396 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3397 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 3398 | |
Eli Friedman | dcdff46 | 2012-08-08 23:53:27 +0000 | [diff] [blame] | 3399 | void invoke(ParsingFieldDeclarator &FD) { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3400 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3401 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 3402 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 3403 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3404 | FieldDecls.push_back(Field); |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3405 | FD.complete(Field); |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3406 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3407 | } Callback(*this, TagDecl, FieldDecls); |
| 3408 | |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3409 | // Parse all the comma separated declarators. |
| 3410 | ParsingDeclSpec DS(*this); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3411 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3412 | } else { // Handle @defs |
| 3413 | ConsumeToken(); |
| 3414 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 3415 | Diag(Tok, diag::err_unexpected_at); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3416 | SkipUntil(tok::semi); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3417 | continue; |
| 3418 | } |
| 3419 | ConsumeToken(); |
| 3420 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 3421 | if (!Tok.is(tok::identifier)) { |
| 3422 | Diag(Tok, diag::err_expected_ident); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3423 | SkipUntil(tok::semi); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3424 | continue; |
| 3425 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3426 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3427 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3428 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3429 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 3430 | ConsumeToken(); |
| 3431 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3432 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3433 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3434 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3435 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3436 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3437 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3438 | break; |
| 3439 | } else { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3440 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 3441 | // Skip to end of block or statement to avoid ext-warning on extra ';'. |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3442 | SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3443 | // If we stopped at a ';', eat it. |
| 3444 | if (Tok.is(tok::semi)) ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3445 | } |
| 3446 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3447 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3448 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3449 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3450 | ParsedAttributes attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3451 | // If attributes exist after struct contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3452 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 3453 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3454 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 3455 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3456 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3457 | attrs.getList()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3458 | StructScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3459 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 3460 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3461 | } |
| 3462 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3463 | /// ParseEnumSpecifier |
| 3464 | /// enum-specifier: [C99 6.7.2.2] |
| 3465 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3466 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3467 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3468 | /// '}' attributes[opt] |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 3469 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3470 | /// '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3471 | /// 'enum' identifier |
| 3472 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3473 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3474 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 3475 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3476 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3477 | /// enum-head: [C++11] |
| 3478 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 3479 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 3480 | /// identifier enum-base[opt] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3481 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3482 | /// enum-key: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3483 | /// 'enum' |
| 3484 | /// 'enum' 'class' |
| 3485 | /// 'enum' 'struct' |
| 3486 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3487 | /// enum-base: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3488 | /// ':' type-specifier-seq |
| 3489 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3490 | /// [C++] elaborated-type-specifier: |
| 3491 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 3492 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3493 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 3494 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3495 | AccessSpecifier AS, DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3496 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3497 | if (Tok.is(tok::code_completion)) { |
| 3498 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3499 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3500 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3501 | } |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3502 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3503 | // If attributes exist after tag, parse them. |
| 3504 | ParsedAttributesWithRange attrs(AttrFactory); |
| 3505 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3506 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3507 | |
| 3508 | // If declspecs exist after tag, parse them. |
| 3509 | while (Tok.is(tok::kw___declspec)) |
| 3510 | ParseMicrosoftDeclSpec(attrs); |
| 3511 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3512 | SourceLocation ScopedEnumKWLoc; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3513 | bool IsScopedUsingClassTag = false; |
| 3514 | |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3515 | // In C++11, recognize 'enum class' and 'enum struct'. |
Richard Trieu | ed5a292 | 2013-04-23 02:47:36 +0000 | [diff] [blame] | 3516 | if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) { |
| 3517 | Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum |
| 3518 | : diag::ext_scoped_enum); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3519 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3520 | ScopedEnumKWLoc = ConsumeToken(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3521 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3522 | // Attributes are not allowed between these keywords. Diagnose, |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3523 | // but then just treat them like they appeared in the right place. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3524 | ProhibitAttributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3525 | |
| 3526 | // They are allowed afterwards, though. |
| 3527 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3528 | MaybeParseCXX11Attributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3529 | while (Tok.is(tok::kw___declspec)) |
| 3530 | ParseMicrosoftDeclSpec(attrs); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3531 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3532 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3533 | // C++11 [temp.explicit]p12: |
| 3534 | // The usual access controls do not apply to names used to specify |
| 3535 | // explicit instantiations. |
| 3536 | // We extend this to also cover explicit specializations. Note that |
| 3537 | // we don't suppress if this turns out to be an elaborated type |
| 3538 | // specifier. |
| 3539 | bool shouldDelayDiagsInTag = |
| 3540 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 3541 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 3542 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3543 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3544 | // Enum definitions should not be parsed in a trailing-return-type. |
| 3545 | bool AllowDeclaration = DSC != DSC_trailing; |
| 3546 | |
| 3547 | bool AllowFixedUnderlyingType = AllowDeclaration && |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3548 | (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt || |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3549 | getLangOpts().ObjC2); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3550 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3551 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3552 | if (getLangOpts().CPlusPlus) { |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3553 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 3554 | // if a fixed underlying type is allowed. |
| 3555 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3556 | |
| 3557 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Richard Smith | 725fe0e | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 3558 | /*EnteringContext=*/true)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3559 | return; |
| 3560 | |
| 3561 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3562 | Diag(Tok, diag::err_expected_ident); |
| 3563 | if (Tok.isNot(tok::l_brace)) { |
| 3564 | // Has no name and is not a definition. |
| 3565 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3566 | SkipUntil(tok::comma, StopAtSemi); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3567 | return; |
| 3568 | } |
| 3569 | } |
| 3570 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3571 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3572 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3573 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3574 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3575 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3576 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3577 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3578 | SkipUntil(tok::comma, StopAtSemi); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3579 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3580 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3581 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3582 | // If an identifier is present, consume and remember it. |
| 3583 | IdentifierInfo *Name = 0; |
| 3584 | SourceLocation NameLoc; |
| 3585 | if (Tok.is(tok::identifier)) { |
| 3586 | Name = Tok.getIdentifierInfo(); |
| 3587 | NameLoc = ConsumeToken(); |
| 3588 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3589 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3590 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3591 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 3592 | // declaration of a scoped enumeration. |
| 3593 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3594 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3595 | IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3596 | } |
| 3597 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3598 | // Okay, end the suppression area. We'll decide whether to emit the |
| 3599 | // diagnostics in a second. |
| 3600 | if (shouldDelayDiagsInTag) |
| 3601 | diagsFromTag.done(); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3602 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3603 | TypeResult BaseType; |
| 3604 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3605 | // Parse the fixed underlying type. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3606 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3607 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3608 | bool PossibleBitfield = false; |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3609 | if (CanBeBitfield) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3610 | // If we're in class scope, this can either be an enum declaration with |
| 3611 | // an underlying type, or a declaration of a bitfield member. We try to |
| 3612 | // use a simple disambiguation scheme first to catch the common cases |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3613 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 3614 | // anything that's a simple-type-specifier followed by '(' as an |
| 3615 | // expression. This suffices because function types are not valid |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3616 | // underlying types anyway. |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 3617 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 3618 | Sema::ConstantEvaluated); |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3619 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3620 | // If the next token starts an expression, we know we're parsing a |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3621 | // bit-field. This is the common case. |
| 3622 | if (TPR == TPResult::True()) |
| 3623 | PossibleBitfield = true; |
| 3624 | // If the next token starts a type-specifier-seq, it may be either a |
| 3625 | // a fixed underlying type or the start of a function-style cast in C++; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3626 | // lookahead one more token to see if it's obvious that we have a |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3627 | // fixed underlying type. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3628 | else if (TPR == TPResult::False() && |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3629 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 3630 | // Consume the ':'. |
| 3631 | ConsumeToken(); |
| 3632 | } else { |
| 3633 | // We have the start of a type-specifier-seq, so we have to perform |
| 3634 | // tentative parsing to determine whether we have an expression or a |
| 3635 | // type. |
| 3636 | TentativeParsingAction TPA(*this); |
| 3637 | |
| 3638 | // Consume the ':'. |
| 3639 | ConsumeToken(); |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3640 | |
| 3641 | // If we see a type specifier followed by an open-brace, we have an |
| 3642 | // ambiguity between an underlying type and a C++11 braced |
| 3643 | // function-style cast. Resolve this by always treating it as an |
| 3644 | // underlying type. |
| 3645 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 3646 | // this case. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3647 | if ((getLangOpts().CPlusPlus && |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3648 | isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3649 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3650 | // We'll parse this as a bitfield later. |
| 3651 | PossibleBitfield = true; |
| 3652 | TPA.Revert(); |
| 3653 | } else { |
| 3654 | // We have a type-specifier-seq. |
| 3655 | TPA.Commit(); |
| 3656 | } |
| 3657 | } |
| 3658 | } else { |
| 3659 | // Consume the ':'. |
| 3660 | ConsumeToken(); |
| 3661 | } |
| 3662 | |
| 3663 | if (!PossibleBitfield) { |
| 3664 | SourceRange Range; |
| 3665 | BaseType = ParseTypeName(&Range); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3666 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3667 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3668 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Eli Friedman | cef3a7b | 2012-11-02 01:34:28 +0000 | [diff] [blame] | 3669 | } else if (!getLangOpts().ObjC2) { |
| 3670 | if (getLangOpts().CPlusPlus) |
| 3671 | Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range; |
| 3672 | else |
| 3673 | Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range; |
| 3674 | } |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3675 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3678 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 3679 | // friend declaration, and cannot have an accompanying definition. If we have |
| 3680 | // 'enum foo;', then this is a forward declaration. If we have |
| 3681 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 3682 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3683 | // |
| 3684 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 3685 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 3686 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 3687 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3688 | Sema::TagUseKind TUK; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3689 | if (!AllowDeclaration) { |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3690 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3691 | } else if (Tok.is(tok::l_brace)) { |
| 3692 | if (DS.isFriendSpecified()) { |
| 3693 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
| 3694 | << SourceRange(DS.getFriendSpecLoc()); |
| 3695 | ConsumeBrace(); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3696 | SkipUntil(tok::r_brace, StopAtSemi); |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3697 | TUK = Sema::TUK_Friend; |
| 3698 | } else { |
| 3699 | TUK = Sema::TUK_Definition; |
| 3700 | } |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3701 | } else if (DSC != DSC_type_specifier && |
| 3702 | (Tok.is(tok::semi) || |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3703 | (Tok.isAtStartOfLine() && |
| 3704 | !isValidAfterTypeSpecifier(CanBeBitfield)))) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3705 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
| 3706 | if (Tok.isNot(tok::semi)) { |
| 3707 | // A semicolon was missing after this declaration. Diagnose and recover. |
| 3708 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, |
| 3709 | "enum"); |
| 3710 | PP.EnterToken(Tok); |
| 3711 | Tok.setKind(tok::semi); |
| 3712 | } |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3713 | } else { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3714 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3715 | } |
| 3716 | |
| 3717 | // If this is an elaborated type specifier, and we delayed |
| 3718 | // diagnostics before, just merge them into the current pool. |
| 3719 | if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) { |
| 3720 | diagsFromTag.redelay(); |
| 3721 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3722 | |
| 3723 | MultiTemplateParamsArg TParams; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3724 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3725 | TUK != Sema::TUK_Reference) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3726 | if (!getLangOpts().CPlusPlus11 || !SS.isSet()) { |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3727 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3728 | Diag(Tok, diag::err_enum_template); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3729 | SkipUntil(tok::comma, StopAtSemi); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3730 | return; |
| 3731 | } |
| 3732 | |
| 3733 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 3734 | // Enumerations can't be explicitly instantiated. |
| 3735 | DS.SetTypeSpecError(); |
| 3736 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 3737 | return; |
| 3738 | } |
| 3739 | |
| 3740 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 3741 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 3742 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3743 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3744 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3745 | if (TUK == Sema::TUK_Reference) |
| 3746 | ProhibitAttributes(attrs); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3747 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3748 | if (!Name && TUK != Sema::TUK_Definition) { |
| 3749 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3750 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3751 | // Skip the rest of this declarator, up until the comma or semicolon. |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3752 | SkipUntil(tok::comma, StopAtSemi); |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3753 | return; |
| 3754 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3755 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3756 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3757 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3758 | const char *PrevSpec = 0; |
| 3759 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3760 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3761 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3762 | AS, DS.getModulePrivateSpecLoc(), TParams, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3763 | Owned, IsDependent, ScopedEnumKWLoc, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3764 | IsScopedUsingClassTag, BaseType); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3765 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3766 | if (IsDependent) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3767 | // This enum has a dependent nested-name-specifier. Handle it as a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3768 | // dependent tag. |
| 3769 | if (!Name) { |
| 3770 | DS.SetTypeSpecError(); |
| 3771 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 3772 | return; |
| 3773 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3774 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3775 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3776 | TUK, SS, Name, StartLoc, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3777 | NameLoc); |
| 3778 | if (Type.isInvalid()) { |
| 3779 | DS.SetTypeSpecError(); |
| 3780 | return; |
| 3781 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3782 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3783 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 3784 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3785 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3786 | Diag(StartLoc, DiagID) << PrevSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3787 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3788 | return; |
| 3789 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3790 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3791 | if (!TagDecl) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3792 | // The action failed to produce an enumeration tag. If this is a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3793 | // definition, consume the entire definition. |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3794 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3795 | ConsumeBrace(); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3796 | SkipUntil(tok::r_brace, StopAtSemi); |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3797 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3798 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3799 | DS.SetTypeSpecError(); |
| 3800 | return; |
| 3801 | } |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3802 | |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3803 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3804 | ParseEnumBody(StartLoc, TagDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3805 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3806 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3807 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3808 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3809 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3810 | } |
| 3811 | |
| 3812 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3813 | /// enumerator-list: |
| 3814 | /// enumerator |
| 3815 | /// enumerator-list ',' enumerator |
| 3816 | /// enumerator: |
| 3817 | /// enumeration-constant |
| 3818 | /// enumeration-constant '=' constant-expression |
| 3819 | /// enumeration-constant: |
| 3820 | /// identifier |
| 3821 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3822 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3823 | // Enter the scope of the enum body and start the definition. |
| 3824 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3825 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3826 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3827 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3828 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3829 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3830 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3831 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3832 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3833 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3834 | SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3835 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3836 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3837 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3838 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3839 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3840 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3841 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3842 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3843 | // If attributes exist after the enumerator, parse them. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3844 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3845 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3846 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3847 | ProhibitAttributes(attrs); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3848 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3849 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3850 | ExprResult AssignedVal; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 3851 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3852 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3853 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3854 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3855 | AssignedVal = ParseConstantExpression(); |
| 3856 | if (AssignedVal.isInvalid()) |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 3857 | SkipUntil(tok::comma, tok::r_brace, StopAtSemi | StopBeforeMatch); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3858 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3859 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3860 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3861 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3862 | LastEnumConstDecl, |
| 3863 | IdentLoc, Ident, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3864 | attrs.getList(), EqualLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3865 | AssignedVal.release()); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3866 | PD.complete(EnumConstDecl); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3867 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3868 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3869 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3870 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3871 | if (Tok.is(tok::identifier)) { |
| 3872 | // We're missing a comma between enumerators. |
| 3873 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3874 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3875 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3876 | continue; |
| 3877 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3878 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3879 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3880 | break; |
| 3881 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3882 | |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3883 | if (Tok.isNot(tok::identifier)) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3884 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11) |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3885 | Diag(CommaLoc, getLangOpts().CPlusPlus ? |
| 3886 | diag::ext_enumerator_list_comma_cxx : |
| 3887 | diag::ext_enumerator_list_comma_c) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3888 | << FixItHint::CreateRemoval(CommaLoc); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3889 | else if (getLangOpts().CPlusPlus11) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3890 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3891 | << FixItHint::CreateRemoval(CommaLoc); |
| 3892 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3893 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3894 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3895 | // Eat the }. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3896 | T.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3897 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3898 | // If attributes exist after the identifier list, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3899 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3900 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3901 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3902 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 3903 | EnumDecl, EnumConstantDecls, |
| 3904 | getCurScope(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3905 | attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3906 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3907 | EnumScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3908 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3909 | T.getCloseLocation()); |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3910 | |
| 3911 | // The next token must be valid after an enum definition. If not, a ';' |
| 3912 | // was probably forgotten. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3913 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
| 3914 | if (!isValidAfterTypeSpecifier(CanBeBitfield)) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3915 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum"); |
| 3916 | // Push this token back into the preprocessor and change our current token |
| 3917 | // to ';' so that the rest of the code recovers as though there were an |
| 3918 | // ';' after the definition. |
| 3919 | PP.EnterToken(Tok); |
| 3920 | Tok.setKind(tok::semi); |
| 3921 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3922 | } |
| 3923 | |
| 3924 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3925 | /// start of a type-qualifier-list. |
| 3926 | bool Parser::isTypeQualifier() const { |
| 3927 | switch (Tok.getKind()) { |
| 3928 | default: return false; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3929 | |
| 3930 | // type-qualifier only in OpenCL |
| 3931 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3932 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3933 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3934 | // type-qualifier |
| 3935 | case tok::kw_const: |
| 3936 | case tok::kw_volatile: |
| 3937 | case tok::kw_restrict: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3938 | case tok::kw___private: |
| 3939 | case tok::kw___local: |
| 3940 | case tok::kw___global: |
| 3941 | case tok::kw___constant: |
| 3942 | case tok::kw___read_only: |
| 3943 | case tok::kw___read_write: |
| 3944 | case tok::kw___write_only: |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3945 | return true; |
| 3946 | } |
| 3947 | } |
| 3948 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3949 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3950 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3951 | /// specifier or if we're not sure. |
| 3952 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3953 | switch (Tok.getKind()) { |
| 3954 | default: return false; |
| 3955 | // type-specifiers |
| 3956 | case tok::kw_short: |
| 3957 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3958 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3959 | case tok::kw___int128: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3960 | case tok::kw_signed: |
| 3961 | case tok::kw_unsigned: |
| 3962 | case tok::kw__Complex: |
| 3963 | case tok::kw__Imaginary: |
| 3964 | case tok::kw_void: |
| 3965 | case tok::kw_char: |
| 3966 | case tok::kw_wchar_t: |
| 3967 | case tok::kw_char16_t: |
| 3968 | case tok::kw_char32_t: |
| 3969 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3970 | case tok::kw_half: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3971 | case tok::kw_float: |
| 3972 | case tok::kw_double: |
| 3973 | case tok::kw_bool: |
| 3974 | case tok::kw__Bool: |
| 3975 | case tok::kw__Decimal32: |
| 3976 | case tok::kw__Decimal64: |
| 3977 | case tok::kw__Decimal128: |
| 3978 | case tok::kw___vector: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3979 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3980 | // OpenCL specific types: |
| 3981 | case tok::kw_image1d_t: |
| 3982 | case tok::kw_image1d_array_t: |
| 3983 | case tok::kw_image1d_buffer_t: |
| 3984 | case tok::kw_image2d_t: |
| 3985 | case tok::kw_image2d_array_t: |
| 3986 | case tok::kw_image3d_t: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3987 | case tok::kw_sampler_t: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3988 | case tok::kw_event_t: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3989 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3990 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3991 | case tok::kw_class: |
| 3992 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3993 | case tok::kw___interface: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3994 | case tok::kw_union: |
| 3995 | // enum-specifier |
| 3996 | case tok::kw_enum: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3997 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3998 | // typedef-name |
| 3999 | case tok::annot_typename: |
| 4000 | return true; |
| 4001 | } |
| 4002 | } |
| 4003 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 4004 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4005 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4006 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4007 | switch (Tok.getKind()) { |
| 4008 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4009 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4010 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4011 | if (TryAltiVecVectorToken()) |
| 4012 | return true; |
| 4013 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4014 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4015 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4016 | // recurse to handle whatever we get. |
| 4017 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4018 | return true; |
| 4019 | if (Tok.is(tok::identifier)) |
| 4020 | return false; |
| 4021 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4022 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4023 | case tok::coloncolon: // ::foo::bar |
| 4024 | if (NextToken().is(tok::kw_new) || // ::new |
| 4025 | NextToken().is(tok::kw_delete)) // ::delete |
| 4026 | return false; |
| 4027 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4028 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4029 | return true; |
| 4030 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4031 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4032 | // GNU attributes support. |
| 4033 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4034 | // GNU typeof support. |
| 4035 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4036 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4037 | // type-specifiers |
| 4038 | case tok::kw_short: |
| 4039 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4040 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4041 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4042 | case tok::kw_signed: |
| 4043 | case tok::kw_unsigned: |
| 4044 | case tok::kw__Complex: |
| 4045 | case tok::kw__Imaginary: |
| 4046 | case tok::kw_void: |
| 4047 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4048 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4049 | case tok::kw_char16_t: |
| 4050 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4051 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4052 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4053 | case tok::kw_float: |
| 4054 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4055 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4056 | case tok::kw__Bool: |
| 4057 | case tok::kw__Decimal32: |
| 4058 | case tok::kw__Decimal64: |
| 4059 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4060 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4061 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 4062 | // OpenCL specific types: |
| 4063 | case tok::kw_image1d_t: |
| 4064 | case tok::kw_image1d_array_t: |
| 4065 | case tok::kw_image1d_buffer_t: |
| 4066 | case tok::kw_image2d_t: |
| 4067 | case tok::kw_image2d_array_t: |
| 4068 | case tok::kw_image3d_t: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4069 | case tok::kw_sampler_t: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4070 | case tok::kw_event_t: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 4071 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4072 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4073 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4074 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4075 | case tok::kw___interface: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4076 | case tok::kw_union: |
| 4077 | // enum-specifier |
| 4078 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4079 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4080 | // type-qualifier |
| 4081 | case tok::kw_const: |
| 4082 | case tok::kw_volatile: |
| 4083 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4084 | |
John McCall | b8a8de3 | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4085 | // Debugger support. |
| 4086 | case tok::kw___unknown_anytype: |
| 4087 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4088 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 4089 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4090 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4091 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 4092 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4093 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4094 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4095 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4096 | case tok::kw___cdecl: |
| 4097 | case tok::kw___stdcall: |
| 4098 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4099 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4100 | case tok::kw___w64: |
| 4101 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4102 | case tok::kw___ptr32: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4103 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4104 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4105 | |
| 4106 | case tok::kw___private: |
| 4107 | case tok::kw___local: |
| 4108 | case tok::kw___global: |
| 4109 | case tok::kw___constant: |
| 4110 | case tok::kw___read_only: |
| 4111 | case tok::kw___read_write: |
| 4112 | case tok::kw___write_only: |
| 4113 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4114 | return true; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4115 | |
| 4116 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4117 | return getLangOpts().OpenCL; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4118 | |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4119 | // C11 _Atomic |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4120 | case tok::kw__Atomic: |
| 4121 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4122 | } |
| 4123 | } |
| 4124 | |
| 4125 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 4126 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4127 | /// |
| 4128 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 4129 | /// this check is to disambiguate between an expression and a declaration. |
| 4130 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4131 | switch (Tok.getKind()) { |
| 4132 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4133 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4134 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4135 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4136 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4137 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4138 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4139 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4140 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4141 | if (TryAltiVecVectorToken()) |
| 4142 | return true; |
| 4143 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4144 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4145 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4146 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4147 | // recurse to handle whatever we get. |
| 4148 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4149 | return true; |
| 4150 | if (Tok.is(tok::identifier)) |
| 4151 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4152 | |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4153 | // If we're in Objective-C and we have an Objective-C class type followed |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4154 | // by an identifier and then either ':' or ']', in a place where an |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4155 | // expression is permitted, then this is probably a class message send |
| 4156 | // missing the initial '['. In this case, we won't consider this to be |
| 4157 | // the start of a declaration. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4158 | if (DisambiguatingWithExpression && |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4159 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 4160 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4161 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4162 | return isDeclarationSpecifier(); |
| 4163 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4164 | case tok::coloncolon: // ::foo::bar |
| 4165 | if (NextToken().is(tok::kw_new) || // ::new |
| 4166 | NextToken().is(tok::kw_delete)) // ::delete |
| 4167 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4168 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4169 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4170 | // recurse to handle whatever we get. |
| 4171 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4172 | return true; |
| 4173 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4174 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4175 | // storage-class-specifier |
| 4176 | case tok::kw_typedef: |
| 4177 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 4178 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4179 | case tok::kw_static: |
| 4180 | case tok::kw_auto: |
| 4181 | case tok::kw_register: |
| 4182 | case tok::kw___thread: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4183 | case tok::kw_thread_local: |
| 4184 | case tok::kw__Thread_local: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4185 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 4186 | // Modules |
| 4187 | case tok::kw___module_private__: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4188 | |
John McCall | b8a8de3 | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4189 | // Debugger support |
| 4190 | case tok::kw___unknown_anytype: |
| 4191 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4192 | // type-specifiers |
| 4193 | case tok::kw_short: |
| 4194 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4195 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4196 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4197 | case tok::kw_signed: |
| 4198 | case tok::kw_unsigned: |
| 4199 | case tok::kw__Complex: |
| 4200 | case tok::kw__Imaginary: |
| 4201 | case tok::kw_void: |
| 4202 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4203 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4204 | case tok::kw_char16_t: |
| 4205 | case tok::kw_char32_t: |
| 4206 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4207 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4208 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4209 | case tok::kw_float: |
| 4210 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4211 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4212 | case tok::kw__Bool: |
| 4213 | case tok::kw__Decimal32: |
| 4214 | case tok::kw__Decimal64: |
| 4215 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4216 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4217 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 4218 | // OpenCL specific types: |
| 4219 | case tok::kw_image1d_t: |
| 4220 | case tok::kw_image1d_array_t: |
| 4221 | case tok::kw_image1d_buffer_t: |
| 4222 | case tok::kw_image2d_t: |
| 4223 | case tok::kw_image2d_array_t: |
| 4224 | case tok::kw_image3d_t: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4225 | case tok::kw_sampler_t: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4226 | case tok::kw_event_t: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 4227 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4228 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4229 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4230 | case tok::kw_struct: |
| 4231 | case tok::kw_union: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4232 | case tok::kw___interface: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4233 | // enum-specifier |
| 4234 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4235 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4236 | // type-qualifier |
| 4237 | case tok::kw_const: |
| 4238 | case tok::kw_volatile: |
| 4239 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4240 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4241 | // function-specifier |
| 4242 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4243 | case tok::kw_virtual: |
| 4244 | case tok::kw_explicit: |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4245 | case tok::kw__Noreturn: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4246 | |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 4247 | // alignment-specifier |
| 4248 | case tok::kw__Alignas: |
| 4249 | |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4250 | // friend keyword. |
| 4251 | case tok::kw_friend: |
| 4252 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 4253 | // static_assert-declaration |
| 4254 | case tok::kw__Static_assert: |
| 4255 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4256 | // GNU typeof support. |
| 4257 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4258 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4259 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4260 | case tok::kw___attribute: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4261 | |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4262 | // C++11 decltype and constexpr. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4263 | case tok::annot_decltype: |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4264 | case tok::kw_constexpr: |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 4265 | |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4266 | // C11 _Atomic |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4267 | case tok::kw__Atomic: |
| 4268 | return true; |
| 4269 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 4270 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4271 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4272 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4273 | |
Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 4274 | // typedef-name |
| 4275 | case tok::annot_typename: |
| 4276 | return !DisambiguatingWithExpression || |
| 4277 | !isStartOfObjCClassMessageMissingOpenBracket(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4278 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 4279 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4280 | case tok::kw___cdecl: |
| 4281 | case tok::kw___stdcall: |
| 4282 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4283 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4284 | case tok::kw___w64: |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4285 | case tok::kw___sptr: |
| 4286 | case tok::kw___uptr: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4287 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4288 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4289 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4290 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4291 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4292 | |
| 4293 | case tok::kw___private: |
| 4294 | case tok::kw___local: |
| 4295 | case tok::kw___global: |
| 4296 | case tok::kw___constant: |
| 4297 | case tok::kw___read_only: |
| 4298 | case tok::kw___read_write: |
| 4299 | case tok::kw___write_only: |
| 4300 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4301 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4302 | } |
| 4303 | } |
| 4304 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4305 | bool Parser::isConstructorDeclarator() { |
| 4306 | TentativeParsingAction TPA(*this); |
| 4307 | |
| 4308 | // Parse the C++ scope specifier. |
| 4309 | CXXScopeSpec SS; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4310 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4311 | /*EnteringContext=*/true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4312 | TPA.Revert(); |
| 4313 | return false; |
| 4314 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4315 | |
| 4316 | // Parse the constructor name. |
| 4317 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 4318 | // We already know that we have a constructor name; just consume |
| 4319 | // the token. |
| 4320 | ConsumeToken(); |
| 4321 | } else { |
| 4322 | TPA.Revert(); |
| 4323 | return false; |
| 4324 | } |
| 4325 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4326 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4327 | if (Tok.isNot(tok::l_paren)) { |
| 4328 | TPA.Revert(); |
| 4329 | return false; |
| 4330 | } |
| 4331 | ConsumeParen(); |
| 4332 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4333 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 4334 | // that we have a constructor. |
| 4335 | if (Tok.is(tok::r_paren) || |
| 4336 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4337 | TPA.Revert(); |
| 4338 | return true; |
| 4339 | } |
| 4340 | |
Richard Smith | 9ec2891 | 2013-09-06 00:12:20 +0000 | [diff] [blame] | 4341 | // A C++11 attribute here signals that we have a constructor, and is an |
| 4342 | // attribute on the first constructor parameter. |
| 4343 | if (getLangOpts().CPlusPlus11 && |
| 4344 | isCXX11AttributeSpecifier(/*Disambiguate*/ false, |
| 4345 | /*OuterMightBeMessageSend*/ true)) { |
| 4346 | TPA.Revert(); |
| 4347 | return true; |
| 4348 | } |
| 4349 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4350 | // If we need to, enter the specified scope. |
| 4351 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4352 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4353 | DeclScopeObj.EnterDeclaratorScope(); |
| 4354 | |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4355 | // Optionally skip Microsoft attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4356 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4357 | MaybeParseMicrosoftAttributes(Attrs); |
| 4358 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4359 | // Check whether the next token(s) are part of a declaration |
| 4360 | // specifier, in which case we have the start of a parameter and, |
| 4361 | // therefore, we know that this is a constructor. |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4362 | bool IsConstructor = false; |
| 4363 | if (isDeclarationSpecifier()) |
| 4364 | IsConstructor = true; |
| 4365 | else if (Tok.is(tok::identifier) || |
| 4366 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 4367 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 4368 | // This might be a parenthesized member name, but is more likely to |
| 4369 | // be a constructor declaration with an invalid argument type. Keep |
| 4370 | // looking. |
| 4371 | if (Tok.is(tok::annot_cxxscope)) |
| 4372 | ConsumeToken(); |
| 4373 | ConsumeToken(); |
| 4374 | |
| 4375 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4376 | // which must have one of the following syntactic forms (see the |
| 4377 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4378 | switch (Tok.getKind()) { |
| 4379 | case tok::l_paren: |
| 4380 | // C(X ( int)); |
| 4381 | case tok::l_square: |
| 4382 | // C(X [ 5]); |
| 4383 | // C(X [ [attribute]]); |
| 4384 | case tok::coloncolon: |
| 4385 | // C(X :: Y); |
| 4386 | // C(X :: *p); |
| 4387 | case tok::r_paren: |
| 4388 | // C(X ) |
| 4389 | // Assume this isn't a constructor, rather than assuming it's a |
| 4390 | // constructor with an unnamed parameter of an ill-formed type. |
| 4391 | break; |
| 4392 | |
| 4393 | default: |
| 4394 | IsConstructor = true; |
| 4395 | break; |
| 4396 | } |
| 4397 | } |
| 4398 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4399 | TPA.Revert(); |
| 4400 | return IsConstructor; |
| 4401 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4402 | |
| 4403 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4404 | /// type-qualifier-list: [C99 6.7.5] |
| 4405 | /// type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4406 | /// [vendor] attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4407 | /// [ only if VendorAttributesAllowed=true ] |
| 4408 | /// type-qualifier-list type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4409 | /// [vendor] type-qualifier-list attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4410 | /// [ only if VendorAttributesAllowed=true ] |
| 4411 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4412 | /// [ only if CXX11AttributesAllowed=true ] |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4413 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4414 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4415 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 4416 | bool VendorAttributesAllowed, |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4417 | bool CXX11AttributesAllowed, |
Bill Wendling | 7f3ec66 | 2013-11-26 04:10:07 +0000 | [diff] [blame] | 4418 | bool AtomicAllowed, |
| 4419 | bool IdentifierRequired) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4420 | if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed && |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4421 | isCXX11AttributeSpecifier()) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4422 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4423 | ParseCXX11Attributes(attrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4424 | DS.takeAttributesFrom(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4425 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4426 | |
| 4427 | SourceLocation EndLoc; |
| 4428 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4429 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4430 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4431 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4432 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4433 | SourceLocation Loc = Tok.getLocation(); |
| 4434 | |
| 4435 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4436 | case tok::code_completion: |
| 4437 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 4438 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4439 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4440 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4441 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4442 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4443 | break; |
| 4444 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4445 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4446 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4447 | break; |
| 4448 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4449 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4450 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4451 | break; |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4452 | case tok::kw__Atomic: |
| 4453 | if (!AtomicAllowed) |
| 4454 | goto DoneWithTypeQuals; |
| 4455 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 4456 | getLangOpts()); |
| 4457 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4458 | |
| 4459 | // OpenCL qualifiers: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4460 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4461 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4462 | goto DoneWithTypeQuals; |
| 4463 | case tok::kw___private: |
| 4464 | case tok::kw___global: |
| 4465 | case tok::kw___local: |
| 4466 | case tok::kw___constant: |
| 4467 | case tok::kw___read_only: |
| 4468 | case tok::kw___write_only: |
| 4469 | case tok::kw___read_write: |
| 4470 | ParseOpenCLQualifiers(DS); |
| 4471 | break; |
| 4472 | |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4473 | case tok::kw___uptr: |
Bill Wendling | 7f3ec66 | 2013-11-26 04:10:07 +0000 | [diff] [blame] | 4474 | // GNU libc headers in C mode use '__uptr' as an identifer which conflicts |
| 4475 | // with the MS modifier keyword. |
| 4476 | if (VendorAttributesAllowed && !getLangOpts().CPlusPlus && |
| 4477 | IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi) && |
| 4478 | PP.getSourceManager().isInSystemHeader(Loc)) { |
| 4479 | Tok.setKind(tok::identifier); |
| 4480 | continue; |
| 4481 | } |
| 4482 | case tok::kw___sptr: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4483 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 4484 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4485 | case tok::kw___ptr32: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4486 | case tok::kw___cdecl: |
| 4487 | case tok::kw___stdcall: |
| 4488 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4489 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4490 | case tok::kw___unaligned: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4491 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4492 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4493 | continue; |
| 4494 | } |
| 4495 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4496 | case tok::kw___pascal: |
| 4497 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4498 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4499 | continue; |
| 4500 | } |
| 4501 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4502 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4503 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4504 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4505 | continue; // do *not* consume the next token! |
| 4506 | } |
| 4507 | // otherwise, FALL THROUGH! |
| 4508 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4509 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4510 | // If this is not a type-qualifier token, we're done reading type |
| 4511 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 4512 | DS.Finish(Diags, PP); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4513 | if (EndLoc.isValid()) |
| 4514 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4515 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4516 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4517 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4518 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 4519 | if (isInvalid) { |
| 4520 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4521 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4522 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4523 | EndLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4524 | } |
| 4525 | } |
| 4526 | |
| 4527 | |
| 4528 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 4529 | /// |
| 4530 | void Parser::ParseDeclarator(Declarator &D) { |
| 4531 | /// This implements the 'declarator' production in the C grammar, then checks |
| 4532 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4533 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4534 | } |
| 4535 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4536 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) { |
| 4537 | if (Kind == tok::star || Kind == tok::caret) |
| 4538 | return true; |
| 4539 | |
| 4540 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 4541 | if (!Lang.CPlusPlus) |
| 4542 | return false; |
| 4543 | |
| 4544 | return Kind == tok::amp || Kind == tok::ampamp; |
| 4545 | } |
| 4546 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4547 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 4548 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 4549 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4550 | /// ptr-operator production. |
| 4551 | /// |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4552 | /// If the grammar of this construct is extended, matching changes must also be |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4553 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 4554 | /// isConstructorDeclarator. |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4555 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4556 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 4557 | /// [C] pointer[opt] direct-declarator |
| 4558 | /// [C++] direct-declarator |
| 4559 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4560 | /// |
| 4561 | /// pointer: [C99 6.7.5] |
| 4562 | /// '*' type-qualifier-list[opt] |
| 4563 | /// '*' type-qualifier-list[opt] pointer |
| 4564 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4565 | /// ptr-operator: |
| 4566 | /// '*' cv-qualifier-seq[opt] |
| 4567 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4568 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4569 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4570 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4571 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4572 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 4573 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 4574 | if (Diags.hasAllExtensionsSilenced()) |
| 4575 | D.setExtension(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4576 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4577 | // C++ member pointers start with a '::' or a nested-name. |
| 4578 | // Member pointers get special handling, since there's no place for the |
| 4579 | // scope spec in the generic path below. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4580 | if (getLangOpts().CPlusPlus && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 4581 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 4582 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4583 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4584 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4585 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4586 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4587 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 4588 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4589 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4590 | // The scope spec really belongs to the direct-declarator. |
Richard Smith | 6a502c4 | 2013-01-08 22:43:49 +0000 | [diff] [blame] | 4591 | if (D.mayHaveIdentifier()) |
| 4592 | D.getCXXScopeSpec() = SS; |
| 4593 | else |
| 4594 | AnnotateScopeToken(SS, true); |
| 4595 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4596 | if (DirectDeclParser) |
| 4597 | (this->*DirectDeclParser)(D); |
| 4598 | return; |
| 4599 | } |
| 4600 | |
| 4601 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4602 | D.SetRangeEnd(Loc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4603 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4604 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4605 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4606 | |
| 4607 | // Recurse to parse whatever is left. |
| 4608 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 4609 | |
| 4610 | // Sema will have to catch (syntactically invalid) pointers into global |
| 4611 | // scope. It has to catch pointers into namespace scope anyway. |
| 4612 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4613 | Loc), |
| 4614 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4615 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4616 | return; |
| 4617 | } |
| 4618 | } |
| 4619 | |
| 4620 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4621 | // Not a pointer, C++ reference, or block. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4622 | if (!isPtrOperatorToken(Kind, getLangOpts())) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4623 | if (DirectDeclParser) |
| 4624 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4625 | return; |
| 4626 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4627 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4628 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 4629 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4630 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4631 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4632 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 4633 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4634 | // Is a pointer. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4635 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4636 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4637 | // FIXME: GNU attributes are not allowed here in a new-type-id. |
Bill Wendling | 7f3ec66 | 2013-11-26 04:10:07 +0000 | [diff] [blame] | 4638 | ParseTypeQualifierListOpt(DS, true, true, true, !D.mayOmitIdentifier()); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4639 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4640 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4641 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4642 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4643 | if (Kind == tok::star) |
| 4644 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 4645 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 4646 | DS.getConstSpecLoc(), |
| 4647 | DS.getVolatileSpecLoc(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4648 | DS.getRestrictSpecLoc()), |
| 4649 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4650 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4651 | else |
| 4652 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4653 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4654 | Loc), |
| 4655 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4656 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4657 | } else { |
| 4658 | // Is a reference |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4659 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4660 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4661 | // Complain about rvalue references in C++03, but then go on and build |
| 4662 | // the declarator. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4663 | if (Kind == tok::ampamp) |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4664 | Diag(Loc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4665 | diag::warn_cxx98_compat_rvalue_reference : |
| 4666 | diag::ext_rvalue_reference); |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4667 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4668 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 4669 | ParseTypeQualifierListOpt(DS); |
| 4670 | D.ExtendWithDeclSpec(DS); |
| 4671 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4672 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 4673 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 4674 | // template type argument, in which case the cv-qualifiers are ignored. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4675 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 4676 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 4677 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4678 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4679 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 4680 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4681 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4682 | // 'restrict' is permitted as an extension. |
| 4683 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 4684 | Diag(DS.getAtomicSpecLoc(), |
| 4685 | diag::err_invalid_reference_qualifier_application) << "_Atomic"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4686 | } |
| 4687 | |
| 4688 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4689 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4690 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4691 | if (D.getNumTypeObjects() > 0) { |
| 4692 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 4693 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 4694 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 4695 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 4696 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4697 | << II; |
| 4698 | else |
| 4699 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4700 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4701 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4702 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4703 | // can go ahead and build the (technically ill-formed) |
| 4704 | // declarator: reference collapsing will take care of it. |
| 4705 | } |
| 4706 | } |
| 4707 | |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4708 | // Remember that we parsed a reference type. |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4709 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4710 | Kind == tok::amp), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4711 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4712 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4713 | } |
| 4714 | } |
| 4715 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4716 | static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D, |
| 4717 | SourceLocation EllipsisLoc) { |
| 4718 | if (EllipsisLoc.isValid()) { |
| 4719 | FixItHint Insertion; |
| 4720 | if (!D.getEllipsisLoc().isValid()) { |
| 4721 | Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "..."); |
| 4722 | D.setEllipsisLoc(EllipsisLoc); |
| 4723 | } |
| 4724 | P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) |
| 4725 | << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName(); |
| 4726 | } |
| 4727 | } |
| 4728 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4729 | /// ParseDirectDeclarator |
| 4730 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4731 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4732 | /// '(' declarator ')' |
| 4733 | /// [GNU] '(' attributes declarator ')' |
| 4734 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4735 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4736 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4737 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4738 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4739 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 4740 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4741 | /// direct-declarator '(' parameter-type-list ')' |
| 4742 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4743 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4744 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 4745 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 4746 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4747 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 4748 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 4749 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4750 | /// [C++] declarator-id |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4751 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4752 | /// |
| 4753 | /// declarator-id: [C++ 8] |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4754 | /// '...'[opt] id-expression |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4755 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 4756 | /// |
| 4757 | /// id-expression: [C++ 5.1] |
| 4758 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4759 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4760 | /// |
| 4761 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4762 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4763 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4764 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4765 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 4766 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 4767 | /// |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4768 | /// Note, any additional constructs added here may need corresponding changes |
| 4769 | /// in isConstructorDeclarator. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4770 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4771 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4772 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4773 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4774 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4775 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4776 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4777 | D.getContext() == Declarator::MemberContext; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4778 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4779 | EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4780 | } |
| 4781 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4782 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4783 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 4784 | // Change the declaration context for name lookup, until this function |
| 4785 | // is exited (and the declarator has been parsed). |
| 4786 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4787 | } |
| 4788 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4789 | // C++0x [dcl.fct]p14: |
| 4790 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4791 | // of a parameter-declaration-clause without a preceding comma. In |
| 4792 | // this case, the ellipsis is parsed as part of the |
| 4793 | // abstract-declarator if the type of the parameter names a template |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4794 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 4795 | // as part of the parameter-declaration-clause. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4796 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4797 | !((D.getContext() == Declarator::PrototypeContext || |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4798 | D.getContext() == Declarator::LambdaExprParameterContext || |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4799 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4800 | NextToken().is(tok::r_paren) && |
Richard Smith | 30f2a74 | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4801 | !D.hasGroupingParens() && |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4802 | !Actions.containsUnexpandedParameterPacks(D))) { |
| 4803 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 4804 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) { |
| 4805 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 4806 | // the user what they should have done. |
| 4807 | ParseDeclarator(D); |
| 4808 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4809 | return; |
| 4810 | } else |
| 4811 | D.setEllipsisLoc(EllipsisLoc); |
| 4812 | |
| 4813 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 4814 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 4815 | // the l_paren token. |
| 4816 | } |
| 4817 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4818 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 4819 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 4820 | // We found something that indicates the start of an unqualified-id. |
| 4821 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4822 | bool AllowConstructorName; |
| 4823 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 4824 | AllowConstructorName = false; |
| 4825 | else if (D.getCXXScopeSpec().isSet()) |
| 4826 | AllowConstructorName = |
| 4827 | (D.getContext() == Declarator::FileContext || |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 4828 | D.getContext() == Declarator::MemberContext); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4829 | else |
| 4830 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 4831 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4832 | SourceLocation TemplateKWLoc; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4833 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 4834 | /*EnteringContext=*/true, |
| 4835 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4836 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4837 | ParsedType(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4838 | TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4839 | D.getName()) || |
| 4840 | // Once we're past the identifier, if the scope was bad, mark the |
| 4841 | // whole declarator bad. |
| 4842 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4843 | D.SetIdentifier(0, Tok.getLocation()); |
| 4844 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4845 | } else { |
| 4846 | // Parsed the unqualified-id; update range information and move along. |
| 4847 | if (D.getSourceRange().getBegin().isInvalid()) |
| 4848 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 4849 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4850 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4851 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4852 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4853 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4854 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4855 | "There's a C++-specific check for tok::identifier above"); |
| 4856 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 4857 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 4858 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4859 | goto PastIdentifier; |
Richard Smith | a38253c | 2013-07-11 05:10:21 +0000 | [diff] [blame] | 4860 | } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) { |
Richard Smith | 8d1ab8a | 2013-10-13 22:12:28 +0000 | [diff] [blame] | 4861 | // A virt-specifier isn't treated as an identifier if it appears after a |
| 4862 | // trailing-return-type. |
| 4863 | if (D.getContext() != Declarator::TrailingReturnContext || |
| 4864 | !isCXX11VirtSpecifier(Tok)) { |
| 4865 | Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id) |
| 4866 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 4867 | D.SetIdentifier(0, Tok.getLocation()); |
| 4868 | ConsumeToken(); |
| 4869 | goto PastIdentifier; |
| 4870 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4871 | } |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4872 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4873 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4874 | // direct-declarator: '(' declarator ')' |
| 4875 | // direct-declarator: '(' attributes declarator ')' |
| 4876 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 4877 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4878 | |
| 4879 | // If the declarator was parenthesized, we entered the declarator |
| 4880 | // scope when parsing the parenthesized declarator, then exited |
| 4881 | // the scope already. Re-enter the scope, if we need to. |
| 4882 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4883 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4884 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4885 | if (!D.isInvalidType() && |
| 4886 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4887 | // Change the declaration context for name lookup, until this function |
| 4888 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4889 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4890 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4891 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4892 | // This could be something simple like "int" (in which case the declarator |
| 4893 | // portion is empty), if an abstract-declarator is allowed. |
| 4894 | D.SetIdentifier(0, Tok.getLocation()); |
Richard Smith | 30f2a74 | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4895 | |
| 4896 | // The grammar for abstract-pack-declarator does not allow grouping parens. |
| 4897 | // FIXME: Revisit this once core issue 1488 is resolved. |
| 4898 | if (D.hasEllipsis() && D.hasGroupingParens()) |
| 4899 | Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()), |
| 4900 | diag::ext_abstract_pack_declarator_parens); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4901 | } else { |
David Blaikie | e75d9cf | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 4902 | if (Tok.getKind() == tok::annot_pragma_parser_crash) |
David Blaikie | 377da4c | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 4903 | LLVM_BUILTIN_TRAP; |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 4904 | if (D.getContext() == Declarator::MemberContext) |
| 4905 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 4906 | << D.getDeclSpec().getSourceRange(); |
Richard Trieu | db55c04c | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 4907 | else if (getLangOpts().CPlusPlus) { |
| 4908 | if (Tok.is(tok::period) || Tok.is(tok::arrow)) |
| 4909 | Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow); |
Richard Trieu | efb288c | 2013-09-05 02:31:33 +0000 | [diff] [blame] | 4910 | else { |
| 4911 | SourceLocation Loc = D.getCXXScopeSpec().getEndLoc(); |
| 4912 | if (Tok.isAtStartOfLine() && Loc.isValid()) |
| 4913 | Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id) |
| 4914 | << getLangOpts().CPlusPlus; |
| 4915 | else |
| 4916 | Diag(Tok, diag::err_expected_unqualified_id) |
| 4917 | << getLangOpts().CPlusPlus; |
| 4918 | } |
Richard Trieu | db55c04c | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 4919 | } else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4920 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4921 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 4922 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4923 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4924 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4925 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4926 | assert(D.isPastIdentifier() && |
| 4927 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4928 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4929 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 4930 | if (D.hasName() && !D.getNumTypeObjects()) |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4931 | MaybeParseCXX11Attributes(D); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4932 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4933 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4934 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4935 | // Enter function-declaration scope, limiting any declarators to the |
| 4936 | // function prototype scope, including parameter declarators. |
| 4937 | ParseScope PrototypeScope(this, |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4938 | Scope::FunctionPrototypeScope|Scope::DeclScope| |
| 4939 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 4940 | ? Scope::FunctionDeclarationScope : 0)); |
| 4941 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4942 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 4943 | // In such a case, check if we actually have a function declarator; if it |
| 4944 | // is not, the declarator has been fully parsed. |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4945 | bool IsAmbiguous = false; |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 4946 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 4947 | // The name of the declarator, if any, is tentatively declared within |
| 4948 | // a possible direct initializer. |
| 4949 | TentativelyDeclaredIdentifiers.push_back(D.getIdentifier()); |
| 4950 | bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous); |
| 4951 | TentativelyDeclaredIdentifiers.pop_back(); |
| 4952 | if (!IsFunctionDecl) |
| 4953 | break; |
| 4954 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4955 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4956 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4957 | T.consumeOpen(); |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4958 | ParseFunctionDeclarator(D, attrs, T, IsAmbiguous); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4959 | PrototypeScope.Exit(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4960 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4961 | ParseBracketDeclarator(D); |
| 4962 | } else { |
| 4963 | break; |
| 4964 | } |
| 4965 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4966 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4967 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4968 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 4969 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4970 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4971 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 4972 | /// |
| 4973 | /// direct-declarator: |
| 4974 | /// '(' declarator ')' |
| 4975 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4976 | /// direct-declarator '(' parameter-type-list ')' |
| 4977 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4978 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4979 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4980 | /// |
| 4981 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4982 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4983 | T.consumeOpen(); |
| 4984 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4985 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4986 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4987 | // Eat any attributes before we look at whether this is a grouping or function |
| 4988 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 4989 | // the type being built up, for example: |
| 4990 | // int (__attribute__(()) *x)(long y) |
| 4991 | // If this ends up not being a grouping paren, the attribute applies to the |
| 4992 | // first argument, for example: |
| 4993 | // int (__attribute__(()) int x) |
| 4994 | // In either case, we need to eat any attributes to be able to determine what |
| 4995 | // sort of paren this is. |
| 4996 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4997 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4998 | bool RequiresArg = false; |
| 4999 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5000 | ParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5001 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5002 | // We require that the argument list (if this is a non-grouping paren) be |
| 5003 | // present even if the attribute list was empty. |
| 5004 | RequiresArg = true; |
| 5005 | } |
Chad Rosier | 9cab1c9 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 5006 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 5007 | // Eat any Microsoft extensions. |
Chad Rosier | 9cab1c9 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 5008 | ParseMicrosoftTypeAttributes(attrs); |
| 5009 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 5010 | // Eat any Borland extensions. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 5011 | if (Tok.is(tok::kw___pascal)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5012 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5013 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5014 | // If we haven't past the identifier yet (or where the identifier would be |
| 5015 | // stored, if this is an abstract declarator), then this is probably just |
| 5016 | // grouping parens. However, if this could be an abstract-declarator, then |
| 5017 | // this could also be the start of function arguments (consider 'void()'). |
| 5018 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5019 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5020 | if (!D.mayOmitIdentifier()) { |
| 5021 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 5022 | // paren, because we haven't seen the identifier yet. |
| 5023 | isGrouping = true; |
| 5024 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 5025 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 5026 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5027 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 5028 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5029 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 5030 | // considered to be a type, not a K&R identifier-list. |
| 5031 | isGrouping = false; |
| 5032 | } else { |
| 5033 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 5034 | isGrouping = true; |
| 5035 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5036 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5037 | // If this is a grouping paren, handle: |
| 5038 | // direct-declarator: '(' declarator ')' |
| 5039 | // direct-declarator: '(' attributes declarator ')' |
| 5040 | if (isGrouping) { |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5041 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 5042 | D.setEllipsisLoc(SourceLocation()); |
| 5043 | |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 5044 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 5045 | D.setGroupingParens(true); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 5046 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5047 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5048 | T.consumeClose(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5049 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5050 | T.getCloseLocation()), |
| 5051 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 5052 | |
| 5053 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 5054 | |
| 5055 | // An ellipsis cannot be placed outside parentheses. |
| 5056 | if (EllipsisLoc.isValid()) |
| 5057 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 5058 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5059 | return; |
| 5060 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5061 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5062 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 5063 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5064 | // identifier (and remember where it would have been), then call into |
| 5065 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5066 | D.SetIdentifier(0, Tok.getLocation()); |
| 5067 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5068 | // Enter function-declaration scope, limiting any declarators to the |
| 5069 | // function prototype scope, including parameter declarators. |
| 5070 | ParseScope PrototypeScope(this, |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 5071 | Scope::FunctionPrototypeScope | Scope::DeclScope | |
| 5072 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 5073 | ? Scope::FunctionDeclarationScope : 0)); |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5074 | ParseFunctionDeclarator(D, attrs, T, false, RequiresArg); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5075 | PrototypeScope.Exit(); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5076 | } |
| 5077 | |
| 5078 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 5079 | /// declarator D up to a paren, which indicates that we are parsing function |
| 5080 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5081 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5082 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 5083 | /// immediately after the open paren - they should be considered to be the |
| 5084 | /// first argument of a parameter. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5085 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5086 | /// If RequiresArg is true, then the first argument of the function is required |
| 5087 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5088 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5089 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 5090 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 5091 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 5092 | /// |
| 5093 | /// [C++11] exception-specification: |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5094 | /// dynamic-exception-specification |
| 5095 | /// noexcept-specification |
| 5096 | /// |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5097 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5098 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5099 | BalancedDelimiterTracker &Tracker, |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 5100 | bool IsAmbiguous, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5101 | bool RequiresArg) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5102 | assert(getCurScope()->isFunctionPrototypeScope() && |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 5103 | "Should call from a Function scope"); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5104 | // lparen is already consumed! |
| 5105 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 5106 | |
| 5107 | // This should be true when the function has typed arguments. |
| 5108 | // Otherwise, it is treated as a K&R-style function. |
| 5109 | bool HasProto = false; |
| 5110 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5111 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5112 | // Remember where we see an ellipsis, if any. |
| 5113 | SourceLocation EllipsisLoc; |
| 5114 | |
| 5115 | DeclSpec DS(AttrFactory); |
| 5116 | bool RefQualifierIsLValueRef = true; |
| 5117 | SourceLocation RefQualifierLoc; |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 5118 | SourceLocation ConstQualifierLoc; |
| 5119 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5120 | ExceptionSpecificationType ESpecType = EST_None; |
| 5121 | SourceRange ESpecRange; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5122 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 5123 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5124 | ExprResult NoexceptExpr; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5125 | ParsedAttributes FnAttrs(AttrFactory); |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 5126 | TypeResult TrailingReturnType; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5127 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 5128 | Actions.ActOnStartFunctionDeclarator(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5129 | /* LocalEndLoc is the end location for the local FunctionTypeLoc. |
| 5130 | EndLoc is the end location for the function declarator. |
| 5131 | They differ for trailing return types. */ |
| 5132 | SourceLocation StartLoc, LocalEndLoc, EndLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5133 | SourceLocation LParenLoc, RParenLoc; |
| 5134 | LParenLoc = Tracker.getOpenLocation(); |
| 5135 | StartLoc = LParenLoc; |
| 5136 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5137 | if (isFunctionDeclaratorIdentifierList()) { |
| 5138 | if (RequiresArg) |
| 5139 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5140 | |
| 5141 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 5142 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5143 | Tracker.consumeClose(); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5144 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5145 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5146 | EndLoc = RParenLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5147 | } else { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5148 | if (Tok.isNot(tok::r_paren)) |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5149 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, |
| 5150 | EllipsisLoc); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5151 | else if (RequiresArg) |
| 5152 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5153 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5154 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5155 | |
| 5156 | // If we have the closing ')', eat it. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5157 | Tracker.consumeClose(); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5158 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5159 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5160 | EndLoc = RParenLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5161 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5162 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5163 | // FIXME: Accept these components in any order, and produce fixits to |
| 5164 | // correct the order if the user gets it wrong. Ideally we should deal |
| 5165 | // with the virt-specifier-seq and pure-specifier in the same way. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5166 | |
| 5167 | // Parse cv-qualifier-seq[opt]. |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5168 | ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false, |
| 5169 | /*CXX11AttributesAllowed*/ false, |
| 5170 | /*AtomicAllowed*/ false); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5171 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 5172 | EndLoc = DS.getSourceRange().getEnd(); |
| 5173 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 5174 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 5175 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5176 | |
| 5177 | // Parse ref-qualifier[opt]. |
| 5178 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5179 | Diag(Tok, getLangOpts().CPlusPlus11 ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5180 | diag::warn_cxx98_compat_ref_qualifier : |
| 5181 | diag::ext_ref_qualifier); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5182 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5183 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 5184 | RefQualifierLoc = ConsumeToken(); |
| 5185 | EndLoc = RefQualifierLoc; |
| 5186 | } |
| 5187 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5188 | // C++11 [expr.prim.general]p3: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5189 | // If a declaration declares a member function or member function |
| 5190 | // template of a class X, the expression this is a prvalue of type |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5191 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5192 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5193 | // declarator. |
Richard Smith | d922779 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5194 | // FIXME: currently, "static" case isn't handled correctly. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5195 | bool IsCXX11MemberFunction = |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5196 | getLangOpts().CPlusPlus11 && |
Richard Smith | d922779 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5197 | (D.getContext() == Declarator::MemberContext |
| 5198 | ? !D.getDeclSpec().isFriendSpecified() |
| 5199 | : D.getContext() == Declarator::FileContext && |
| 5200 | D.getCXXScopeSpec().isValid() && |
| 5201 | Actions.CurContext->isRecord()); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5202 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 5203 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
Richard Smith | 7b19cb1 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 5204 | DS.getTypeQualifiers() | |
Richard Smith | 8404626 | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 5205 | (D.getDeclSpec().isConstexprSpecified() && |
| 5206 | !getLangOpts().CPlusPlus1y |
Richard Smith | 7b19cb1 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 5207 | ? Qualifiers::Const : 0), |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5208 | IsCXX11MemberFunction); |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5209 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5210 | // Parse exception-specification[opt]. |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5211 | ESpecType = tryParseExceptionSpecification(ESpecRange, |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 5212 | DynamicExceptions, |
| 5213 | DynamicExceptionRanges, |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5214 | NoexceptExpr); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5215 | if (ESpecType != EST_None) |
| 5216 | EndLoc = ESpecRange.getEnd(); |
| 5217 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5218 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 5219 | // after the exception-specification. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5220 | MaybeParseCXX11Attributes(FnAttrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5221 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5222 | // Parse trailing-return-type[opt]. |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5223 | LocalEndLoc = EndLoc; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5224 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5225 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5226 | if (D.getDeclSpec().getTypeSpecType() == TST_auto) |
| 5227 | StartLoc = D.getDeclSpec().getTypeSpecTypeLoc(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5228 | LocalEndLoc = Tok.getLocation(); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 5229 | SourceRange Range; |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 5230 | TrailingReturnType = ParseTrailingReturnType(Range); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5231 | EndLoc = Range.getEnd(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5232 | } |
| 5233 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5234 | } |
| 5235 | |
| 5236 | // Remember that we parsed a function type, and remember the attributes. |
| 5237 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5238 | IsAmbiguous, |
| 5239 | LParenLoc, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5240 | ParamInfo.data(), ParamInfo.size(), |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5241 | EllipsisLoc, RParenLoc, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5242 | DS.getTypeQualifiers(), |
| 5243 | RefQualifierIsLValueRef, |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 5244 | RefQualifierLoc, ConstQualifierLoc, |
| 5245 | VolatileQualifierLoc, |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 5246 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5247 | ESpecType, ESpecRange.getBegin(), |
| 5248 | DynamicExceptions.data(), |
| 5249 | DynamicExceptionRanges.data(), |
| 5250 | DynamicExceptions.size(), |
| 5251 | NoexceptExpr.isUsable() ? |
| 5252 | NoexceptExpr.get() : 0, |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5253 | StartLoc, LocalEndLoc, D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5254 | TrailingReturnType), |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5255 | FnAttrs, EndLoc); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 5256 | |
| 5257 | Actions.ActOnEndFunctionDeclarator(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5258 | } |
| 5259 | |
| 5260 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 5261 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 5262 | /// |
| 5263 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 5264 | /// abstract-declarators. |
| 5265 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5266 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5267 | && Tok.is(tok::identifier) |
| 5268 | && !TryAltiVecVectorToken() |
| 5269 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 5270 | // 6.7.5.3p11. |
| 5271 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 5272 | // Identifier lists follow a really simple grammar: the identifiers can |
| 5273 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 5274 | // identifier lists are really rare in the brave new modern world, and |
| 5275 | // it is very common for someone to typo a type in a non-K&R style |
| 5276 | // list. If we are presented with something like: "void foo(intptr x, |
| 5277 | // float y)", we don't want to start parsing the function declarator as |
| 5278 | // though it is a K&R style declarator just because intptr is an |
| 5279 | // invalid type. |
| 5280 | // |
| 5281 | // To handle this, we check to see if the token after the first |
| 5282 | // identifier is a "," or ")". Only then do we parse it as an |
| 5283 | // identifier list. |
| 5284 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 5285 | } |
| 5286 | |
| 5287 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 5288 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 5289 | /// |
| 5290 | /// After returning, ParamInfo will hold the parsed parameters. |
| 5291 | /// |
| 5292 | /// identifier-list: [C99 6.7.5] |
| 5293 | /// identifier |
| 5294 | /// identifier-list ',' identifier |
| 5295 | /// |
| 5296 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 5297 | Declarator &D, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5298 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5299 | // If there was no identifier specified for the declarator, either we are in |
| 5300 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 5301 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 5302 | // diagnose this. |
| 5303 | if (!D.getIdentifier()) |
| 5304 | Diag(Tok, diag::ext_ident_list_in_param); |
| 5305 | |
| 5306 | // Maintain an efficient lookup of params we have seen so far. |
| 5307 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 5308 | |
| 5309 | while (1) { |
| 5310 | // If this isn't an identifier, report the error and skip until ')'. |
| 5311 | if (Tok.isNot(tok::identifier)) { |
| 5312 | Diag(Tok, diag::err_expected_ident); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5313 | SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5314 | // Forget we parsed anything. |
| 5315 | ParamInfo.clear(); |
| 5316 | return; |
| 5317 | } |
| 5318 | |
| 5319 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 5320 | |
| 5321 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 5322 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 5323 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 5324 | |
| 5325 | // Verify that the argument identifier has not already been mentioned. |
| 5326 | if (!ParamsSoFar.insert(ParmII)) { |
| 5327 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 5328 | } else { |
| 5329 | // Remember this identifier in ParamInfo. |
| 5330 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 5331 | Tok.getLocation(), |
| 5332 | 0)); |
| 5333 | } |
| 5334 | |
| 5335 | // Eat the identifier. |
| 5336 | ConsumeToken(); |
| 5337 | |
| 5338 | // The list continues if we see a comma. |
| 5339 | if (Tok.isNot(tok::comma)) |
| 5340 | break; |
| 5341 | ConsumeToken(); |
| 5342 | } |
| 5343 | } |
| 5344 | |
| 5345 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 5346 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 5347 | /// identifier list. |
| 5348 | /// |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5349 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 5350 | /// caller parsed those arguments immediately after the open paren - they should |
| 5351 | /// be considered to be part of the first parameter. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5352 | /// |
| 5353 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 5354 | /// be the location of the ellipsis, if any was parsed. |
| 5355 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5356 | /// parameter-type-list: [C99 6.7.5] |
| 5357 | /// parameter-list |
| 5358 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5359 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5360 | /// |
| 5361 | /// parameter-list: [C99 6.7.5] |
| 5362 | /// parameter-declaration |
| 5363 | /// parameter-list ',' parameter-declaration |
| 5364 | /// |
| 5365 | /// parameter-declaration: [C99 6.7.5] |
| 5366 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5367 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5368 | /// [C++11] initializer-clause |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5369 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 5370 | /// declaration-specifiers abstract-declarator[opt] |
| 5371 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 5372 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5373 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5374 | /// [C++11] attribute-specifier-seq parameter-declaration |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5375 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5376 | void Parser::ParseParameterDeclarationClause( |
| 5377 | Declarator &D, |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5378 | ParsedAttributes &FirstArgAttrs, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5379 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5380 | SourceLocation &EllipsisLoc) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5381 | while (1) { |
| 5382 | if (Tok.is(tok::ellipsis)) { |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5383 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 5384 | // before deciding this was a parameter-declaration-clause. |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 5385 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5386 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5387 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5388 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5389 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5390 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5391 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5392 | |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5393 | // Parse any C++11 attributes. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5394 | MaybeParseCXX11Attributes(DS.getAttributes()); |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5395 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5396 | // Skip any Microsoft attributes before a param. |
Chad Rosier | 16f90bf | 2012-12-20 20:37:53 +0000 | [diff] [blame] | 5397 | MaybeParseMicrosoftAttributes(DS.getAttributes()); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5398 | |
| 5399 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5400 | |
| 5401 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5402 | // Take them so that we only apply the attributes to the first parameter. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5403 | // FIXME: If we can leave the attributes in the token stream somehow, we can |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5404 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 5405 | // too much hassle. |
| 5406 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5407 | |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 5408 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5409 | |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5410 | |
| 5411 | // Parse the declarator. This is "PrototypeContext" or |
| 5412 | // "LambdaExprParameterContext", because we must accept either |
| 5413 | // 'declarator' or 'abstract-declarator' here. |
| 5414 | Declarator ParmDeclarator(DS, |
| 5415 | D.getContext() == Declarator::LambdaExprContext ? |
| 5416 | Declarator::LambdaExprParameterContext : |
| 5417 | Declarator::PrototypeContext); |
| 5418 | ParseDeclarator(ParmDeclarator); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5419 | |
| 5420 | // Parse GNU attributes, if present. |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5421 | MaybeParseGNUAttributes(ParmDeclarator); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5422 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5423 | // Remember this parsed parameter in ParamInfo. |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5424 | IdentifierInfo *ParmII = ParmDeclarator.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5425 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5426 | // DefArgToks is used when the parsing of default arguments needs |
| 5427 | // to be delayed. |
| 5428 | CachedTokens *DefArgToks = 0; |
| 5429 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5430 | // If no parameter was specified, verify that *something* was specified, |
| 5431 | // otherwise we have a missing type and identifier. |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5432 | if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 && |
| 5433 | ParmDeclarator.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5434 | // Completely missing, emit error. |
| 5435 | Diag(DSStart, diag::err_missing_param); |
| 5436 | } else { |
| 5437 | // Otherwise, we have something. Add it and let semantic analysis try |
| 5438 | // to grok it and add the result to the ParamInfo we are building. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5439 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5440 | // Inform the actions module about the parameter declarator, so it gets |
| 5441 | // added to the current scope. |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5442 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), |
| 5443 | ParmDeclarator); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5444 | // Parse the default argument, if any. We parse the default |
| 5445 | // arguments in all dialects; the semantic analysis in |
| 5446 | // ActOnParamDefaultArgument will reject the default argument in |
| 5447 | // C. |
| 5448 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5449 | SourceLocation EqualLoc = Tok.getLocation(); |
| 5450 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5451 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5452 | if (D.getContext() == Declarator::MemberContext) { |
| 5453 | // If we're inside a class definition, cache the tokens |
| 5454 | // corresponding to the default argument. We'll actually parse |
| 5455 | // them when we see the end of the class definition. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5456 | // FIXME: Can we use a smart pointer for Toks? |
| 5457 | DefArgToks = new CachedTokens; |
| 5458 | |
Richard Smith | 9bd3cdc | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 5459 | if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5460 | delete DefArgToks; |
| 5461 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5462 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5463 | } else { |
| 5464 | // Mark the end of the default argument so that we know when to |
| 5465 | // stop when we parse it later on. |
| 5466 | Token DefArgEnd; |
| 5467 | DefArgEnd.startToken(); |
| 5468 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 5469 | DefArgEnd.setLocation(Tok.getLocation()); |
| 5470 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5471 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 5472 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5473 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5474 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5475 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5476 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5477 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5478 | // The argument isn't actually potentially evaluated unless it is |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5479 | // used. |
| 5480 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 5481 | Sema::PotentiallyEvaluatedIfUsed, |
| 5482 | Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5483 | |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5484 | ExprResult DefArgResult; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5485 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5486 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5487 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5488 | } else |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5489 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5490 | if (DefArgResult.isInvalid()) { |
| 5491 | Actions.ActOnParamDefaultArgumentError(Param); |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5492 | SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5493 | } else { |
| 5494 | // Inform the actions module about the default argument |
| 5495 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5496 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5497 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5498 | } |
| 5499 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5500 | |
| 5501 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5502 | ParmDeclarator.getIdentifierLoc(), |
| 5503 | Param, DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5504 | } |
| 5505 | |
| 5506 | // If the next token is a comma, consume it and keep reading arguments. |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5507 | if (Tok.isNot(tok::comma)) { |
| 5508 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5509 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5510 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5511 | if (!getLangOpts().CPlusPlus) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5512 | // We have ellipsis without a preceding ',', which is ill-formed |
| 5513 | // in C. Complain and provide the fix. |
| 5514 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5515 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5516 | } |
| 5517 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5518 | |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5519 | break; |
| 5520 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5521 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5522 | // Consume the comma. |
| 5523 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5524 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5525 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 5526 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5527 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5528 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 5529 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 5530 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 5531 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 5532 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5533 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 5534 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5535 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5536 | if (CheckProhibitedCXX11Attribute()) |
| 5537 | return; |
| 5538 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5539 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 5540 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5541 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5542 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 5543 | // This code does a fast path to handle some of the most obvious cases. |
| 5544 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5545 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5546 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5547 | MaybeParseCXX11Attributes(attrs); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5548 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5549 | // Remember that we parsed the empty array type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5550 | ExprResult NumElements; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5551 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5552 | T.getOpenLocation(), |
| 5553 | T.getCloseLocation()), |
| 5554 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5555 | return; |
| 5556 | } else if (Tok.getKind() == tok::numeric_constant && |
| 5557 | GetLookAheadToken(1).is(tok::r_square)) { |
| 5558 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5559 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5560 | ConsumeToken(); |
| 5561 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5562 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5563 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5564 | MaybeParseCXX11Attributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5565 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5566 | // Remember that we parsed a array type, and remember its features. |
Nikola Smiljanic | ebf0fa8 | 2013-01-11 08:33:05 +0000 | [diff] [blame] | 5567 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5568 | ExprRes.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5569 | T.getOpenLocation(), |
| 5570 | T.getCloseLocation()), |
| 5571 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5572 | return; |
| 5573 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5574 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5575 | // If valid, this location is the position where we read the 'static' keyword. |
| 5576 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5577 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5578 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5579 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5580 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5581 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5582 | DeclSpec DS(AttrFactory); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5583 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5584 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5585 | // If we haven't already read 'static', check to see if there is one after the |
| 5586 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5587 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5588 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5589 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5590 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 5591 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5592 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5593 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5594 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 5595 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 5596 | // the token after the star is a ']'. Since stars in arrays are |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5597 | // infrequent, use of lookahead is not costly here. |
| 5598 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 5599 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5600 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5601 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5602 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5603 | StaticLoc = SourceLocation(); // Drop the static. |
| 5604 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5605 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5606 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5607 | // Note, in C89, this production uses the constant-expr production instead |
| 5608 | // of assignment-expr. The only difference is that assignment-expr allows |
| 5609 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 5610 | // are not i-c-e's, so we don't need to distinguish between the two here. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5611 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5612 | // Parse the constant-expression or assignment-expression now (depending |
| 5613 | // on dialect). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5614 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5615 | NumElements = ParseConstantExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5616 | } else { |
| 5617 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 5618 | Sema::ConstantEvaluated); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5619 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5620 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5621 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5622 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5623 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 5624 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 5625 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5626 | // If the expression was invalid, skip it. |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5627 | SkipUntil(tok::r_square, StopAtSemi); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5628 | return; |
| 5629 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5630 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5631 | T.consumeClose(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5632 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5633 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5634 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5635 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5636 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5637 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5638 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5639 | NumElements.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5640 | T.getOpenLocation(), |
| 5641 | T.getCloseLocation()), |
| 5642 | attrs, T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5643 | } |
| 5644 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5645 | /// [GNU] typeof-specifier: |
| 5646 | /// typeof ( expressions ) |
| 5647 | /// typeof ( type-name ) |
| 5648 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5649 | /// |
| 5650 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5651 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5652 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5653 | SourceLocation StartLoc = ConsumeToken(); |
| 5654 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5655 | const bool hasParens = Tok.is(tok::l_paren); |
| 5656 | |
Eli Friedman | 80bfa3d | 2012-09-26 04:34:21 +0000 | [diff] [blame] | 5657 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated, |
| 5658 | Sema::ReuseLambdaContextDecl); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5659 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5660 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5661 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5662 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5663 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 5664 | CastTy, CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5665 | if (hasParens) |
| 5666 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5667 | |
| 5668 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5669 | // FIXME: Not accurate, the range gets one token more than it should. |
| 5670 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5671 | else |
| 5672 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5673 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5674 | if (isCastExpr) { |
| 5675 | if (!CastTy) { |
| 5676 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5677 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 5678 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5679 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5680 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5681 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5682 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5683 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5684 | DiagID, CastTy)) |
| 5685 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5686 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5687 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5688 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5689 | // If we get here, the operand to the typeof was an expresion. |
| 5690 | if (Operand.isInvalid()) { |
| 5691 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 5692 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5693 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5694 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5695 | // We might need to transform the operand if it is potentially evaluated. |
| 5696 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 5697 | if (Operand.isInvalid()) { |
| 5698 | DS.SetTypeSpecError(); |
| 5699 | return; |
| 5700 | } |
| 5701 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5702 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5703 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5704 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5705 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5706 | DiagID, Operand.get())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5707 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5708 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5709 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 5710 | /// [C11] atomic-specifier: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5711 | /// _Atomic ( type-name ) |
| 5712 | /// |
| 5713 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5714 | assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) && |
| 5715 | "Not an atomic specifier"); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5716 | |
| 5717 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5718 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5719 | if (T.consumeOpen()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5720 | return; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5721 | |
| 5722 | TypeResult Result = ParseTypeName(); |
| 5723 | if (Result.isInvalid()) { |
Alexey Bataev | 8fe2475 | 2013-11-18 08:17:37 +0000 | [diff] [blame] | 5724 | SkipUntil(tok::r_paren, StopAtSemi); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5725 | return; |
| 5726 | } |
| 5727 | |
| 5728 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5729 | T.consumeClose(); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5730 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5731 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5732 | return; |
| 5733 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5734 | DS.setTypeofParensRange(T.getRange()); |
| 5735 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5736 | |
| 5737 | const char *PrevSpec = 0; |
| 5738 | unsigned DiagID; |
| 5739 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
| 5740 | DiagID, Result.release())) |
| 5741 | Diag(StartLoc, DiagID) << PrevSpec; |
| 5742 | } |
| 5743 | |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5744 | |
| 5745 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 5746 | /// from TryAltiVecVectorToken. |
| 5747 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 5748 | Token Next = NextToken(); |
| 5749 | switch (Next.getKind()) { |
| 5750 | default: return false; |
| 5751 | case tok::kw_short: |
| 5752 | case tok::kw_long: |
| 5753 | case tok::kw_signed: |
| 5754 | case tok::kw_unsigned: |
| 5755 | case tok::kw_void: |
| 5756 | case tok::kw_char: |
| 5757 | case tok::kw_int: |
| 5758 | case tok::kw_float: |
| 5759 | case tok::kw_double: |
| 5760 | case tok::kw_bool: |
| 5761 | case tok::kw___pixel: |
| 5762 | Tok.setKind(tok::kw___vector); |
| 5763 | return true; |
| 5764 | case tok::identifier: |
| 5765 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5766 | Tok.setKind(tok::kw___vector); |
| 5767 | return true; |
| 5768 | } |
Bill Schmidt | 3e3d20b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5769 | if (Next.getIdentifierInfo() == Ident_bool) { |
| 5770 | Tok.setKind(tok::kw___vector); |
| 5771 | return true; |
| 5772 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5773 | return false; |
| 5774 | } |
| 5775 | } |
| 5776 | |
| 5777 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 5778 | const char *&PrevSpec, unsigned &DiagID, |
| 5779 | bool &isInvalid) { |
| 5780 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 5781 | Token Next = NextToken(); |
| 5782 | switch (Next.getKind()) { |
| 5783 | case tok::kw_short: |
| 5784 | case tok::kw_long: |
| 5785 | case tok::kw_signed: |
| 5786 | case tok::kw_unsigned: |
| 5787 | case tok::kw_void: |
| 5788 | case tok::kw_char: |
| 5789 | case tok::kw_int: |
| 5790 | case tok::kw_float: |
| 5791 | case tok::kw_double: |
| 5792 | case tok::kw_bool: |
| 5793 | case tok::kw___pixel: |
| 5794 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5795 | return true; |
| 5796 | case tok::identifier: |
| 5797 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5798 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5799 | return true; |
| 5800 | } |
Bill Schmidt | 3e3d20b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5801 | if (Next.getIdentifierInfo() == Ident_bool) { |
| 5802 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5803 | return true; |
| 5804 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5805 | break; |
| 5806 | default: |
| 5807 | break; |
| 5808 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 5809 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5810 | DS.isTypeAltiVecVector()) { |
| 5811 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 5812 | return true; |
Bill Schmidt | 3e3d20b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5813 | } else if ((Tok.getIdentifierInfo() == Ident_bool) && |
| 5814 | DS.isTypeAltiVecVector()) { |
| 5815 | isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID); |
| 5816 | return true; |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5817 | } |
| 5818 | return false; |
| 5819 | } |