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")) { |
| 127 | SkipUntil(tok::r_paren, true); // 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, "(")) { |
| 131 | SkipUntil(tok::r_paren, true); // 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)) |
Richard Smith | d92aa2d | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 175 | SkipUntil(tok::r_paren); |
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)) |
| 178 | SkipUntil(tok::r_paren); |
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. |
| 291 | if (AttrKind == AttributeList::UnknownAttribute) { |
| 292 | const Token &Next = NextToken(); |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 293 | IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma); |
Richard Smith | d92aa2d | 2013-10-24 01:07:54 +0000 | [diff] [blame] | 294 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 295 | |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 296 | if (IsIdentifierArg) |
| 297 | ArgExprs.push_back(ParseIdentifierLoc()); |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 300 | if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) { |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 301 | // Eat the comma. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 302 | if (!ArgExprs.empty()) |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 303 | ConsumeToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 304 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 305 | // Parse the non-empty comma-separated list of expressions. |
| 306 | while (1) { |
| 307 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 308 | if (ArgExpr.isInvalid()) { |
| 309 | SkipUntil(tok::r_paren); |
| 310 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 311 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 312 | ArgExprs.push_back(ArgExpr.release()); |
| 313 | if (Tok.isNot(tok::comma)) |
| 314 | break; |
| 315 | ConsumeToken(); // Eat the comma, move to the next argument |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 316 | } |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 317 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 318 | |
| 319 | SourceLocation RParen = Tok.getLocation(); |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 320 | if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 321 | SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc; |
Richard Smith | d386fef | 2013-10-31 01:56:18 +0000 | [diff] [blame] | 322 | Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc, |
| 323 | ArgExprs.data(), ArgExprs.size(), Syntax); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 327 | /// \brief Parses a single argument for a declspec, including the |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 328 | /// surrounding parens. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 329 | void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 330 | SourceLocation AttrNameLoc, |
| 331 | ParsedAttributes &Attrs) |
| 332 | { |
| 333 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 334 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 335 | AttrName->getNameStart(), tok::r_paren)) |
| 336 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 337 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 338 | ExprResult ArgExpr(ParseConstantExpression()); |
| 339 | if (ArgExpr.isInvalid()) { |
| 340 | T.skipToEnd(); |
| 341 | return; |
| 342 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 343 | ArgsUnion ExprList = ArgExpr.take(); |
| 344 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1, |
| 345 | AttributeList::AS_Declspec); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 346 | |
| 347 | T.consumeClose(); |
| 348 | } |
| 349 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 350 | /// \brief Determines whether a declspec is a "simple" one requiring no |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 351 | /// arguments. |
| 352 | bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) { |
| 353 | return llvm::StringSwitch<bool>(Ident->getName()) |
| 354 | .Case("dllimport", true) |
| 355 | .Case("dllexport", true) |
| 356 | .Case("noreturn", true) |
| 357 | .Case("nothrow", true) |
| 358 | .Case("noinline", true) |
| 359 | .Case("naked", true) |
| 360 | .Case("appdomain", true) |
| 361 | .Case("process", true) |
| 362 | .Case("jitintrinsic", true) |
| 363 | .Case("noalias", true) |
| 364 | .Case("restrict", true) |
| 365 | .Case("novtable", true) |
| 366 | .Case("selectany", true) |
| 367 | .Case("thread", true) |
Aaron Ballman | 3ce0de6 | 2013-05-04 16:58:37 +0000 | [diff] [blame] | 368 | .Case("safebuffers", true ) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 369 | .Default(false); |
| 370 | } |
| 371 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 372 | /// \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] | 373 | /// parameters). Will return false if we properly handled the declspec, or |
| 374 | /// true if it is an unknown declspec. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 375 | void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 376 | SourceLocation Loc, |
| 377 | ParsedAttributes &Attrs) { |
| 378 | // Try to handle the easy case first -- these declspecs all take a single |
| 379 | // parameter as their argument. |
| 380 | if (llvm::StringSwitch<bool>(Ident->getName()) |
| 381 | .Case("uuid", true) |
| 382 | .Case("align", true) |
| 383 | .Case("allocate", true) |
| 384 | .Default(false)) { |
| 385 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 386 | } else if (Ident->getName() == "deprecated") { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 387 | // The deprecated declspec has an optional single argument, so we will |
| 388 | // 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] | 389 | // not. |
| 390 | if (Tok.getKind() == tok::l_paren) |
| 391 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 392 | else |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 393 | Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 394 | } else if (Ident->getName() == "property") { |
| 395 | // 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] | 396 | // assignment expressions as a parameter, but the lhs of the assignment |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 397 | // must be named get or put. |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 398 | if (Tok.isNot(tok::l_paren)) { |
| 399 | Diag(Tok.getLocation(), diag::err_expected_lparen_after) |
| 400 | << Ident->getNameStart(); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 401 | return; |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 402 | } |
| 403 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 404 | T.expectAndConsume(diag::err_expected_lparen_after, |
| 405 | Ident->getNameStart(), tok::r_paren); |
| 406 | |
| 407 | enum AccessorKind { |
| 408 | AK_Invalid = -1, |
| 409 | AK_Put = 0, AK_Get = 1 // indices into AccessorNames |
| 410 | }; |
| 411 | IdentifierInfo *AccessorNames[] = { 0, 0 }; |
| 412 | bool HasInvalidAccessor = false; |
| 413 | |
| 414 | // Parse the accessor specifications. |
| 415 | while (true) { |
| 416 | // Stop if this doesn't look like an accessor spec. |
| 417 | if (!Tok.is(tok::identifier)) { |
| 418 | // If the user wrote a completely empty list, use a special diagnostic. |
| 419 | if (Tok.is(tok::r_paren) && !HasInvalidAccessor && |
| 420 | AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) { |
| 421 | Diag(Loc, diag::err_ms_property_no_getter_or_putter); |
| 422 | break; |
| 423 | } |
| 424 | |
| 425 | Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor); |
| 426 | break; |
| 427 | } |
| 428 | |
| 429 | AccessorKind Kind; |
| 430 | SourceLocation KindLoc = Tok.getLocation(); |
| 431 | StringRef KindStr = Tok.getIdentifierInfo()->getName(); |
| 432 | if (KindStr == "get") { |
| 433 | Kind = AK_Get; |
| 434 | } else if (KindStr == "put") { |
| 435 | Kind = AK_Put; |
| 436 | |
| 437 | // Recover from the common mistake of using 'set' instead of 'put'. |
| 438 | } else if (KindStr == "set") { |
| 439 | Diag(KindLoc, diag::err_ms_property_has_set_accessor) |
| 440 | << FixItHint::CreateReplacement(KindLoc, "put"); |
| 441 | Kind = AK_Put; |
| 442 | |
| 443 | // Handle the mistake of forgetting the accessor kind by skipping |
| 444 | // this accessor. |
| 445 | } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) { |
| 446 | Diag(KindLoc, diag::err_ms_property_missing_accessor_kind); |
| 447 | ConsumeToken(); |
| 448 | HasInvalidAccessor = true; |
| 449 | goto next_property_accessor; |
| 450 | |
| 451 | // Otherwise, complain about the unknown accessor kind. |
| 452 | } else { |
| 453 | Diag(KindLoc, diag::err_ms_property_unknown_accessor); |
| 454 | HasInvalidAccessor = true; |
| 455 | Kind = AK_Invalid; |
| 456 | |
| 457 | // Try to keep parsing unless it doesn't look like an accessor spec. |
| 458 | if (!NextToken().is(tok::equal)) break; |
| 459 | } |
| 460 | |
| 461 | // Consume the identifier. |
| 462 | ConsumeToken(); |
| 463 | |
| 464 | // Consume the '='. |
| 465 | if (Tok.is(tok::equal)) { |
| 466 | ConsumeToken(); |
| 467 | } else { |
| 468 | Diag(Tok.getLocation(), diag::err_ms_property_expected_equal) |
| 469 | << KindStr; |
| 470 | break; |
| 471 | } |
| 472 | |
| 473 | // Expect the method name. |
| 474 | if (!Tok.is(tok::identifier)) { |
| 475 | Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name); |
| 476 | break; |
| 477 | } |
| 478 | |
| 479 | if (Kind == AK_Invalid) { |
| 480 | // Just drop invalid accessors. |
| 481 | } else if (AccessorNames[Kind] != NULL) { |
| 482 | // Complain about the repeated accessor, ignore it, and keep parsing. |
| 483 | Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr; |
| 484 | } else { |
| 485 | AccessorNames[Kind] = Tok.getIdentifierInfo(); |
| 486 | } |
| 487 | ConsumeToken(); |
| 488 | |
| 489 | next_property_accessor: |
| 490 | // Keep processing accessors until we run out. |
| 491 | if (Tok.is(tok::comma)) { |
| 492 | ConsumeAnyToken(); |
| 493 | continue; |
| 494 | |
| 495 | // If we run into the ')', stop without consuming it. |
| 496 | } else if (Tok.is(tok::r_paren)) { |
| 497 | break; |
| 498 | } else { |
| 499 | Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen); |
| 500 | break; |
| 501 | } |
| 502 | } |
| 503 | |
| 504 | // Only add the property attribute if it was well-formed. |
| 505 | if (!HasInvalidAccessor) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 506 | Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(), |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 507 | AccessorNames[AK_Get], AccessorNames[AK_Put], |
| 508 | AttributeList::AS_Declspec); |
| 509 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 510 | T.skipToEnd(); |
| 511 | } else { |
| 512 | // We don't recognize this as a valid declspec, but instead of creating the |
| 513 | // attribute and allowing sema to warn about it, we will warn here instead. |
| 514 | // This is because some attributes have multiple spellings, but we need to |
| 515 | // 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] | 516 | // attribute, we'd have to split the valid declspec spelling logic into |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 517 | // both locations. |
| 518 | Diag(Loc, diag::warn_ms_declspec_unknown) << Ident; |
| 519 | |
| 520 | // If there's an open paren, we should eat the open and close parens under |
| 521 | // the assumption that this unknown declspec has parameters. |
| 522 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 523 | if (!T.consumeOpen()) |
| 524 | T.skipToEnd(); |
| 525 | } |
| 526 | } |
| 527 | |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 528 | /// [MS] decl-specifier: |
| 529 | /// __declspec ( extended-decl-modifier-seq ) |
| 530 | /// |
| 531 | /// [MS] extended-decl-modifier-seq: |
| 532 | /// extended-decl-modifier[opt] |
| 533 | /// extended-decl-modifier extended-decl-modifier-seq |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 534 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) { |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 535 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 536 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 537 | ConsumeToken(); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 538 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 539 | if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec", |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 540 | tok::r_paren)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 541 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 542 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 543 | // An empty declspec is perfectly legal and should not warn. Additionally, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 544 | // you can specify multiple attributes per declspec. |
| 545 | while (Tok.getKind() != tok::r_paren) { |
| 546 | // We expect either a well-known identifier or a generic string. Anything |
| 547 | // else is a malformed declspec. |
| 548 | bool IsString = Tok.getKind() == tok::string_literal ? true : false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 549 | if (!IsString && Tok.getKind() != tok::identifier && |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 550 | Tok.getKind() != tok::kw_restrict) { |
| 551 | Diag(Tok, diag::err_ms_declspec_type); |
| 552 | T.skipToEnd(); |
| 553 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 554 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 555 | |
| 556 | IdentifierInfo *AttrName; |
| 557 | SourceLocation AttrNameLoc; |
| 558 | if (IsString) { |
| 559 | SmallString<8> StrBuffer; |
| 560 | bool Invalid = false; |
| 561 | StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid); |
| 562 | if (Invalid) { |
| 563 | T.skipToEnd(); |
| 564 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 565 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 566 | AttrName = PP.getIdentifierInfo(Str); |
| 567 | AttrNameLoc = ConsumeStringToken(); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 568 | } else { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 569 | AttrName = Tok.getIdentifierInfo(); |
| 570 | AttrNameLoc = ConsumeToken(); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 571 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 572 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 573 | if (IsString || IsSimpleMicrosoftDeclSpec(AttrName)) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 574 | // If we have a generic string, we will allow it because there is no |
| 575 | // documented list of allowable string declspecs, but we know they exist |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 576 | // (for instance, SAL declspecs in older versions of MSVC). |
| 577 | // |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 578 | // Alternatively, if the identifier is a simple one, then it requires no |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 579 | // arguments and can be turned into an attribute directly. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 580 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 581 | AttributeList::AS_Declspec); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 582 | else |
| 583 | ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 584 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 585 | T.consumeClose(); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 586 | } |
| 587 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 588 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 589 | // Treat these like attributes |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 590 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 591 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 592 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 593 | Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) || |
| 594 | Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 595 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 596 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 597 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 598 | AttributeList::AS_Keyword); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 599 | } |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 600 | } |
| 601 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 602 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 603 | // Treat these like attributes |
| 604 | while (Tok.is(tok::kw___pascal)) { |
| 605 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 606 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 607 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 608 | AttributeList::AS_Keyword); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 609 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 610 | } |
| 611 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 612 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 613 | // Treat these like attributes |
| 614 | while (Tok.is(tok::kw___kernel)) { |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 615 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 616 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 617 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 618 | AttributeList::AS_Keyword); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 622 | void Parser::ParseOpenCLQualifiers(DeclSpec &DS) { |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 623 | // FIXME: The mapping from attribute spelling to semantics should be |
| 624 | // performed in Sema, not here. |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 625 | SourceLocation Loc = Tok.getLocation(); |
| 626 | switch(Tok.getKind()) { |
| 627 | // OpenCL qualifiers: |
| 628 | case tok::kw___private: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 629 | case tok::kw_private: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 630 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 631 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 632 | PP.getIdentifierInfo("address_space"), Loc, 0); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 633 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 634 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 635 | case tok::kw___global: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 636 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 637 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 638 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 639 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 640 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 641 | case tok::kw___local: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 642 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 643 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 644 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 645 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 646 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 647 | case tok::kw___constant: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 648 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 649 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 650 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 651 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 652 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 653 | case tok::kw___read_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 654 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 655 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 656 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 657 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 658 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 659 | case tok::kw___write_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 660 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 661 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 662 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 663 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 664 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 665 | case tok::kw___read_write: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 666 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 667 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 668 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 669 | break; |
| 670 | default: break; |
| 671 | } |
| 672 | } |
| 673 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 674 | /// \brief Parse a version number. |
| 675 | /// |
| 676 | /// version: |
| 677 | /// simple-integer |
| 678 | /// simple-integer ',' simple-integer |
| 679 | /// simple-integer ',' simple-integer ',' simple-integer |
| 680 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 681 | Range = Tok.getLocation(); |
| 682 | |
| 683 | if (!Tok.is(tok::numeric_constant)) { |
| 684 | Diag(Tok, diag::err_expected_version); |
| 685 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 686 | return VersionTuple(); |
| 687 | } |
| 688 | |
| 689 | // Parse the major (and possibly minor and subminor) versions, which |
| 690 | // are stored in the numeric constant. We utilize a quirk of the |
| 691 | // lexer, which is that it handles something like 1.2.3 as a single |
| 692 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 693 | SmallString<512> Buffer; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 694 | Buffer.resize(Tok.getLength()+1); |
| 695 | const char *ThisTokBegin = &Buffer[0]; |
| 696 | |
| 697 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 698 | bool Invalid = false; |
| 699 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 700 | if (Invalid) |
| 701 | return VersionTuple(); |
| 702 | |
| 703 | // Parse the major version. |
| 704 | unsigned AfterMajor = 0; |
| 705 | unsigned Major = 0; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 706 | while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 707 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 708 | ++AfterMajor; |
| 709 | } |
| 710 | |
| 711 | if (AfterMajor == 0) { |
| 712 | Diag(Tok, diag::err_expected_version); |
| 713 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 714 | return VersionTuple(); |
| 715 | } |
| 716 | |
| 717 | if (AfterMajor == ActualLength) { |
| 718 | ConsumeToken(); |
| 719 | |
| 720 | // We only had a single version component. |
| 721 | if (Major == 0) { |
| 722 | Diag(Tok, diag::err_zero_version); |
| 723 | return VersionTuple(); |
| 724 | } |
| 725 | |
| 726 | return VersionTuple(Major); |
| 727 | } |
| 728 | |
| 729 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 730 | Diag(Tok, diag::err_expected_version); |
| 731 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 732 | return VersionTuple(); |
| 733 | } |
| 734 | |
| 735 | // Parse the minor version. |
| 736 | unsigned AfterMinor = AfterMajor + 1; |
| 737 | unsigned Minor = 0; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 738 | while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 739 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 740 | ++AfterMinor; |
| 741 | } |
| 742 | |
| 743 | if (AfterMinor == ActualLength) { |
| 744 | ConsumeToken(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 745 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 746 | // We had major.minor. |
| 747 | if (Major == 0 && Minor == 0) { |
| 748 | Diag(Tok, diag::err_zero_version); |
| 749 | return VersionTuple(); |
| 750 | } |
| 751 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 752 | return VersionTuple(Major, Minor); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | // If what follows is not a '.', we have a problem. |
| 756 | if (ThisTokBegin[AfterMinor] != '.') { |
| 757 | Diag(Tok, diag::err_expected_version); |
| 758 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 759 | return VersionTuple(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | // Parse the subminor version. |
| 763 | unsigned AfterSubminor = AfterMinor + 1; |
| 764 | unsigned Subminor = 0; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 765 | while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 766 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 767 | ++AfterSubminor; |
| 768 | } |
| 769 | |
| 770 | if (AfterSubminor != ActualLength) { |
| 771 | Diag(Tok, diag::err_expected_version); |
| 772 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 773 | return VersionTuple(); |
| 774 | } |
| 775 | ConsumeToken(); |
| 776 | return VersionTuple(Major, Minor, Subminor); |
| 777 | } |
| 778 | |
| 779 | /// \brief Parse the contents of the "availability" attribute. |
| 780 | /// |
| 781 | /// availability-attribute: |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 782 | /// 'availability' '(' platform ',' version-arg-list, opt-message')' |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 783 | /// |
| 784 | /// platform: |
| 785 | /// identifier |
| 786 | /// |
| 787 | /// version-arg-list: |
| 788 | /// version-arg |
| 789 | /// version-arg ',' version-arg-list |
| 790 | /// |
| 791 | /// version-arg: |
| 792 | /// 'introduced' '=' version |
| 793 | /// 'deprecated' '=' version |
Douglas Gregor | 93a7067 | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 794 | /// 'obsoleted' = version |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 795 | /// 'unavailable' |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 796 | /// opt-message: |
| 797 | /// 'message' '=' <string> |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 798 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 799 | SourceLocation AvailabilityLoc, |
| 800 | ParsedAttributes &attrs, |
| 801 | SourceLocation *endLoc) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 802 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 803 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 804 | ExprResult MessageExpr; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 805 | |
| 806 | // Opening '('. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 807 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 808 | if (T.consumeOpen()) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 809 | Diag(Tok, diag::err_expected_lparen); |
| 810 | return; |
| 811 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 812 | |
| 813 | // Parse the platform name, |
| 814 | if (Tok.isNot(tok::identifier)) { |
| 815 | Diag(Tok, diag::err_availability_expected_platform); |
| 816 | SkipUntil(tok::r_paren); |
| 817 | return; |
| 818 | } |
Richard Smith | 8edabd9 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 819 | IdentifierLoc *Platform = ParseIdentifierLoc(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 820 | |
| 821 | // Parse the ',' following the platform name. |
| 822 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren)) |
| 823 | return; |
| 824 | |
| 825 | // If we haven't grabbed the pointers for the identifiers |
| 826 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 827 | if (!Ident_introduced) { |
| 828 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 829 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 830 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 831 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 832 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 836 | SourceLocation UnavailableLoc; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 837 | do { |
| 838 | if (Tok.isNot(tok::identifier)) { |
| 839 | Diag(Tok, diag::err_availability_expected_change); |
| 840 | SkipUntil(tok::r_paren); |
| 841 | return; |
| 842 | } |
| 843 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 844 | SourceLocation KeywordLoc = ConsumeToken(); |
| 845 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 846 | if (Keyword == Ident_unavailable) { |
| 847 | if (UnavailableLoc.isValid()) { |
| 848 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 849 | << Keyword << SourceRange(UnavailableLoc); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 850 | } |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 851 | UnavailableLoc = KeywordLoc; |
| 852 | |
| 853 | if (Tok.isNot(tok::comma)) |
| 854 | break; |
| 855 | |
| 856 | ConsumeToken(); |
| 857 | continue; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 860 | if (Tok.isNot(tok::equal)) { |
| 861 | Diag(Tok, diag::err_expected_equal_after) |
| 862 | << Keyword; |
| 863 | SkipUntil(tok::r_paren); |
| 864 | return; |
| 865 | } |
| 866 | ConsumeToken(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 867 | if (Keyword == Ident_message) { |
Benjamin Kramer | c561714 | 2013-09-13 17:31:48 +0000 | [diff] [blame] | 868 | if (Tok.isNot(tok::string_literal)) { // Also reject wide string literals. |
Andy Gibbs | 97f8461 | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 869 | Diag(Tok, diag::err_expected_string_literal) |
| 870 | << /*Source='availability attribute'*/2; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 871 | SkipUntil(tok::r_paren); |
| 872 | return; |
| 873 | } |
| 874 | MessageExpr = ParseStringLiteralExpression(); |
| 875 | break; |
| 876 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 877 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 878 | SourceRange VersionRange; |
| 879 | VersionTuple Version = ParseVersionTuple(VersionRange); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 880 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 881 | if (Version.empty()) { |
| 882 | SkipUntil(tok::r_paren); |
| 883 | return; |
| 884 | } |
| 885 | |
| 886 | unsigned Index; |
| 887 | if (Keyword == Ident_introduced) |
| 888 | Index = Introduced; |
| 889 | else if (Keyword == Ident_deprecated) |
| 890 | Index = Deprecated; |
| 891 | else if (Keyword == Ident_obsoleted) |
| 892 | Index = Obsoleted; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 893 | else |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 894 | Index = Unknown; |
| 895 | |
| 896 | if (Index < Unknown) { |
| 897 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 898 | Diag(KeywordLoc, diag::err_availability_redundant) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 899 | << Keyword |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 900 | << SourceRange(Changes[Index].KeywordLoc, |
| 901 | Changes[Index].VersionRange.getEnd()); |
| 902 | } |
| 903 | |
| 904 | Changes[Index].KeywordLoc = KeywordLoc; |
| 905 | Changes[Index].Version = Version; |
| 906 | Changes[Index].VersionRange = VersionRange; |
| 907 | } else { |
| 908 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 909 | << Keyword << VersionRange; |
| 910 | } |
| 911 | |
| 912 | if (Tok.isNot(tok::comma)) |
| 913 | break; |
| 914 | |
| 915 | ConsumeToken(); |
| 916 | } while (true); |
| 917 | |
| 918 | // Closing ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 919 | if (T.consumeClose()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 920 | return; |
| 921 | |
| 922 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 923 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 924 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 925 | // The 'unavailable' availability cannot be combined with any other |
| 926 | // availability changes. Make sure that hasn't happened. |
| 927 | if (UnavailableLoc.isValid()) { |
| 928 | bool Complained = false; |
| 929 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 930 | if (Changes[Index].KeywordLoc.isValid()) { |
| 931 | if (!Complained) { |
| 932 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 933 | << SourceRange(Changes[Index].KeywordLoc, |
| 934 | Changes[Index].VersionRange.getEnd()); |
| 935 | Complained = true; |
| 936 | } |
| 937 | |
| 938 | // Clear out the availability. |
| 939 | Changes[Index] = AvailabilityChange(); |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 944 | // Record this attribute |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 945 | attrs.addNew(&Availability, |
| 946 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Fariborz Jahanian | f96708d | 2012-01-23 23:38:32 +0000 | [diff] [blame] | 947 | 0, AvailabilityLoc, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 948 | Platform, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 949 | Changes[Introduced], |
| 950 | Changes[Deprecated], |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 951 | Changes[Obsoleted], |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 952 | UnavailableLoc, MessageExpr.take(), |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 953 | AttributeList::AS_GNU); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 956 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 957 | // Late Parsed Attributes: |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 958 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 959 | |
| 960 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 961 | |
| 962 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 963 | Self->ParseLexedAttributes(*Class); |
| 964 | } |
| 965 | |
| 966 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 967 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 971 | /// scope appropriately. |
| 972 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 973 | // Deal with templates |
| 974 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 975 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 976 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 977 | HasTemplateScope); |
| 978 | if (HasTemplateScope) |
| 979 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 980 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 981 | // Set or update the scope flags. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 982 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 983 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 984 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 985 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 986 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 987 | // Enter the scope of nested classes |
| 988 | if (!AlreadyHasClassScope) |
| 989 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 990 | Class.TagOrTemplate); |
Benjamin Kramer | 268efba | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 991 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 992 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 993 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 994 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 995 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 996 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 997 | if (!AlreadyHasClassScope) |
| 998 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 999 | Class.TagOrTemplate); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1002 | |
| 1003 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 1004 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 1005 | bool EnterScope, bool OnDefinition) { |
DeLesley Hutchins | 161db02 | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1006 | assert(LAs.parseSoon() && |
| 1007 | "Attribute list should be marked for immediate parsing."); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1008 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 95526a4 | 2012-08-15 22:41:04 +0000 | [diff] [blame] | 1009 | if (D) |
| 1010 | LAs[i]->addDecl(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1011 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | d306cf7 | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 1012 | delete LAs[i]; |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1013 | } |
| 1014 | LAs.clear(); |
| 1015 | } |
| 1016 | |
| 1017 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1018 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 1019 | /// This will be called at the end of parsing a class declaration |
| 1020 | /// for each LateParsedAttribute. We consume the saved tokens and |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1021 | /// create an attribute with the arguments filled in. We add this |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1022 | /// to the Attribute list for the decl. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1023 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 1024 | bool EnterScope, bool OnDefinition) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1025 | // Save the current token position. |
| 1026 | SourceLocation OrigLoc = Tok.getLocation(); |
| 1027 | |
| 1028 | // Append the current token at the end of the new token stream so that it |
| 1029 | // doesn't get lost. |
| 1030 | LA.Toks.push_back(Tok); |
| 1031 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 1032 | // Consume the previously pushed token. |
Argyrios Kyrtzidis | ab2d09b | 2013-03-27 23:58:17 +0000 | [diff] [blame] | 1033 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1034 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1035 | if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) { |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1036 | // FIXME: Do not warn on C++11 attributes, once we start supporting |
| 1037 | // them here. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1038 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 1039 | << LA.AttrName.getName(); |
| 1040 | } |
| 1041 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1042 | ParsedAttributes Attrs(AttrFactory); |
| 1043 | SourceLocation endLoc; |
| 1044 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1045 | if (LA.Decls.size() > 0) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1046 | Decl *D = LA.Decls[0]; |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1047 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1048 | RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1049 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1050 | // Allow 'this' within late-parsed attributes. |
Richard Smith | cafeb94 | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 1051 | Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0, |
| 1052 | ND && ND->isCXXInstanceMember()); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1053 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1054 | if (LA.Decls.size() == 1) { |
| 1055 | // If the Decl is templatized, add template parameters to scope. |
| 1056 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 1057 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 1058 | if (HasTemplateScope) |
| 1059 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1060 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1061 | // If the Decl is on a function, add function parameters to the scope. |
| 1062 | bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
| 1063 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope); |
| 1064 | if (HasFunScope) |
| 1065 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1066 | |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1067 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 1068 | 0, SourceLocation(), AttributeList::AS_GNU); |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1069 | |
| 1070 | if (HasFunScope) { |
| 1071 | Actions.ActOnExitFunctionContext(); |
| 1072 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 1073 | } |
| 1074 | if (HasTemplateScope) { |
| 1075 | TempScope.Exit(); |
| 1076 | } |
| 1077 | } else { |
| 1078 | // If there are multiple decls, then the decl cannot be within the |
| 1079 | // function scope. |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1080 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 1081 | 0, SourceLocation(), AttributeList::AS_GNU); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1082 | } |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 1083 | } else { |
| 1084 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1087 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) { |
| 1088 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
| 1089 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1090 | |
| 1091 | if (Tok.getLocation() != OrigLoc) { |
| 1092 | // Due to a parsing error, we either went over the cached tokens or |
| 1093 | // there are still cached tokens left, so we skip the leftover tokens. |
| 1094 | // Since this is an uncommon situation that should be avoided, use the |
| 1095 | // expensive isBeforeInTranslationUnit call. |
| 1096 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 1097 | OrigLoc)) |
| 1098 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
Douglas Gregor | d78ef5b | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 1099 | ConsumeAnyToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1103 | /// \brief Wrapper around a case statement checking if AttrName is |
| 1104 | /// one of the thread safety attributes |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1105 | bool Parser::IsThreadSafetyAttribute(StringRef AttrName) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1106 | return llvm::StringSwitch<bool>(AttrName) |
| 1107 | .Case("guarded_by", true) |
| 1108 | .Case("guarded_var", true) |
| 1109 | .Case("pt_guarded_by", true) |
| 1110 | .Case("pt_guarded_var", true) |
| 1111 | .Case("lockable", true) |
| 1112 | .Case("scoped_lockable", true) |
| 1113 | .Case("no_thread_safety_analysis", true) |
| 1114 | .Case("acquired_after", true) |
| 1115 | .Case("acquired_before", true) |
| 1116 | .Case("exclusive_lock_function", true) |
| 1117 | .Case("shared_lock_function", true) |
| 1118 | .Case("exclusive_trylock_function", true) |
| 1119 | .Case("shared_trylock_function", true) |
| 1120 | .Case("unlock_function", true) |
| 1121 | .Case("lock_returned", true) |
| 1122 | .Case("locks_excluded", true) |
| 1123 | .Case("exclusive_locks_required", true) |
| 1124 | .Case("shared_locks_required", true) |
| 1125 | .Default(false); |
| 1126 | } |
| 1127 | |
| 1128 | /// \brief Parse the contents of thread safety attributes. These |
| 1129 | /// should always be parsed as an expression list. |
| 1130 | /// |
| 1131 | /// We need to special case the parsing due to the fact that if the first token |
| 1132 | /// of the first argument is an identifier, the main parse loop will store |
| 1133 | /// that token as a "parameter" and the rest of |
| 1134 | /// the arguments will be added to a list of "arguments". However, |
| 1135 | /// subsequent tokens in the first argument are lost. We instead parse each |
| 1136 | /// argument as an expression and add all arguments to the list of "arguments". |
| 1137 | /// In future, we will take advantage of this special case to also |
| 1138 | /// deal with some argument scoping issues here (for example, referring to a |
| 1139 | /// function parameter in the attribute on that function). |
| 1140 | void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName, |
| 1141 | SourceLocation AttrNameLoc, |
| 1142 | ParsedAttributes &Attrs, |
| 1143 | SourceLocation *EndLoc) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1144 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1145 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1146 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1147 | T.consumeOpen(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1148 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1149 | ArgsVector ArgExprs; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1150 | bool ArgExprsOk = true; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1151 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1152 | // now parse the list of expressions |
DeLesley Hutchins | 4805f15 | 2011-12-14 19:36:06 +0000 | [diff] [blame] | 1153 | while (Tok.isNot(tok::r_paren)) { |
DeLesley Hutchins | ed4330b | 2013-02-07 19:01:07 +0000 | [diff] [blame] | 1154 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1155 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 1156 | if (ArgExpr.isInvalid()) { |
| 1157 | ArgExprsOk = false; |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1158 | T.consumeClose(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1159 | break; |
| 1160 | } else { |
| 1161 | ArgExprs.push_back(ArgExpr.release()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1162 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1163 | if (Tok.isNot(tok::comma)) |
| 1164 | break; |
| 1165 | ConsumeToken(); // Eat the comma, move to the next argument |
| 1166 | } |
| 1167 | // Match the ')'. |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 1168 | if (ArgExprsOk && !T.consumeClose()) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1169 | Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, ArgExprs.data(), |
| 1170 | ArgExprs.size(), AttributeList::AS_GNU); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1171 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1172 | if (EndLoc) |
| 1173 | *EndLoc = T.getCloseLocation(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1176 | void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName, |
| 1177 | SourceLocation AttrNameLoc, |
| 1178 | ParsedAttributes &Attrs, |
| 1179 | SourceLocation *EndLoc) { |
| 1180 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 1181 | |
| 1182 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1183 | T.consumeOpen(); |
| 1184 | |
| 1185 | if (Tok.isNot(tok::identifier)) { |
| 1186 | Diag(Tok, diag::err_expected_ident); |
| 1187 | T.skipToEnd(); |
| 1188 | return; |
| 1189 | } |
Richard Smith | 8edabd9 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 1190 | IdentifierLoc *ArgumentKind = ParseIdentifierLoc(); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1191 | |
| 1192 | if (Tok.isNot(tok::comma)) { |
| 1193 | Diag(Tok, diag::err_expected_comma); |
| 1194 | T.skipToEnd(); |
| 1195 | return; |
| 1196 | } |
| 1197 | ConsumeToken(); |
| 1198 | |
| 1199 | SourceRange MatchingCTypeRange; |
| 1200 | TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange); |
| 1201 | if (MatchingCType.isInvalid()) { |
| 1202 | T.skipToEnd(); |
| 1203 | return; |
| 1204 | } |
| 1205 | |
| 1206 | bool LayoutCompatible = false; |
| 1207 | bool MustBeNull = false; |
| 1208 | while (Tok.is(tok::comma)) { |
| 1209 | ConsumeToken(); |
| 1210 | if (Tok.isNot(tok::identifier)) { |
| 1211 | Diag(Tok, diag::err_expected_ident); |
| 1212 | T.skipToEnd(); |
| 1213 | return; |
| 1214 | } |
| 1215 | IdentifierInfo *Flag = Tok.getIdentifierInfo(); |
| 1216 | if (Flag->isStr("layout_compatible")) |
| 1217 | LayoutCompatible = true; |
| 1218 | else if (Flag->isStr("must_be_null")) |
| 1219 | MustBeNull = true; |
| 1220 | else { |
| 1221 | Diag(Tok, diag::err_type_safety_unknown_flag) << Flag; |
| 1222 | T.skipToEnd(); |
| 1223 | return; |
| 1224 | } |
| 1225 | ConsumeToken(); // consume flag |
| 1226 | } |
| 1227 | |
| 1228 | if (!T.consumeClose()) { |
| 1229 | Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1230 | ArgumentKind, MatchingCType.release(), |
| 1231 | LayoutCompatible, MustBeNull, |
| 1232 | AttributeList::AS_GNU); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | if (EndLoc) |
| 1236 | *EndLoc = T.getCloseLocation(); |
| 1237 | } |
| 1238 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1239 | /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets |
| 1240 | /// of a C++11 attribute-specifier in a location where an attribute is not |
| 1241 | /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this |
| 1242 | /// situation. |
| 1243 | /// |
| 1244 | /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if |
| 1245 | /// this doesn't appear to actually be an attribute-specifier, and the caller |
| 1246 | /// should try to parse it. |
| 1247 | bool Parser::DiagnoseProhibitedCXX11Attribute() { |
| 1248 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)); |
| 1249 | |
| 1250 | switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) { |
| 1251 | case CAK_NotAttributeSpecifier: |
| 1252 | // No diagnostic: we're in Obj-C++11 and this is not actually an attribute. |
| 1253 | return false; |
| 1254 | |
| 1255 | case CAK_InvalidAttributeSpecifier: |
| 1256 | Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute); |
| 1257 | return false; |
| 1258 | |
| 1259 | case CAK_AttributeSpecifier: |
| 1260 | // Parse and discard the attributes. |
| 1261 | SourceLocation BeginLoc = ConsumeBracket(); |
| 1262 | ConsumeBracket(); |
| 1263 | SkipUntil(tok::r_square, /*StopAtSemi*/ false); |
| 1264 | assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied"); |
| 1265 | SourceLocation EndLoc = ConsumeBracket(); |
| 1266 | Diag(BeginLoc, diag::err_attributes_not_allowed) |
| 1267 | << SourceRange(BeginLoc, EndLoc); |
| 1268 | return true; |
| 1269 | } |
Chandler Carruth | 2c6dbd7 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 1270 | llvm_unreachable("All cases handled above."); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1271 | } |
| 1272 | |
Richard Smith | 975d52c | 2013-02-20 01:17:14 +0000 | [diff] [blame] | 1273 | /// \brief We have found the opening square brackets of a C++11 |
| 1274 | /// attribute-specifier in a location where an attribute is not permitted, but |
| 1275 | /// we know where the attributes ought to be written. Parse them anyway, and |
| 1276 | /// provide a fixit moving them to the right place. |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1277 | void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs, |
| 1278 | SourceLocation CorrectLocation) { |
| 1279 | assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) || |
| 1280 | Tok.is(tok::kw_alignas)); |
| 1281 | |
| 1282 | // Consume the attributes. |
| 1283 | SourceLocation Loc = Tok.getLocation(); |
| 1284 | ParseCXX11Attributes(Attrs); |
| 1285 | CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true); |
| 1286 | |
| 1287 | Diag(Loc, diag::err_attributes_not_allowed) |
| 1288 | << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange) |
| 1289 | << FixItHint::CreateRemoval(AttrRange); |
| 1290 | } |
| 1291 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1292 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 1293 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 1294 | << attrs.Range; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1295 | } |
| 1296 | |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1297 | void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) { |
| 1298 | AttributeList *AttrList = attrs.getList(); |
| 1299 | while (AttrList) { |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1300 | if (AttrList->isCXX11Attribute()) { |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 1301 | Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr) |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1302 | << AttrList->getName(); |
| 1303 | AttrList->setInvalid(); |
| 1304 | } |
| 1305 | AttrList = AttrList->getNext(); |
| 1306 | } |
| 1307 | } |
| 1308 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1309 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 1310 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1311 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 1312 | /// location of the semicolon in DeclEnd. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1313 | /// |
| 1314 | /// declaration: [C99 6.7] |
| 1315 | /// block-declaration -> |
| 1316 | /// simple-declaration |
| 1317 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1318 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1319 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1320 | /// [C++] using-directive |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1321 | /// [C++] using-declaration |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1322 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1323 | /// others... [FIXME] |
| 1324 | /// |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1325 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 1326 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1327 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1328 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 1329 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | e8cff36 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 1330 | // Must temporarily exit the objective-c container scope for |
| 1331 | // parsing c none objective-c decls. |
| 1332 | ObjCDeclContextSwitch ObjCDC(*this); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1333 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1334 | Decl *SingleDecl = 0; |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1335 | Decl *OwnedType = 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1336 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1337 | case tok::kw_template: |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1338 | case tok::kw_export: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1339 | ProhibitAttributes(attrs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1340 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1341 | break; |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1342 | case tok::kw_inline: |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 1343 | // 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] | 1344 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1345 | ProhibitAttributes(attrs); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1346 | SourceLocation InlineLoc = ConsumeToken(); |
| 1347 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 1348 | break; |
| 1349 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1350 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1351 | true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1352 | case tok::kw_namespace: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1353 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1354 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1355 | break; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1356 | case tok::kw_using: |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1357 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1358 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1359 | break; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1360 | case tok::kw_static_assert: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1361 | case tok::kw__Static_assert: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1362 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1363 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1364 | break; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1365 | default: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1366 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1367 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1368 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1369 | // 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] | 1370 | // single decl, convert it now. Alias declarations can also declare a type; |
| 1371 | // include that too if it is present. |
| 1372 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1376 | /// declaration-specifiers init-declarator-list[opt] ';' |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1377 | /// [C++11] attribute-specifier-seq decl-specifier-seq[opt] |
| 1378 | /// init-declarator-list ';' |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1379 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 1380 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1381 | /// |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1382 | /// for-range-declaration: [C++11 6.5p1: stmt.ranged] |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1383 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1384 | /// |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1385 | /// 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] | 1386 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1387 | /// |
| 1388 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 1389 | /// of a simple-declaration. If we find that we are, we also parse the |
| 1390 | /// for-range-initializer, and place it here. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1391 | Parser::DeclGroupPtrTy |
| 1392 | Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context, |
| 1393 | SourceLocation &DeclEnd, |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1394 | ParsedAttributesWithRange &Attrs, |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1395 | bool RequireSemi, ForRangeInit *FRI) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1396 | // Parse the common declaration-specifiers piece. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1397 | ParsingDeclSpec DS(*this); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1398 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1399 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1400 | getDeclSpecContextFromDeclaratorContext(Context)); |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1401 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1402 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1403 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1404 | if (Tok.is(tok::semi)) { |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1405 | ProhibitAttributes(Attrs); |
Argyrios Kyrtzidis | 5641b0d | 2012-05-16 23:49:15 +0000 | [diff] [blame] | 1406 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1407 | if (RequireSemi) ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1408 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1409 | DS); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1410 | DS.complete(TheDecl); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1411 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1412 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1413 | |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1414 | DS.takeAttributesFrom(Attrs); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1415 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1416 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1417 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1418 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1419 | /// for a declarator. |
| 1420 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1421 | switch (Tok.getKind()) { |
| 1422 | case tok::annot_cxxscope: |
| 1423 | case tok::annot_template_id: |
| 1424 | case tok::caret: |
| 1425 | case tok::code_completion: |
| 1426 | case tok::coloncolon: |
| 1427 | case tok::ellipsis: |
| 1428 | case tok::kw___attribute: |
| 1429 | case tok::kw_operator: |
| 1430 | case tok::l_paren: |
| 1431 | case tok::star: |
| 1432 | return true; |
| 1433 | |
| 1434 | case tok::amp: |
| 1435 | case tok::ampamp: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1436 | return getLangOpts().CPlusPlus; |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1437 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1438 | case tok::l_square: // Might be an attribute on an unnamed bit-field. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1439 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 && |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1440 | NextToken().is(tok::l_square); |
| 1441 | |
| 1442 | case tok::colon: // Might be a typo for '::' or an unnamed bit-field. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1443 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1444 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1445 | case tok::identifier: |
| 1446 | switch (NextToken().getKind()) { |
| 1447 | case tok::code_completion: |
| 1448 | case tok::coloncolon: |
| 1449 | case tok::comma: |
| 1450 | case tok::equal: |
| 1451 | case tok::equalequal: // Might be a typo for '='. |
| 1452 | case tok::kw_alignas: |
| 1453 | case tok::kw_asm: |
| 1454 | case tok::kw___attribute: |
| 1455 | case tok::l_brace: |
| 1456 | case tok::l_paren: |
| 1457 | case tok::l_square: |
| 1458 | case tok::less: |
| 1459 | case tok::r_brace: |
| 1460 | case tok::r_paren: |
| 1461 | case tok::r_square: |
| 1462 | case tok::semi: |
| 1463 | return true; |
| 1464 | |
| 1465 | case tok::colon: |
| 1466 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1467 | // and in block scope it's probably a label. Inside a class definition, |
| 1468 | // this is a bit-field. |
| 1469 | return Context == Declarator::MemberContext || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1470 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1471 | |
| 1472 | case tok::identifier: // Possible virt-specifier. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1473 | return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken()); |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1474 | |
| 1475 | default: |
| 1476 | return false; |
| 1477 | } |
| 1478 | |
| 1479 | default: |
| 1480 | return false; |
| 1481 | } |
| 1482 | } |
| 1483 | |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1484 | /// Skip until we reach something which seems like a sensible place to pick |
| 1485 | /// up parsing after a malformed declaration. This will sometimes stop sooner |
| 1486 | /// than SkipUntil(tok::r_brace) would, but will never stop later. |
| 1487 | void Parser::SkipMalformedDecl() { |
| 1488 | while (true) { |
| 1489 | switch (Tok.getKind()) { |
| 1490 | case tok::l_brace: |
| 1491 | // Skip until matching }, then stop. We've probably skipped over |
| 1492 | // a malformed class or function definition or similar. |
| 1493 | ConsumeBrace(); |
| 1494 | SkipUntil(tok::r_brace, /*StopAtSemi*/false); |
| 1495 | if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) { |
| 1496 | // This declaration isn't over yet. Keep skipping. |
| 1497 | continue; |
| 1498 | } |
| 1499 | if (Tok.is(tok::semi)) |
| 1500 | ConsumeToken(); |
| 1501 | return; |
| 1502 | |
| 1503 | case tok::l_square: |
| 1504 | ConsumeBracket(); |
| 1505 | SkipUntil(tok::r_square, /*StopAtSemi*/false); |
| 1506 | continue; |
| 1507 | |
| 1508 | case tok::l_paren: |
| 1509 | ConsumeParen(); |
| 1510 | SkipUntil(tok::r_paren, /*StopAtSemi*/false); |
| 1511 | continue; |
| 1512 | |
| 1513 | case tok::r_brace: |
| 1514 | return; |
| 1515 | |
| 1516 | case tok::semi: |
| 1517 | ConsumeToken(); |
| 1518 | return; |
| 1519 | |
| 1520 | case tok::kw_inline: |
| 1521 | // 'inline namespace' at the start of a line is almost certainly |
Jordan Rose | 94f29f4 | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1522 | // a good place to pick back up parsing, except in an Objective-C |
| 1523 | // @interface context. |
| 1524 | if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) && |
| 1525 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1526 | return; |
| 1527 | break; |
| 1528 | |
| 1529 | case tok::kw_namespace: |
| 1530 | // 'namespace' at the start of a line is almost certainly a good |
Jordan Rose | 94f29f4 | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1531 | // place to pick back up parsing, except in an Objective-C |
| 1532 | // @interface context. |
| 1533 | if (Tok.isAtStartOfLine() && |
| 1534 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
| 1535 | return; |
| 1536 | break; |
| 1537 | |
| 1538 | case tok::at: |
| 1539 | // @end is very much like } in Objective-C contexts. |
| 1540 | if (NextToken().isObjCAtKeyword(tok::objc_end) && |
| 1541 | ParsingInObjCContainer) |
| 1542 | return; |
| 1543 | break; |
| 1544 | |
| 1545 | case tok::minus: |
| 1546 | case tok::plus: |
| 1547 | // - and + probably start new method declarations in Objective-C contexts. |
| 1548 | if (Tok.isAtStartOfLine() && ParsingInObjCContainer) |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1549 | return; |
| 1550 | break; |
| 1551 | |
| 1552 | case tok::eof: |
| 1553 | return; |
| 1554 | |
| 1555 | default: |
| 1556 | break; |
| 1557 | } |
| 1558 | |
| 1559 | ConsumeAnyToken(); |
| 1560 | } |
| 1561 | } |
| 1562 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1563 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1564 | /// definition or a group of object declarations, actually parse the |
| 1565 | /// result. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1566 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1567 | unsigned Context, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1568 | bool AllowFunctionDefinitions, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1569 | SourceLocation *DeclEnd, |
| 1570 | ForRangeInit *FRI) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1571 | // Parse the first declarator. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1572 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1573 | ParseDeclarator(D); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1574 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1575 | // Bail out if the first declarator didn't seem well-formed. |
| 1576 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1577 | SkipMalformedDecl(); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1578 | return DeclGroupPtrTy(); |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1579 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1580 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1581 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1582 | // appropriate function scope after the function Decl has been constructed. |
DeLesley Hutchins | 161db02 | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1583 | // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList. |
| 1584 | LateParsedAttrList LateParsedAttrs(true); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1585 | if (D.isFunctionDeclarator()) |
| 1586 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1587 | |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1588 | // 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] | 1589 | if (D.isFunctionDeclarator() && |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1590 | // Look at the next token to make sure that this isn't a function |
| 1591 | // declaration. We have to check this because __attribute__ might be the |
| 1592 | // start of a function definition in GCC-extended K&R C. |
Fariborz Jahanian | be1d4ec | 2012-08-10 15:54:40 +0000 | [diff] [blame] | 1593 | !isDeclarationAfterDeclarator()) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1594 | |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1595 | if (AllowFunctionDefinitions) { |
| 1596 | if (isStartOfFunctionDefinition(D)) { |
| 1597 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1598 | Diag(Tok, diag::err_function_declared_typedef); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1599 | |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1600 | // Recover by treating the 'typedef' as spurious. |
| 1601 | DS.ClearStorageClassSpecs(); |
| 1602 | } |
| 1603 | |
| 1604 | Decl *TheDecl = |
| 1605 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
| 1606 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1607 | } |
| 1608 | |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1609 | if (isDeclarationSpecifier()) { |
| 1610 | // If there is an invalid declaration specifier right after the function |
| 1611 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1612 | // actually a body. Just fall through into the code that handles it as a |
| 1613 | // prototype, and let the top-level code handle the erroneous declspec |
| 1614 | // where it would otherwise expect a comma or semicolon. |
| 1615 | } else { |
| 1616 | Diag(Tok, diag::err_expected_fn_body); |
| 1617 | SkipUntil(tok::semi); |
| 1618 | return DeclGroupPtrTy(); |
| 1619 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1620 | } else { |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1621 | if (Tok.is(tok::l_brace)) { |
| 1622 | Diag(Tok, diag::err_function_definition_not_allowed); |
| 1623 | SkipUntil(tok::r_brace, true, true); |
| 1624 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1625 | } |
| 1626 | } |
| 1627 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1628 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1629 | return DeclGroupPtrTy(); |
| 1630 | |
| 1631 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1632 | // must parse and analyze the for-range-initializer before the declaration is |
| 1633 | // analyzed. |
Douglas Gregor | 12849d0 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1634 | // |
| 1635 | // Handle the Objective-C for-in loop variable similarly, although we |
| 1636 | // don't need to parse the container in advance. |
| 1637 | if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) { |
| 1638 | bool IsForRangeLoop = false; |
| 1639 | if (Tok.is(tok::colon)) { |
| 1640 | IsForRangeLoop = true; |
| 1641 | FRI->ColonLoc = ConsumeToken(); |
| 1642 | if (Tok.is(tok::l_brace)) |
| 1643 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1644 | else |
| 1645 | FRI->RangeExpr = ParseExpression(); |
| 1646 | } |
| 1647 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1648 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | 12849d0 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1649 | if (IsForRangeLoop) |
| 1650 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1651 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | 6895a64 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 1652 | D.complete(ThisDecl); |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1653 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1654 | } |
| 1655 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1656 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1657 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1658 | if (LateParsedAttrs.size() > 0) |
| 1659 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1660 | D.complete(FirstDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1661 | if (FirstDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1662 | DeclsInGroup.push_back(FirstDecl); |
| 1663 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1664 | bool ExpectSemi = Context != Declarator::ForContext; |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 1665 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1666 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1667 | // error, bail out. |
| 1668 | while (Tok.is(tok::comma)) { |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1669 | SourceLocation CommaLoc = ConsumeToken(); |
| 1670 | |
| 1671 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1672 | // This comma was followed by a line-break and something which can't be |
| 1673 | // the start of a declarator. The comma was probably a typo for a |
| 1674 | // semicolon. |
| 1675 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1676 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1677 | ExpectSemi = false; |
| 1678 | break; |
| 1679 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1680 | |
| 1681 | // Parse the next declarator. |
| 1682 | D.clear(); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1683 | D.setCommaLoc(CommaLoc); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1684 | |
| 1685 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1686 | // declaration, these would be part of the declspec. In subsequent |
| 1687 | // declarators, they become part of the declarator itself, so that they |
| 1688 | // don't apply to declarators after *this* one. Examples: |
| 1689 | // short __attribute__((common)) var; -> declspec |
| 1690 | // short var __attribute__((common)); -> declarator |
| 1691 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1692 | MaybeParseGNUAttributes(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1693 | |
| 1694 | ParseDeclarator(D); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1695 | if (!D.isInvalidType()) { |
| 1696 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1697 | D.complete(ThisDecl); |
| 1698 | if (ThisDecl) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1699 | DeclsInGroup.push_back(ThisDecl); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1700 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | if (DeclEnd) |
| 1704 | *DeclEnd = Tok.getLocation(); |
| 1705 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1706 | if (ExpectSemi && |
Chris Lattner | 8bb21d3 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 1707 | ExpectAndConsumeSemi(Context == Declarator::FileContext |
| 1708 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1709 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1710 | // Okay, there was no semicolon and one was expected. If we see a |
| 1711 | // declaration specifier, just assume it was missing and continue parsing. |
| 1712 | // Otherwise things are very confused and we skip to recover. |
| 1713 | if (!isDeclarationSpecifier()) { |
| 1714 | SkipUntil(tok::r_brace, true, true); |
| 1715 | if (Tok.is(tok::semi)) |
| 1716 | ConsumeToken(); |
| 1717 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1720 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1723 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1724 | /// declarator. Returns true on an error. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1725 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1726 | // If a simple-asm-expr is present, parse it. |
| 1727 | if (Tok.is(tok::kw_asm)) { |
| 1728 | SourceLocation Loc; |
| 1729 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1730 | if (AsmLabel.isInvalid()) { |
| 1731 | SkipUntil(tok::semi, true, true); |
| 1732 | return true; |
| 1733 | } |
| 1734 | |
| 1735 | D.setAsmLabel(AsmLabel.release()); |
| 1736 | D.SetRangeEnd(Loc); |
| 1737 | } |
| 1738 | |
| 1739 | MaybeParseGNUAttributes(D); |
| 1740 | return false; |
| 1741 | } |
| 1742 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1743 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1744 | /// declarator'. This method parses the remainder of the declaration |
| 1745 | /// (including any attributes or initializer, among other things) and |
| 1746 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1747 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1748 | /// init-declarator: [C99 6.7] |
| 1749 | /// declarator |
| 1750 | /// declarator '=' initializer |
| 1751 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1752 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1753 | /// [C++] declarator initializer[opt] |
| 1754 | /// |
| 1755 | /// [C++] initializer: |
| 1756 | /// [C++] '=' initializer-clause |
| 1757 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1758 | /// [C++0x] '=' 'default' [TODO] |
| 1759 | /// [C++0x] '=' 'delete' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1760 | /// [C++0x] braced-init-list |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1761 | /// |
| 1762 | /// According to the standard grammar, =default and =delete are function |
| 1763 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1764 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1765 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1766 | const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1767 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1768 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1769 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1770 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1771 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1772 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1773 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1774 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1775 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1776 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1777 | switch (TemplateInfo.Kind) { |
| 1778 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1779 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1780 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1781 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1782 | case ParsedTemplateInfo::Template: |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1783 | case ParsedTemplateInfo::ExplicitSpecialization: { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1784 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1785 | *TemplateInfo.TemplateParams, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1786 | D); |
Larisse Voufo | 2521813 | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 1787 | if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl)) |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1788 | // Re-direct this decl to refer to the templated decl so that we can |
| 1789 | // initialize it. |
| 1790 | ThisDecl = VT->getTemplatedDecl(); |
| 1791 | break; |
| 1792 | } |
| 1793 | case ParsedTemplateInfo::ExplicitInstantiation: { |
| 1794 | if (Tok.is(tok::semi)) { |
| 1795 | DeclResult ThisRes = Actions.ActOnExplicitInstantiation( |
| 1796 | getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D); |
| 1797 | if (ThisRes.isInvalid()) { |
| 1798 | SkipUntil(tok::semi, true, true); |
| 1799 | return 0; |
| 1800 | } |
| 1801 | ThisDecl = ThisRes.get(); |
| 1802 | } else { |
| 1803 | // FIXME: This check should be for a variable template instantiation only. |
| 1804 | |
| 1805 | // Check that this is a valid instantiation |
| 1806 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 1807 | // If the declarator-id is not a template-id, issue a diagnostic and |
| 1808 | // recover by ignoring the 'template' keyword. |
| 1809 | Diag(Tok, diag::err_template_defn_explicit_instantiation) |
| 1810 | << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc); |
| 1811 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1812 | } else { |
| 1813 | SourceLocation LAngleLoc = |
| 1814 | PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
| 1815 | Diag(D.getIdentifierLoc(), |
| 1816 | diag::err_explicit_instantiation_with_definition) |
| 1817 | << SourceRange(TemplateInfo.TemplateLoc) |
| 1818 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
| 1819 | |
| 1820 | // Recover as if it were an explicit specialization. |
| 1821 | TemplateParameterLists FakedParamLists; |
| 1822 | FakedParamLists.push_back(Actions.ActOnTemplateParameterList( |
| 1823 | 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0, |
| 1824 | LAngleLoc)); |
| 1825 | |
| 1826 | ThisDecl = |
| 1827 | Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D); |
| 1828 | } |
| 1829 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1830 | break; |
| 1831 | } |
| 1832 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1833 | |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1834 | bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType(); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1835 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1836 | // Parse declarator '=' initializer. |
Richard Trieu | d6c7c67 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1837 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | fcaf27e | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1838 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1839 | ConsumeToken(); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1840 | |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1841 | if (Tok.is(tok::kw_delete)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1842 | if (D.isFunctionDeclarator()) |
| 1843 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1844 | << 1 /* delete */; |
| 1845 | else |
| 1846 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Sean Hunt | fe2695e | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1847 | } else if (Tok.is(tok::kw_default)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1848 | if (D.isFunctionDeclarator()) |
Sebastian Redl | ecfcd56 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1849 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1850 | << 0 /* default */; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1851 | else |
| 1852 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1853 | } else { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1854 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1855 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1856 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1857 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1858 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1859 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1860 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Peter Collingbourne | ec98f2f | 2012-07-27 12:56:09 +0000 | [diff] [blame] | 1861 | Actions.FinalizeDeclaration(ThisDecl); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1862 | cutOffParsing(); |
| 1863 | return 0; |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1864 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1865 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1866 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1867 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1868 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1869 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1870 | ExitScope(); |
| 1871 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1872 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1873 | if (Init.isInvalid()) { |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1874 | SkipUntil(tok::comma, true, true); |
| 1875 | Actions.ActOnInitializerError(ThisDecl); |
| 1876 | } else |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1877 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1878 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1879 | } |
| 1880 | } else if (Tok.is(tok::l_paren)) { |
| 1881 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1882 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1883 | T.consumeOpen(); |
| 1884 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1885 | ExprVector Exprs; |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1886 | CommaLocsTy CommaLocs; |
| 1887 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1888 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1889 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1890 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1893 | if (ParseExpressionList(Exprs, CommaLocs)) { |
David Blaikie | 3ea19c8 | 2012-10-10 23:15:05 +0000 | [diff] [blame] | 1894 | Actions.ActOnInitializerError(ThisDecl); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1895 | SkipUntil(tok::r_paren); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1896 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1897 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1898 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1899 | ExitScope(); |
| 1900 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1901 | } else { |
| 1902 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1903 | T.consumeClose(); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1904 | |
| 1905 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1906 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1907 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1908 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1909 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1910 | ExitScope(); |
| 1911 | } |
| 1912 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1913 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 1914 | T.getCloseLocation(), |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1915 | Exprs); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1916 | Actions.AddInitializerToDecl(ThisDecl, Initializer.take(), |
| 1917 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1918 | } |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1919 | } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) && |
Fariborz Jahanian | b0ed95c | 2012-07-03 23:22:13 +0000 | [diff] [blame] | 1920 | (!CurParsedObjCImpl || !D.isFunctionDeclarator())) { |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1921 | // Parse C++0x braced-init-list. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1922 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1923 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1924 | if (D.getCXXScopeSpec().isSet()) { |
| 1925 | EnterScope(0); |
| 1926 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1927 | } |
| 1928 | |
| 1929 | ExprResult Init(ParseBraceInitializer()); |
| 1930 | |
| 1931 | if (D.getCXXScopeSpec().isSet()) { |
| 1932 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1933 | ExitScope(); |
| 1934 | } |
| 1935 | |
| 1936 | if (Init.isInvalid()) { |
| 1937 | Actions.ActOnInitializerError(ThisDecl); |
| 1938 | } else |
| 1939 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1940 | /*DirectInit=*/true, TypeContainsAuto); |
| 1941 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1942 | } else { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1943 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1944 | } |
| 1945 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1946 | Actions.FinalizeDeclaration(ThisDecl); |
| 1947 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1948 | return ThisDecl; |
| 1949 | } |
| 1950 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1951 | /// ParseSpecifierQualifierList |
| 1952 | /// specifier-qualifier-list: |
| 1953 | /// type-specifier specifier-qualifier-list[opt] |
| 1954 | /// type-qualifier specifier-qualifier-list[opt] |
| 1955 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 1956 | /// |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1957 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 1958 | DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1959 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1960 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1961 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1962 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1963 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1964 | // Validate declspec for type-name. |
| 1965 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 1966 | if ((DSC == DSC_type_specifier || DSC == DSC_trailing) && |
| 1967 | !DS.hasTypeSpecifier()) { |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1968 | Diag(Tok, diag::err_expected_type); |
| 1969 | DS.SetTypeSpecError(); |
| 1970 | } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 1971 | !DS.hasAttributes()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1972 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1973 | if (!DS.hasTypeSpecifier()) |
| 1974 | DS.SetTypeSpecError(); |
| 1975 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1976 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1977 | // Issue diagnostic and remove storage class if present. |
| 1978 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 1979 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1980 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 1981 | else |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 1982 | Diag(DS.getThreadStorageClassSpecLoc(), |
| 1983 | diag::err_typename_invalid_storageclass); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1984 | DS.ClearStorageClassSpecs(); |
| 1985 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1986 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1987 | // Issue diagnostic and remove function specfier if present. |
| 1988 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1989 | if (DS.isInlineSpecified()) |
| 1990 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1991 | if (DS.isVirtualSpecified()) |
| 1992 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1993 | if (DS.isExplicitSpecified()) |
| 1994 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1995 | DS.ClearFunctionSpecs(); |
| 1996 | } |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1997 | |
| 1998 | // Issue diagnostic and remove constexpr specfier if present. |
| 1999 | if (DS.isConstexprSpecified()) { |
| 2000 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 2001 | DS.ClearConstexprSpec(); |
| 2002 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2005 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 2006 | /// specified token is valid after the identifier in a declarator which |
| 2007 | /// immediately follows the declspec. For example, these things are valid: |
| 2008 | /// |
| 2009 | /// int x [ 4]; // direct-declarator |
| 2010 | /// int x ( int y); // direct-declarator |
| 2011 | /// int(int x ) // direct-declarator |
| 2012 | /// int x ; // simple-declaration |
| 2013 | /// int x = 17; // init-declarator-list |
| 2014 | /// int x , y; // init-declarator-list |
| 2015 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2016 | /// int x : 4; // struct-declarator |
Chris Lattner | c83c27a | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 2017 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2018 | /// |
| 2019 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 2020 | /// ')' happens to be valid anyway). |
| 2021 | /// int (x) |
| 2022 | /// |
| 2023 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 2024 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 2025 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2026 | 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] | 2027 | } |
| 2028 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2029 | |
| 2030 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 2031 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 2032 | /// the declspec has no type specifier. In this case, the declspec is either |
| 2033 | /// malformed or is "implicit int" (in K&R and C89). |
| 2034 | /// |
| 2035 | /// This method handles diagnosing this prettily and returns false if the |
| 2036 | /// declspec is done being processed. If it recovers and thinks there may be |
| 2037 | /// other pieces of declspec after it, it returns true. |
| 2038 | /// |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2039 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2040 | const ParsedTemplateInfo &TemplateInfo, |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2041 | AccessSpecifier AS, DeclSpecContext DSC, |
| 2042 | ParsedAttributesWithRange &Attrs) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2043 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2044 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2045 | SourceLocation Loc = Tok.getLocation(); |
| 2046 | // If we see an identifier that is not a type name, we normally would |
| 2047 | // parse it as the identifer being declared. However, when a typename |
| 2048 | // is typo'd or the definition is not included, this will incorrectly |
| 2049 | // parse the typename as the identifier name and fall over misparsing |
| 2050 | // later parts of the diagnostic. |
| 2051 | // |
| 2052 | // As such, we try to do some look-ahead in cases where this would |
| 2053 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 2054 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 2055 | // an identifier with implicit int, we'd get a parse error because the |
| 2056 | // next token is obviously invalid for a type. Parse these as a case |
| 2057 | // with an invalid type specifier. |
| 2058 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2059 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2060 | // 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] | 2061 | // error, do lookahead to try to do better recovery. This never applies |
| 2062 | // within a type specifier. Outside of C++, we allow this even if the |
| 2063 | // language doesn't "officially" support implicit int -- we support |
Richard Smith | 58eb370 | 2013-04-30 22:43:51 +0000 | [diff] [blame] | 2064 | // implicit int as an extension in C99 and C11. |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 2065 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
Richard Smith | 58eb370 | 2013-04-30 22:43:51 +0000 | [diff] [blame] | 2066 | !getLangOpts().CPlusPlus && |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2067 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2068 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 2069 | // we just avoid eating the identifier, so it will be parsed as the |
| 2070 | // identifier in the declarator. |
| 2071 | return false; |
| 2072 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2073 | |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2074 | if (getLangOpts().CPlusPlus && |
| 2075 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 2076 | // Don't require a type specifier if we have the 'auto' storage class |
| 2077 | // specifier in C++98 -- we'll promote it to a type specifier. |
Richard Smith | b79b17b | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2078 | if (SS) |
| 2079 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2080 | return false; |
| 2081 | } |
| 2082 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2083 | // Otherwise, if we don't consume this token, we are going to emit an |
| 2084 | // error anyway. Try to recover from various common problems. Check |
| 2085 | // to see if this was a reference to a tag name without a tag specified. |
| 2086 | // 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] | 2087 | // |
| 2088 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 2089 | if (SS == 0) { |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2090 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2091 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2092 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2093 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2094 | default: break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2095 | case DeclSpec::TST_enum: |
| 2096 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 2097 | case DeclSpec::TST_union: |
| 2098 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 2099 | case DeclSpec::TST_struct: |
| 2100 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2101 | case DeclSpec::TST_interface: |
| 2102 | TagName="__interface"; FixitTagName = "__interface "; |
| 2103 | TagKind=tok::kw___interface;break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2104 | case DeclSpec::TST_class: |
| 2105 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2106 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2107 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2108 | if (TagName) { |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2109 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 2110 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 2111 | Sema::LookupOrdinaryName); |
| 2112 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2113 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2114 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 2115 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 2116 | |
| 2117 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 2118 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 2119 | I != IEnd; ++I) |
Kaelyn Uhrain | 392b3f5 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 2120 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2121 | << TokenName << TagName; |
| 2122 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2123 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2124 | // Parse this as a tag as if the missing tag were present. |
| 2125 | if (TagKind == tok::kw_enum) |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2126 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2127 | else |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2128 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2129 | /*EnteringContext*/ false, DSC_normal, Attrs); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2130 | return true; |
| 2131 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2132 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2133 | |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2134 | // Determine whether this identifier could plausibly be the name of something |
Richard Smith | 7514db2 | 2012-05-15 21:42:17 +0000 | [diff] [blame] | 2135 | // being declared (with a missing type). |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2136 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
| 2137 | (!SS || DSC == DSC_top_level || DSC == DSC_class)) { |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2138 | // Look ahead to the next token to try to figure out what this declaration |
| 2139 | // was supposed to be. |
| 2140 | switch (NextToken().getKind()) { |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2141 | case tok::l_paren: { |
| 2142 | // static x(4); // 'x' is not a type |
| 2143 | // x(int n); // 'x' is not a type |
| 2144 | // x (*p)[]; // 'x' is a type |
| 2145 | // |
| 2146 | // Since we're in an error case (or the rare 'implicit int in C++' MS |
| 2147 | // extension), we can afford to perform a tentative parse to determine |
| 2148 | // which case we're in. |
| 2149 | TentativeParsingAction PA(*this); |
| 2150 | ConsumeToken(); |
| 2151 | TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false); |
| 2152 | PA.Revert(); |
Richard Smith | b79b17b | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2153 | |
| 2154 | if (TPR != TPResult::False()) { |
| 2155 | // The identifier is followed by a parenthesized declarator. |
| 2156 | // It's supposed to be a type. |
| 2157 | break; |
| 2158 | } |
| 2159 | |
| 2160 | // If we're in a context where we could be declaring a constructor, |
| 2161 | // check whether this is a constructor declaration with a bogus name. |
| 2162 | if (DSC == DSC_class || (DSC == DSC_top_level && SS)) { |
| 2163 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2164 | if (Actions.isCurrentClassNameTypo(II, SS)) { |
| 2165 | Diag(Loc, diag::err_constructor_bad_name) |
| 2166 | << Tok.getIdentifierInfo() << II |
| 2167 | << FixItHint::CreateReplacement(Tok.getLocation(), II->getName()); |
| 2168 | Tok.setIdentifierInfo(II); |
| 2169 | } |
| 2170 | } |
| 2171 | // Fall through. |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2172 | } |
Richard Smith | b79b17b | 2013-10-15 00:00:26 +0000 | [diff] [blame] | 2173 | case tok::comma: |
| 2174 | case tok::equal: |
| 2175 | case tok::kw_asm: |
| 2176 | case tok::l_brace: |
| 2177 | case tok::l_square: |
| 2178 | case tok::semi: |
| 2179 | // This looks like a variable or function declaration. The type is |
| 2180 | // probably missing. We're done parsing decl-specifiers. |
| 2181 | if (SS) |
| 2182 | AnnotateScopeToken(*SS, /*IsNewAnnotation*/false); |
| 2183 | return false; |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2184 | |
| 2185 | default: |
| 2186 | // This is probably supposed to be a type. This includes cases like: |
| 2187 | // int f(itn); |
| 2188 | // struct S { unsinged : 4; }; |
| 2189 | break; |
| 2190 | } |
| 2191 | } |
| 2192 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2193 | // 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] | 2194 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2195 | ParsedType T; |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2196 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2197 | if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2198 | // The action emitted a diagnostic, so we don't have to. |
| 2199 | if (T) { |
| 2200 | // The action has suggested that the type T could be used. Set that as |
| 2201 | // the type in the declaration specifiers, consume the would-be type |
| 2202 | // name token, and we're done. |
| 2203 | const char *PrevSpec; |
| 2204 | unsigned DiagID; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2205 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2206 | DS.SetRangeEnd(Tok.getLocation()); |
| 2207 | ConsumeToken(); |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2208 | // There may be other declaration specifiers after this. |
| 2209 | return true; |
| 2210 | } else if (II != Tok.getIdentifierInfo()) { |
| 2211 | // If no type was suggested, the correction is to a keyword |
| 2212 | Tok.setKind(II->getTokenID()); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2213 | // There may be other declaration specifiers after this. |
| 2214 | return true; |
| 2215 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2216 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2217 | // Fall through; the action had no suggestion for us. |
| 2218 | } else { |
| 2219 | // The action did not emit a diagnostic, so emit one now. |
| 2220 | SourceRange R; |
| 2221 | if (SS) R = SS->getRange(); |
| 2222 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 2223 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2224 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2225 | // Mark this as an error. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2226 | DS.SetTypeSpecError(); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2227 | DS.SetRangeEnd(Tok.getLocation()); |
| 2228 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2229 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2230 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 2231 | // avoid rippling error messages on subsequent uses of the same type, |
| 2232 | // could be useful if #include was forgotten. |
| 2233 | return false; |
| 2234 | } |
| 2235 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2236 | /// \brief Determine the declaration specifier context from the declarator |
| 2237 | /// context. |
| 2238 | /// |
| 2239 | /// \param Context the declarator context, which is one of the |
| 2240 | /// Declarator::TheContext enumerator values. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2241 | Parser::DeclSpecContext |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2242 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 2243 | if (Context == Declarator::MemberContext) |
| 2244 | return DSC_class; |
| 2245 | if (Context == Declarator::FileContext) |
| 2246 | return DSC_top_level; |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 2247 | if (Context == Declarator::TrailingReturnContext) |
| 2248 | return DSC_trailing; |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2249 | return DSC_normal; |
| 2250 | } |
| 2251 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2252 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 2253 | /// |
| 2254 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 2255 | /// type. Ideally, the type should be propagated directly into Sema. |
| 2256 | /// |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2257 | /// [C11] type-id |
| 2258 | /// [C11] constant-expression |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2259 | /// [C++0x] type-id ...[opt] |
| 2260 | /// [C++0x] assignment-expression ...[opt] |
| 2261 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 2262 | SourceLocation &EllipsisLoc) { |
| 2263 | ExprResult ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2264 | if (isTypeIdInParens()) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2265 | SourceLocation TypeLoc = Tok.getLocation(); |
| 2266 | ParsedType Ty = ParseTypeName().get(); |
| 2267 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2268 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 2269 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2270 | } else |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2271 | ER = ParseConstantExpression(); |
| 2272 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2273 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis)) |
Peter Collingbourne | fe9b2a8 | 2011-10-24 17:56:00 +0000 | [diff] [blame] | 2274 | EllipsisLoc = ConsumeToken(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2275 | |
| 2276 | return ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 2280 | /// attribute to Attrs. |
| 2281 | /// |
| 2282 | /// alignment-specifier: |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2283 | /// [C11] '_Alignas' '(' type-id ')' |
| 2284 | /// [C11] '_Alignas' '(' constant-expression ')' |
Richard Smith | 33f04a2 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2285 | /// [C++11] 'alignas' '(' type-id ...[opt] ')' |
| 2286 | /// [C++11] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2287 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2288 | SourceLocation *EndLoc) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2289 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 2290 | "Not an alignment-specifier!"); |
| 2291 | |
Richard Smith | 33f04a2 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2292 | IdentifierInfo *KWName = Tok.getIdentifierInfo(); |
| 2293 | SourceLocation KWLoc = ConsumeToken(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2294 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2295 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2296 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2297 | return; |
| 2298 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2299 | SourceLocation EllipsisLoc; |
| 2300 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2301 | if (ArgExpr.isInvalid()) { |
| 2302 | SkipUntil(tok::r_paren); |
| 2303 | return; |
| 2304 | } |
| 2305 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2306 | T.consumeClose(); |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2307 | if (EndLoc) |
| 2308 | *EndLoc = T.getCloseLocation(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2309 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2310 | ArgsVector ArgExprs; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2311 | ArgExprs.push_back(ArgExpr.release()); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2312 | Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1, |
| 2313 | AttributeList::AS_Keyword, EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2314 | } |
| 2315 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2316 | /// ParseDeclarationSpecifiers |
| 2317 | /// declaration-specifiers: [C99 6.7] |
| 2318 | /// storage-class-specifier declaration-specifiers[opt] |
| 2319 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2320 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2321 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2322 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2323 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2324 | /// |
| 2325 | /// storage-class-specifier: [C99 6.7.1] |
| 2326 | /// 'typedef' |
| 2327 | /// 'extern' |
| 2328 | /// 'static' |
| 2329 | /// 'auto' |
| 2330 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2331 | /// [C++] 'mutable' |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2332 | /// [C++11] 'thread_local' |
| 2333 | /// [C11] '_Thread_local' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2334 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2335 | /// function-specifier: [C99 6.7.4] |
| 2336 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2337 | /// [C++] 'virtual' |
| 2338 | /// [C++] 'explicit' |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2339 | /// [OpenCL] '__kernel' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2340 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2341 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2342 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2343 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 2344 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2345 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2346 | AccessSpecifier AS, |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2347 | DeclSpecContext DSContext, |
| 2348 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2349 | if (DS.getSourceRange().isInvalid()) { |
| 2350 | DS.SetRangeStart(Tok.getLocation()); |
| 2351 | DS.SetRangeEnd(Tok.getLocation()); |
| 2352 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2353 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2354 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2355 | bool AttrsLastTime = false; |
| 2356 | ParsedAttributesWithRange attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2357 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2358 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2359 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2360 | unsigned DiagID = 0; |
| 2361 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2362 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2363 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2364 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2365 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2366 | DoneWithDeclSpec: |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2367 | if (!AttrsLastTime) |
| 2368 | ProhibitAttributes(attrs); |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2369 | else { |
| 2370 | // Reject C++11 attributes that appertain to decl specifiers as |
| 2371 | // we don't support any C++11 attributes that appertain to decl |
| 2372 | // specifiers. This also conforms to what g++ 4.8 is doing. |
| 2373 | ProhibitCXX11Attributes(attrs); |
| 2374 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2375 | DS.takeAttributesFrom(attrs); |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2376 | } |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 2377 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2378 | // If this is not a declaration specifier token, we're done reading decl |
| 2379 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2380 | DS.Finish(Diags, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2381 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2382 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2383 | case tok::l_square: |
| 2384 | case tok::kw_alignas: |
Richard Smith | 672edb0 | 2013-02-22 09:15:49 +0000 | [diff] [blame] | 2385 | if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier()) |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2386 | goto DoneWithDeclSpec; |
| 2387 | |
| 2388 | ProhibitAttributes(attrs); |
| 2389 | // FIXME: It would be good to recover by accepting the attributes, |
| 2390 | // but attempting to do that now would cause serious |
| 2391 | // madness in terms of diagnostics. |
| 2392 | attrs.clear(); |
| 2393 | attrs.Range = SourceRange(); |
| 2394 | |
| 2395 | ParseCXX11Attributes(attrs); |
| 2396 | AttrsLastTime = true; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2397 | continue; |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2398 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2399 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2400 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2401 | if (DS.hasTypeSpecifier()) { |
| 2402 | bool AllowNonIdentifiers |
| 2403 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 2404 | Scope::BlockScope | |
| 2405 | Scope::TemplateParamScope | |
| 2406 | Scope::FunctionPrototypeScope | |
| 2407 | Scope::AtCatchScope)) == 0; |
| 2408 | bool AllowNestedNameSpecifiers |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2409 | = DSContext == DSC_top_level || |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2410 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 2411 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2412 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2413 | AllowNonIdentifiers, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2414 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2415 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2418 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 2419 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 2420 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2421 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2422 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2423 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2424 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 2425 | else if (CurParsedObjCImpl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2426 | CCC = Sema::PCC_ObjCImplementation; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2427 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2428 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2429 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2432 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2433 | // C++ scope specifier. Annotate and loop, or bail out on error. |
Eli Friedman | 5a42820 | 2013-08-15 23:59:20 +0000 | [diff] [blame] | 2434 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2435 | if (!DS.hasTypeSpecifier()) |
| 2436 | DS.SetTypeSpecError(); |
| 2437 | goto DoneWithDeclSpec; |
| 2438 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 2439 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 2440 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2441 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2442 | |
| 2443 | case tok::annot_cxxscope: { |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2444 | if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector()) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2445 | goto DoneWithDeclSpec; |
| 2446 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2447 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 2448 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 2449 | Tok.getAnnotationRange(), |
| 2450 | SS); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2451 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2452 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2453 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2454 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2455 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2456 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2457 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2458 | |
| 2459 | // C++ [class.qual]p2: |
| 2460 | // In a lookup in which the constructor is an acceptable lookup |
| 2461 | // result and the nested-name-specifier nominates a class C: |
| 2462 | // |
| 2463 | // - if the name specified after the |
| 2464 | // nested-name-specifier, when looked up in C, is the |
| 2465 | // injected-class-name of C (Clause 9), or |
| 2466 | // |
| 2467 | // - if the name specified after the nested-name-specifier |
| 2468 | // is the same as the identifier or the |
| 2469 | // simple-template-id's template-name in the last |
| 2470 | // component of the nested-name-specifier, |
| 2471 | // |
| 2472 | // the name is instead considered to name the constructor of |
| 2473 | // class C. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2474 | // |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2475 | // Thus, if the template-name is actually the constructor |
| 2476 | // name, then the code is ill-formed; this interpretation is |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2477 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2478 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2479 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2480 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2481 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2482 | if (isConstructorDeclarator()) { |
| 2483 | // The user meant this to be an out-of-line constructor |
| 2484 | // definition, but template arguments are not allowed |
| 2485 | // there. Just allow this as a constructor; we'll |
| 2486 | // complain about it later. |
| 2487 | goto DoneWithDeclSpec; |
| 2488 | } |
| 2489 | |
| 2490 | // The user meant this to name a type, but it actually names |
| 2491 | // a constructor with some extraneous template |
| 2492 | // arguments. Complain, then parse it as a type as the user |
| 2493 | // intended. |
| 2494 | Diag(TemplateId->TemplateNameLoc, |
| 2495 | diag::err_out_of_line_template_id_names_constructor) |
| 2496 | << TemplateId->Name; |
| 2497 | } |
| 2498 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2499 | DS.getTypeSpecScope() = SS; |
| 2500 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2501 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2502 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2503 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2504 | continue; |
| 2505 | } |
| 2506 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2507 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2508 | DS.getTypeSpecScope() = SS; |
| 2509 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2510 | if (Tok.getAnnotationValue()) { |
| 2511 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2512 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2513 | Tok.getAnnotationEndLoc(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2514 | PrevSpec, DiagID, T); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2515 | if (isInvalid) |
| 2516 | break; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2517 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2518 | else |
| 2519 | DS.SetTypeSpecError(); |
| 2520 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2521 | ConsumeToken(); // The typename |
| 2522 | } |
| 2523 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2524 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2525 | goto DoneWithDeclSpec; |
| 2526 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2527 | // If we're in a context where the identifier could be a class name, |
| 2528 | // check whether this is a constructor declaration. |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2529 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2530 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2531 | &SS)) { |
| 2532 | if (isConstructorDeclarator()) |
| 2533 | goto DoneWithDeclSpec; |
| 2534 | |
| 2535 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 2536 | // of the class is qualified in a context where it could name |
| 2537 | // a constructor, its a constructor name. However, we've |
| 2538 | // looked at the declarator, and the user probably meant this |
| 2539 | // to be a type. Complain that it isn't supposed to be treated |
| 2540 | // as a type, then proceed to parse it as a type. |
| 2541 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 2542 | << Next.getIdentifierInfo(); |
| 2543 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2544 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2545 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 2546 | Next.getLocation(), |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2547 | getCurScope(), &SS, |
| 2548 | false, false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2549 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2550 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2551 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2552 | // If the referenced identifier is not a type, then this declspec is |
| 2553 | // erroneous: We already checked about that it has no type specifier, and |
| 2554 | // 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] | 2555 | // typename. |
David Blaikie | 7247c88 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 2556 | if (!TypeRep) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2557 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2558 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2559 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) { |
| 2560 | if (!Attrs.empty()) { |
| 2561 | AttrsLastTime = true; |
| 2562 | attrs.takeAllFrom(Attrs); |
| 2563 | } |
| 2564 | continue; |
| 2565 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2566 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2567 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2568 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2569 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2570 | ConsumeToken(); // The C++ scope. |
| 2571 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2572 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2573 | DiagID, TypeRep); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2574 | if (isInvalid) |
| 2575 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2576 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2577 | DS.SetRangeEnd(Tok.getLocation()); |
| 2578 | ConsumeToken(); // The typename. |
| 2579 | |
| 2580 | continue; |
| 2581 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2582 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2583 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2584 | if (Tok.getAnnotationValue()) { |
| 2585 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | c43271e | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 2586 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2587 | DiagID, T); |
| 2588 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2589 | DS.SetTypeSpecError(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2590 | |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 2591 | if (isInvalid) |
| 2592 | break; |
| 2593 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2594 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2595 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2596 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2597 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2598 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2599 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2600 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2601 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2602 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2603 | continue; |
| 2604 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2605 | |
Douglas Gregor | bfad915 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2606 | case tok::kw___is_signed: |
| 2607 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 2608 | // typically treats it as a trait. If we see __is_signed as it appears |
| 2609 | // in libstdc++, e.g., |
| 2610 | // |
| 2611 | // static const bool __is_signed; |
| 2612 | // |
| 2613 | // then treat __is_signed as an identifier rather than as a keyword. |
| 2614 | if (DS.getTypeSpecType() == TST_bool && |
| 2615 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
| 2616 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 2617 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
| 2618 | Tok.setKind(tok::identifier); |
| 2619 | } |
| 2620 | |
| 2621 | // We're done with the declaration-specifiers. |
| 2622 | goto DoneWithDeclSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2623 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2624 | // typedef-name |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2625 | case tok::kw_decltype: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2626 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2627 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 2628 | // so handle it as such. This is important for ctor parsing. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2629 | if (getLangOpts().CPlusPlus) { |
Eli Friedman | 5a42820 | 2013-08-15 23:59:20 +0000 | [diff] [blame] | 2630 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2631 | if (!DS.hasTypeSpecifier()) |
| 2632 | DS.SetTypeSpecError(); |
| 2633 | goto DoneWithDeclSpec; |
| 2634 | } |
| 2635 | if (!Tok.is(tok::identifier)) |
| 2636 | continue; |
| 2637 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2638 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2639 | // This identifier can only be a typedef name if we haven't already seen |
| 2640 | // a type-specifier. Without this check we misparse: |
| 2641 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 2642 | if (DS.hasTypeSpecifier()) |
| 2643 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2644 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2645 | // Check for need to substitute AltiVec keyword tokens. |
| 2646 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2647 | break; |
| 2648 | |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2649 | // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not |
| 2650 | // allow the use of a typedef name as a type specifier. |
| 2651 | if (DS.isTypeAltiVecVector()) |
| 2652 | goto DoneWithDeclSpec; |
| 2653 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2654 | ParsedType TypeRep = |
| 2655 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 2656 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2657 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2658 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 2659 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2660 | if (!TypeRep) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2661 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2662 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) { |
| 2663 | if (!Attrs.empty()) { |
| 2664 | AttrsLastTime = true; |
| 2665 | attrs.takeAllFrom(Attrs); |
| 2666 | } |
| 2667 | continue; |
| 2668 | } |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2669 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2670 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2671 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2672 | // If we're in a context where the identifier could be a class name, |
| 2673 | // check whether this is a constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2674 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2675 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2676 | isConstructorDeclarator()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2677 | goto DoneWithDeclSpec; |
| 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); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2681 | if (isInvalid) |
| 2682 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2683 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2684 | DS.SetRangeEnd(Tok.getLocation()); |
| 2685 | ConsumeToken(); // The identifier |
| 2686 | |
| 2687 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2688 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2689 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2690 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2691 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2692 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2693 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2694 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2695 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2696 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2697 | |
| 2698 | // type-name |
| 2699 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2700 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2701 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2702 | // This template-id does not refer to a type name, so we're |
| 2703 | // done with the type-specifiers. |
| 2704 | goto DoneWithDeclSpec; |
| 2705 | } |
| 2706 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2707 | // If we're in a context where the template-id could be a |
| 2708 | // constructor name or specialization, check whether this is a |
| 2709 | // constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2710 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2711 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2712 | isConstructorDeclarator()) |
| 2713 | goto DoneWithDeclSpec; |
| 2714 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2715 | // Turn the template-id annotation token into a type annotation |
| 2716 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2717 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2718 | continue; |
| 2719 | } |
| 2720 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2721 | // GNU attributes support. |
| 2722 | case tok::kw___attribute: |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2723 | ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2724 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2725 | |
| 2726 | // Microsoft declspec support. |
| 2727 | case tok::kw___declspec: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2728 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2729 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2730 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2731 | // Microsoft single token adornments. |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2732 | case tok::kw___forceinline: { |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2733 | isInvalid = DS.setFunctionSpecInline(Loc); |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2734 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2735 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 2736 | // FIXME: This does not work correctly if it is set to be a declspec |
| 2737 | // attribute, and a GNU attribute is simply incorrect. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2738 | DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 2739 | AttributeList::AS_GNU); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2740 | break; |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2741 | } |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2742 | |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 2743 | case tok::kw___sptr: |
| 2744 | case tok::kw___uptr: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2745 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2746 | case tok::kw___ptr32: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2747 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2748 | case tok::kw___cdecl: |
| 2749 | case tok::kw___stdcall: |
| 2750 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2751 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2752 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2753 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2754 | continue; |
| 2755 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2756 | // Borland single token adornments. |
| 2757 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2758 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2759 | continue; |
| 2760 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2761 | // OpenCL single token adornments. |
| 2762 | case tok::kw___kernel: |
| 2763 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2764 | continue; |
| 2765 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2766 | // storage-class-specifier |
| 2767 | case tok::kw_typedef: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2768 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
| 2769 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2770 | break; |
| 2771 | case tok::kw_extern: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2772 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2773 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2774 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
| 2775 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2776 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2777 | case tok::kw___private_extern__: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2778 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
| 2779 | Loc, PrevSpec, DiagID); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2780 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2781 | case tok::kw_static: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2782 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2783 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2784 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
| 2785 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2786 | break; |
| 2787 | case tok::kw_auto: |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2788 | if (getLangOpts().CPlusPlus11) { |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2789 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2790 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2791 | PrevSpec, DiagID); |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2792 | if (!isInvalid) |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2793 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2794 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2795 | } else |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2796 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 2797 | DiagID); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2798 | } else |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2799 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2800 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2801 | break; |
| 2802 | case tok::kw_register: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2803 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
| 2804 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2805 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2806 | case tok::kw_mutable: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2807 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
| 2808 | PrevSpec, DiagID); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2809 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2810 | case tok::kw___thread: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2811 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc, |
| 2812 | PrevSpec, DiagID); |
| 2813 | break; |
| 2814 | case tok::kw_thread_local: |
| 2815 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc, |
| 2816 | PrevSpec, DiagID); |
| 2817 | break; |
| 2818 | case tok::kw__Thread_local: |
| 2819 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local, |
| 2820 | Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2821 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2822 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2823 | // function-specifier |
| 2824 | case tok::kw_inline: |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2825 | isInvalid = DS.setFunctionSpecInline(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2826 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2827 | case tok::kw_virtual: |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2828 | isInvalid = DS.setFunctionSpecVirtual(Loc); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2829 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2830 | case tok::kw_explicit: |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2831 | isInvalid = DS.setFunctionSpecExplicit(Loc); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2832 | break; |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 2833 | case tok::kw__Noreturn: |
| 2834 | if (!getLangOpts().C11) |
| 2835 | Diag(Loc, diag::ext_c11_noreturn); |
| 2836 | isInvalid = DS.setFunctionSpecNoreturn(Loc); |
| 2837 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2838 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2839 | // alignment-specifier |
| 2840 | case tok::kw__Alignas: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2841 | if (!getLangOpts().C11) |
Jordan Rose | f70a886 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2842 | Diag(Tok, diag::ext_c11_alignment) << Tok.getName(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2843 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2844 | continue; |
| 2845 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2846 | // friend |
| 2847 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2848 | if (DSContext == DSC_class) |
| 2849 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 2850 | else { |
| 2851 | PrevSpec = ""; // not actually used by the diagnostic |
| 2852 | DiagID = diag::err_friend_invalid_in_context; |
| 2853 | isInvalid = true; |
| 2854 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2855 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2856 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2857 | // Modules |
| 2858 | case tok::kw___module_private__: |
| 2859 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 2860 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2861 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2862 | // constexpr |
| 2863 | case tok::kw_constexpr: |
| 2864 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 2865 | break; |
| 2866 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2867 | // type-specifier |
| 2868 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2869 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 2870 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2871 | break; |
| 2872 | case tok::kw_long: |
| 2873 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2874 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2875 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2876 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2877 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2878 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2879 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2880 | case tok::kw___int64: |
| 2881 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2882 | DiagID); |
| 2883 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2884 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2885 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 2886 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2887 | break; |
| 2888 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2889 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2890 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2891 | break; |
| 2892 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2893 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2894 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2895 | break; |
| 2896 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2897 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2898 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2899 | break; |
| 2900 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2901 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 2902 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2903 | break; |
| 2904 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2905 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 2906 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2907 | break; |
| 2908 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2909 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 2910 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2911 | break; |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 2912 | case tok::kw___int128: |
| 2913 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
| 2914 | DiagID); |
| 2915 | break; |
| 2916 | case tok::kw_half: |
| 2917 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
| 2918 | DiagID); |
| 2919 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2920 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2921 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 2922 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2923 | break; |
| 2924 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2925 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 2926 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2927 | break; |
| 2928 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2929 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 2930 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2931 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2932 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2933 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 2934 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2935 | break; |
| 2936 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2937 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 2938 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2939 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2940 | case tok::kw_bool: |
| 2941 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2942 | if (Tok.is(tok::kw_bool) && |
| 2943 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 2944 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 2945 | PrevSpec = ""; // Not used by the diagnostic. |
| 2946 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2947 | // For better error recovery. |
| 2948 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2949 | isInvalid = true; |
| 2950 | } else { |
| 2951 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 2952 | DiagID); |
| 2953 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2954 | break; |
| 2955 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2956 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2957 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2958 | break; |
| 2959 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2960 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2961 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2962 | break; |
| 2963 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2964 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2965 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2966 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2967 | case tok::kw___vector: |
| 2968 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2969 | break; |
| 2970 | case tok::kw___pixel: |
| 2971 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2972 | break; |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 2973 | case tok::kw_image1d_t: |
| 2974 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc, |
| 2975 | PrevSpec, DiagID); |
| 2976 | break; |
| 2977 | case tok::kw_image1d_array_t: |
| 2978 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc, |
| 2979 | PrevSpec, DiagID); |
| 2980 | break; |
| 2981 | case tok::kw_image1d_buffer_t: |
| 2982 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc, |
| 2983 | PrevSpec, DiagID); |
| 2984 | break; |
| 2985 | case tok::kw_image2d_t: |
| 2986 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc, |
| 2987 | PrevSpec, DiagID); |
| 2988 | break; |
| 2989 | case tok::kw_image2d_array_t: |
| 2990 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc, |
| 2991 | PrevSpec, DiagID); |
| 2992 | break; |
| 2993 | case tok::kw_image3d_t: |
| 2994 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc, |
| 2995 | PrevSpec, DiagID); |
| 2996 | break; |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2997 | case tok::kw_sampler_t: |
| 2998 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc, |
| 2999 | PrevSpec, DiagID); |
| 3000 | break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3001 | case tok::kw_event_t: |
| 3002 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc, |
| 3003 | PrevSpec, DiagID); |
| 3004 | break; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3005 | case tok::kw___unknown_anytype: |
| 3006 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 3007 | PrevSpec, DiagID); |
| 3008 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3009 | |
| 3010 | // class-specifier: |
| 3011 | case tok::kw_class: |
| 3012 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3013 | case tok::kw___interface: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3014 | case tok::kw_union: { |
| 3015 | tok::TokenKind Kind = Tok.getKind(); |
| 3016 | ConsumeToken(); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3017 | |
| 3018 | // These are attributes following class specifiers. |
| 3019 | // To produce better diagnostic, we parse them when |
| 3020 | // parsing class specifier. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3021 | ParsedAttributesWithRange Attributes(AttrFactory); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3022 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3023 | EnteringContext, DSContext, Attributes); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3024 | |
| 3025 | // If there are attributes following class specifier, |
| 3026 | // take them over and handle them here. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3027 | if (!Attributes.empty()) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3028 | AttrsLastTime = true; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3029 | attrs.takeAllFrom(Attributes); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3030 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3031 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3032 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3033 | |
| 3034 | // enum-specifier: |
| 3035 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3036 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3037 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3038 | continue; |
| 3039 | |
| 3040 | // cv-qualifier: |
| 3041 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3042 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3043 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3044 | break; |
| 3045 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3046 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3047 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3048 | break; |
| 3049 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3050 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3051 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3052 | break; |
| 3053 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3054 | // C++ typename-specifier: |
| 3055 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3056 | if (TryAnnotateTypeOrScopeToken()) { |
| 3057 | DS.SetTypeSpecError(); |
| 3058 | goto DoneWithDeclSpec; |
| 3059 | } |
| 3060 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3061 | continue; |
| 3062 | break; |
| 3063 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3064 | // GNU typeof support. |
| 3065 | case tok::kw_typeof: |
| 3066 | ParseTypeofSpecifier(DS); |
| 3067 | continue; |
| 3068 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3069 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 3070 | ParseDecltypeSpecifier(DS); |
| 3071 | continue; |
| 3072 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3073 | case tok::kw___underlying_type: |
| 3074 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3075 | continue; |
| 3076 | |
| 3077 | case tok::kw__Atomic: |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3078 | // C11 6.7.2.4/4: |
| 3079 | // If the _Atomic keyword is immediately followed by a left parenthesis, |
| 3080 | // it is interpreted as a type specifier (with a type name), not as a |
| 3081 | // type qualifier. |
| 3082 | if (NextToken().is(tok::l_paren)) { |
| 3083 | ParseAtomicSpecifier(DS); |
| 3084 | continue; |
| 3085 | } |
| 3086 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 3087 | getLangOpts()); |
| 3088 | break; |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3089 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3090 | // OpenCL qualifiers: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3091 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3092 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3093 | goto DoneWithDeclSpec; |
| 3094 | case tok::kw___private: |
| 3095 | case tok::kw___global: |
| 3096 | case tok::kw___local: |
| 3097 | case tok::kw___constant: |
| 3098 | case tok::kw___read_only: |
| 3099 | case tok::kw___write_only: |
| 3100 | case tok::kw___read_write: |
| 3101 | ParseOpenCLQualifiers(DS); |
| 3102 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3103 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 3104 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3105 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3106 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 3107 | // but we support it. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3108 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3109 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3110 | |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 3111 | if (!ParseObjCProtocolQualifiers(DS)) |
| 3112 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 3113 | << FixItHint::CreateInsertion(Loc, "id") |
| 3114 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3115 | |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 3116 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 3117 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 3118 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3119 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3120 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3121 | if (isInvalid) { |
| 3122 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3123 | assert(DiagID); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3124 | |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 3125 | if (DiagID == diag::ext_duplicate_declspec) |
| 3126 | Diag(Tok, DiagID) |
| 3127 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 3128 | else |
| 3129 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3130 | } |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3131 | |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 3132 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3133 | if (DiagID != diag::err_bool_redeclaration) |
| 3134 | ConsumeToken(); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3135 | |
| 3136 | AttrsLastTime = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3137 | } |
| 3138 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 3139 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3140 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 3141 | /// semicolon. |
| 3142 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3143 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3144 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3145 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3146 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3147 | /// struct-declarator-list: |
| 3148 | /// struct-declarator |
| 3149 | /// struct-declarator-list ',' struct-declarator |
| 3150 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 3151 | /// struct-declarator: |
| 3152 | /// declarator |
| 3153 | /// [GNU] declarator attributes[opt] |
| 3154 | /// declarator[opt] ':' constant-expression |
| 3155 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 3156 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3157 | void Parser:: |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3158 | ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3159 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3160 | if (Tok.is(tok::kw___extension__)) { |
| 3161 | // __extension__ silences extension warnings in the subexpression. |
| 3162 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3163 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3164 | return ParseStructDeclaration(DS, Fields); |
| 3165 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3166 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3167 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3168 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3169 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3170 | // If there are no declarators, this is a free-standing declaration |
| 3171 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3172 | if (Tok.is(tok::semi)) { |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3173 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
| 3174 | DS); |
| 3175 | DS.complete(TheDecl); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3176 | return; |
| 3177 | } |
| 3178 | |
| 3179 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3180 | bool FirstDeclarator = true; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3181 | SourceLocation CommaLoc; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3182 | while (1) { |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3183 | ParsingFieldDeclarator DeclaratorInfo(*this, DS); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3184 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3185 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3186 | // Attributes are only allowed here on successive declarators. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3187 | if (!FirstDeclarator) |
| 3188 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3189 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3190 | /// struct-declarator: declarator |
| 3191 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3192 | if (Tok.isNot(tok::colon)) { |
| 3193 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 3194 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3195 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3196 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3197 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3198 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3199 | ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3200 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3201 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3202 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 3203 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 3204 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3205 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3206 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3207 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3208 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3209 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3210 | // We're done with this declarator; invoke the callback. |
Eli Friedman | 817a886 | 2012-08-08 23:35:12 +0000 | [diff] [blame] | 3211 | Fields.invoke(DeclaratorInfo); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3212 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3213 | // If we don't have a comma, it is either the end of the list (a ';') |
| 3214 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3215 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3216 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3217 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3218 | // Consume the comma. |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3219 | CommaLoc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3220 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3221 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3222 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3223 | } |
| 3224 | |
| 3225 | /// ParseStructUnionBody |
| 3226 | /// struct-contents: |
| 3227 | /// struct-declaration-list |
| 3228 | /// [EXT] empty |
| 3229 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 3230 | /// struct-declaration-list: |
| 3231 | /// struct-declaration |
| 3232 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3233 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3234 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3235 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3236 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3237 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 3238 | "parsing struct/union body"); |
Andy Gibbs | f50f3f7 | 2013-04-03 09:31:19 +0000 | [diff] [blame] | 3239 | assert(!getLangOpts().CPlusPlus && "C++ declarations not supported"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3240 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3241 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3242 | if (T.consumeOpen()) |
| 3243 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3244 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3245 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3246 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3247 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3248 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3249 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3250 | // 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] | 3251 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3252 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3253 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3254 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3255 | if (Tok.is(tok::semi)) { |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3256 | ConsumeExtraSemi(InsideStruct, TagType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3257 | continue; |
| 3258 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3259 | |
Andy Gibbs | 74b9fa1 | 2013-04-03 09:46:04 +0000 | [diff] [blame] | 3260 | // Parse _Static_assert declaration. |
| 3261 | if (Tok.is(tok::kw__Static_assert)) { |
| 3262 | SourceLocation DeclEnd; |
| 3263 | ParseStaticAssertDeclaration(DeclEnd); |
| 3264 | continue; |
| 3265 | } |
| 3266 | |
Argyrios Kyrtzidis | bd95745 | 2013-04-18 01:42:35 +0000 | [diff] [blame] | 3267 | if (Tok.is(tok::annot_pragma_pack)) { |
| 3268 | HandlePragmaPack(); |
| 3269 | continue; |
| 3270 | } |
| 3271 | |
| 3272 | if (Tok.is(tok::annot_pragma_align)) { |
| 3273 | HandlePragmaAlign(); |
| 3274 | continue; |
| 3275 | } |
| 3276 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3277 | if (!Tok.is(tok::at)) { |
| 3278 | struct CFieldCallback : FieldCallback { |
| 3279 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3280 | Decl *TagDecl; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3281 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3282 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3283 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3284 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3285 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 3286 | |
Eli Friedman | dcdff46 | 2012-08-08 23:53:27 +0000 | [diff] [blame] | 3287 | void invoke(ParsingFieldDeclarator &FD) { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3288 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3289 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 3290 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 3291 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3292 | FieldDecls.push_back(Field); |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3293 | FD.complete(Field); |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3294 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3295 | } Callback(*this, TagDecl, FieldDecls); |
| 3296 | |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3297 | // Parse all the comma separated declarators. |
| 3298 | ParsingDeclSpec DS(*this); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3299 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3300 | } else { // Handle @defs |
| 3301 | ConsumeToken(); |
| 3302 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 3303 | Diag(Tok, diag::err_unexpected_at); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3304 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3305 | continue; |
| 3306 | } |
| 3307 | ConsumeToken(); |
| 3308 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 3309 | if (!Tok.is(tok::identifier)) { |
| 3310 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3311 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3312 | continue; |
| 3313 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3314 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3315 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3316 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3317 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 3318 | ConsumeToken(); |
| 3319 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3320 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3321 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3322 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3323 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3324 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3325 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3326 | break; |
| 3327 | } else { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3328 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 3329 | // Skip to end of block or statement to avoid ext-warning on extra ';'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3330 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3331 | // If we stopped at a ';', eat it. |
| 3332 | if (Tok.is(tok::semi)) ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3333 | } |
| 3334 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3335 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3336 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3337 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3338 | ParsedAttributes attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3339 | // If attributes exist after struct contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3340 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 3341 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3342 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 3343 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3344 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3345 | attrs.getList()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3346 | StructScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3347 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 3348 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3349 | } |
| 3350 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3351 | /// ParseEnumSpecifier |
| 3352 | /// enum-specifier: [C99 6.7.2.2] |
| 3353 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3354 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3355 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3356 | /// '}' attributes[opt] |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 3357 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3358 | /// '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3359 | /// 'enum' identifier |
| 3360 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3361 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3362 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 3363 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3364 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3365 | /// enum-head: [C++11] |
| 3366 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 3367 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 3368 | /// identifier enum-base[opt] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3369 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3370 | /// enum-key: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3371 | /// 'enum' |
| 3372 | /// 'enum' 'class' |
| 3373 | /// 'enum' 'struct' |
| 3374 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3375 | /// enum-base: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3376 | /// ':' type-specifier-seq |
| 3377 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3378 | /// [C++] elaborated-type-specifier: |
| 3379 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 3380 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3381 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 3382 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3383 | AccessSpecifier AS, DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3384 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3385 | if (Tok.is(tok::code_completion)) { |
| 3386 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3387 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3388 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3389 | } |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3390 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3391 | // If attributes exist after tag, parse them. |
| 3392 | ParsedAttributesWithRange attrs(AttrFactory); |
| 3393 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3394 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3395 | |
| 3396 | // If declspecs exist after tag, parse them. |
| 3397 | while (Tok.is(tok::kw___declspec)) |
| 3398 | ParseMicrosoftDeclSpec(attrs); |
| 3399 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3400 | SourceLocation ScopedEnumKWLoc; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3401 | bool IsScopedUsingClassTag = false; |
| 3402 | |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3403 | // In C++11, recognize 'enum class' and 'enum struct'. |
Richard Trieu | ed5a292 | 2013-04-23 02:47:36 +0000 | [diff] [blame] | 3404 | if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) { |
| 3405 | Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum |
| 3406 | : diag::ext_scoped_enum); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3407 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3408 | ScopedEnumKWLoc = ConsumeToken(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3409 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3410 | // Attributes are not allowed between these keywords. Diagnose, |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3411 | // but then just treat them like they appeared in the right place. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3412 | ProhibitAttributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3413 | |
| 3414 | // They are allowed afterwards, though. |
| 3415 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3416 | MaybeParseCXX11Attributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3417 | while (Tok.is(tok::kw___declspec)) |
| 3418 | ParseMicrosoftDeclSpec(attrs); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3419 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3420 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3421 | // C++11 [temp.explicit]p12: |
| 3422 | // The usual access controls do not apply to names used to specify |
| 3423 | // explicit instantiations. |
| 3424 | // We extend this to also cover explicit specializations. Note that |
| 3425 | // we don't suppress if this turns out to be an elaborated type |
| 3426 | // specifier. |
| 3427 | bool shouldDelayDiagsInTag = |
| 3428 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 3429 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 3430 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3431 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3432 | // Enum definitions should not be parsed in a trailing-return-type. |
| 3433 | bool AllowDeclaration = DSC != DSC_trailing; |
| 3434 | |
| 3435 | bool AllowFixedUnderlyingType = AllowDeclaration && |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3436 | (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt || |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3437 | getLangOpts().ObjC2); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3438 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3439 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3440 | if (getLangOpts().CPlusPlus) { |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3441 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 3442 | // if a fixed underlying type is allowed. |
| 3443 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3444 | |
| 3445 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Richard Smith | 725fe0e | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 3446 | /*EnteringContext=*/true)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3447 | return; |
| 3448 | |
| 3449 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3450 | Diag(Tok, diag::err_expected_ident); |
| 3451 | if (Tok.isNot(tok::l_brace)) { |
| 3452 | // Has no name and is not a definition. |
| 3453 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3454 | SkipUntil(tok::comma, true); |
| 3455 | return; |
| 3456 | } |
| 3457 | } |
| 3458 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3459 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3460 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3461 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3462 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3463 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3464 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3465 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3466 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3467 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3468 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3469 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3470 | // If an identifier is present, consume and remember it. |
| 3471 | IdentifierInfo *Name = 0; |
| 3472 | SourceLocation NameLoc; |
| 3473 | if (Tok.is(tok::identifier)) { |
| 3474 | Name = Tok.getIdentifierInfo(); |
| 3475 | NameLoc = ConsumeToken(); |
| 3476 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3477 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3478 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3479 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 3480 | // declaration of a scoped enumeration. |
| 3481 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3482 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3483 | IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3484 | } |
| 3485 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3486 | // Okay, end the suppression area. We'll decide whether to emit the |
| 3487 | // diagnostics in a second. |
| 3488 | if (shouldDelayDiagsInTag) |
| 3489 | diagsFromTag.done(); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3490 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3491 | TypeResult BaseType; |
| 3492 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3493 | // Parse the fixed underlying type. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3494 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3495 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3496 | bool PossibleBitfield = false; |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3497 | if (CanBeBitfield) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3498 | // If we're in class scope, this can either be an enum declaration with |
| 3499 | // an underlying type, or a declaration of a bitfield member. We try to |
| 3500 | // use a simple disambiguation scheme first to catch the common cases |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3501 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 3502 | // anything that's a simple-type-specifier followed by '(' as an |
| 3503 | // expression. This suffices because function types are not valid |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3504 | // underlying types anyway. |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 3505 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 3506 | Sema::ConstantEvaluated); |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3507 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3508 | // 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] | 3509 | // bit-field. This is the common case. |
| 3510 | if (TPR == TPResult::True()) |
| 3511 | PossibleBitfield = true; |
| 3512 | // If the next token starts a type-specifier-seq, it may be either a |
| 3513 | // 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] | 3514 | // 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] | 3515 | // fixed underlying type. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3516 | else if (TPR == TPResult::False() && |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3517 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 3518 | // Consume the ':'. |
| 3519 | ConsumeToken(); |
| 3520 | } else { |
| 3521 | // We have the start of a type-specifier-seq, so we have to perform |
| 3522 | // tentative parsing to determine whether we have an expression or a |
| 3523 | // type. |
| 3524 | TentativeParsingAction TPA(*this); |
| 3525 | |
| 3526 | // Consume the ':'. |
| 3527 | ConsumeToken(); |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3528 | |
| 3529 | // If we see a type specifier followed by an open-brace, we have an |
| 3530 | // ambiguity between an underlying type and a C++11 braced |
| 3531 | // function-style cast. Resolve this by always treating it as an |
| 3532 | // underlying type. |
| 3533 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 3534 | // this case. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3535 | if ((getLangOpts().CPlusPlus && |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3536 | isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3537 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3538 | // We'll parse this as a bitfield later. |
| 3539 | PossibleBitfield = true; |
| 3540 | TPA.Revert(); |
| 3541 | } else { |
| 3542 | // We have a type-specifier-seq. |
| 3543 | TPA.Commit(); |
| 3544 | } |
| 3545 | } |
| 3546 | } else { |
| 3547 | // Consume the ':'. |
| 3548 | ConsumeToken(); |
| 3549 | } |
| 3550 | |
| 3551 | if (!PossibleBitfield) { |
| 3552 | SourceRange Range; |
| 3553 | BaseType = ParseTypeName(&Range); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3554 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3555 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3556 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Eli Friedman | cef3a7b | 2012-11-02 01:34:28 +0000 | [diff] [blame] | 3557 | } else if (!getLangOpts().ObjC2) { |
| 3558 | if (getLangOpts().CPlusPlus) |
| 3559 | Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range; |
| 3560 | else |
| 3561 | Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range; |
| 3562 | } |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3563 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3564 | } |
| 3565 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3566 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 3567 | // friend declaration, and cannot have an accompanying definition. If we have |
| 3568 | // 'enum foo;', then this is a forward declaration. If we have |
| 3569 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 3570 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3571 | // |
| 3572 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 3573 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 3574 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 3575 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3576 | Sema::TagUseKind TUK; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3577 | if (!AllowDeclaration) { |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3578 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3579 | } else if (Tok.is(tok::l_brace)) { |
| 3580 | if (DS.isFriendSpecified()) { |
| 3581 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
| 3582 | << SourceRange(DS.getFriendSpecLoc()); |
| 3583 | ConsumeBrace(); |
| 3584 | SkipUntil(tok::r_brace); |
| 3585 | TUK = Sema::TUK_Friend; |
| 3586 | } else { |
| 3587 | TUK = Sema::TUK_Definition; |
| 3588 | } |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3589 | } else if (DSC != DSC_type_specifier && |
| 3590 | (Tok.is(tok::semi) || |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3591 | (Tok.isAtStartOfLine() && |
| 3592 | !isValidAfterTypeSpecifier(CanBeBitfield)))) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3593 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
| 3594 | if (Tok.isNot(tok::semi)) { |
| 3595 | // A semicolon was missing after this declaration. Diagnose and recover. |
| 3596 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, |
| 3597 | "enum"); |
| 3598 | PP.EnterToken(Tok); |
| 3599 | Tok.setKind(tok::semi); |
| 3600 | } |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3601 | } else { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3602 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3603 | } |
| 3604 | |
| 3605 | // If this is an elaborated type specifier, and we delayed |
| 3606 | // diagnostics before, just merge them into the current pool. |
| 3607 | if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) { |
| 3608 | diagsFromTag.redelay(); |
| 3609 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3610 | |
| 3611 | MultiTemplateParamsArg TParams; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3612 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3613 | TUK != Sema::TUK_Reference) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3614 | if (!getLangOpts().CPlusPlus11 || !SS.isSet()) { |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3615 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3616 | Diag(Tok, diag::err_enum_template); |
| 3617 | SkipUntil(tok::comma, true); |
| 3618 | return; |
| 3619 | } |
| 3620 | |
| 3621 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 3622 | // Enumerations can't be explicitly instantiated. |
| 3623 | DS.SetTypeSpecError(); |
| 3624 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 3625 | return; |
| 3626 | } |
| 3627 | |
| 3628 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 3629 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 3630 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3631 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3632 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3633 | if (TUK == Sema::TUK_Reference) |
| 3634 | ProhibitAttributes(attrs); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3635 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3636 | if (!Name && TUK != Sema::TUK_Definition) { |
| 3637 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3638 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3639 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3640 | SkipUntil(tok::comma, true); |
| 3641 | return; |
| 3642 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3643 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3644 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3645 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3646 | const char *PrevSpec = 0; |
| 3647 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3648 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3649 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3650 | AS, DS.getModulePrivateSpecLoc(), TParams, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3651 | Owned, IsDependent, ScopedEnumKWLoc, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3652 | IsScopedUsingClassTag, BaseType); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3653 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3654 | if (IsDependent) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3655 | // This enum has a dependent nested-name-specifier. Handle it as a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3656 | // dependent tag. |
| 3657 | if (!Name) { |
| 3658 | DS.SetTypeSpecError(); |
| 3659 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 3660 | return; |
| 3661 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3662 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3663 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3664 | TUK, SS, Name, StartLoc, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3665 | NameLoc); |
| 3666 | if (Type.isInvalid()) { |
| 3667 | DS.SetTypeSpecError(); |
| 3668 | return; |
| 3669 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3670 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3671 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 3672 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3673 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3674 | Diag(StartLoc, DiagID) << PrevSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3675 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3676 | return; |
| 3677 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3678 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3679 | if (!TagDecl) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3680 | // The action failed to produce an enumeration tag. If this is a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3681 | // definition, consume the entire definition. |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3682 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3683 | ConsumeBrace(); |
| 3684 | SkipUntil(tok::r_brace); |
| 3685 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3686 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3687 | DS.SetTypeSpecError(); |
| 3688 | return; |
| 3689 | } |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3690 | |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3691 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3692 | ParseEnumBody(StartLoc, TagDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3693 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3694 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3695 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3696 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3697 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3698 | } |
| 3699 | |
| 3700 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3701 | /// enumerator-list: |
| 3702 | /// enumerator |
| 3703 | /// enumerator-list ',' enumerator |
| 3704 | /// enumerator: |
| 3705 | /// enumeration-constant |
| 3706 | /// enumeration-constant '=' constant-expression |
| 3707 | /// enumeration-constant: |
| 3708 | /// identifier |
| 3709 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3710 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3711 | // Enter the scope of the enum body and start the definition. |
| 3712 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3713 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3714 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3715 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3716 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3717 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3718 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3719 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3720 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3721 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3722 | SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3723 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3724 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3725 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3726 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3727 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3728 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3729 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3730 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3731 | // If attributes exist after the enumerator, parse them. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3732 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3733 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3734 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3735 | ProhibitAttributes(attrs); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3736 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3737 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3738 | ExprResult AssignedVal; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 3739 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3740 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3741 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3742 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3743 | AssignedVal = ParseConstantExpression(); |
| 3744 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3745 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3746 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3747 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3748 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3749 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3750 | LastEnumConstDecl, |
| 3751 | IdentLoc, Ident, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3752 | attrs.getList(), EqualLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3753 | AssignedVal.release()); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3754 | PD.complete(EnumConstDecl); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3755 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3756 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3757 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3758 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3759 | if (Tok.is(tok::identifier)) { |
| 3760 | // We're missing a comma between enumerators. |
| 3761 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3762 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3763 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3764 | continue; |
| 3765 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3766 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3767 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3768 | break; |
| 3769 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3770 | |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3771 | if (Tok.isNot(tok::identifier)) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3772 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11) |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3773 | Diag(CommaLoc, getLangOpts().CPlusPlus ? |
| 3774 | diag::ext_enumerator_list_comma_cxx : |
| 3775 | diag::ext_enumerator_list_comma_c) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3776 | << FixItHint::CreateRemoval(CommaLoc); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3777 | else if (getLangOpts().CPlusPlus11) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3778 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3779 | << FixItHint::CreateRemoval(CommaLoc); |
| 3780 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3781 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3782 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3783 | // Eat the }. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3784 | T.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3785 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3786 | // If attributes exist after the identifier list, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3787 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3788 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3789 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3790 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 3791 | EnumDecl, EnumConstantDecls, |
| 3792 | getCurScope(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3793 | attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3794 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3795 | EnumScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3796 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3797 | T.getCloseLocation()); |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3798 | |
| 3799 | // The next token must be valid after an enum definition. If not, a ';' |
| 3800 | // was probably forgotten. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3801 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
| 3802 | if (!isValidAfterTypeSpecifier(CanBeBitfield)) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3803 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum"); |
| 3804 | // Push this token back into the preprocessor and change our current token |
| 3805 | // to ';' so that the rest of the code recovers as though there were an |
| 3806 | // ';' after the definition. |
| 3807 | PP.EnterToken(Tok); |
| 3808 | Tok.setKind(tok::semi); |
| 3809 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3810 | } |
| 3811 | |
| 3812 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3813 | /// start of a type-qualifier-list. |
| 3814 | bool Parser::isTypeQualifier() const { |
| 3815 | switch (Tok.getKind()) { |
| 3816 | default: return false; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3817 | |
| 3818 | // type-qualifier only in OpenCL |
| 3819 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3820 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3821 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3822 | // type-qualifier |
| 3823 | case tok::kw_const: |
| 3824 | case tok::kw_volatile: |
| 3825 | case tok::kw_restrict: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3826 | case tok::kw___private: |
| 3827 | case tok::kw___local: |
| 3828 | case tok::kw___global: |
| 3829 | case tok::kw___constant: |
| 3830 | case tok::kw___read_only: |
| 3831 | case tok::kw___read_write: |
| 3832 | case tok::kw___write_only: |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3833 | return true; |
| 3834 | } |
| 3835 | } |
| 3836 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3837 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3838 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3839 | /// specifier or if we're not sure. |
| 3840 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3841 | switch (Tok.getKind()) { |
| 3842 | default: return false; |
| 3843 | // type-specifiers |
| 3844 | case tok::kw_short: |
| 3845 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3846 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3847 | case tok::kw___int128: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3848 | case tok::kw_signed: |
| 3849 | case tok::kw_unsigned: |
| 3850 | case tok::kw__Complex: |
| 3851 | case tok::kw__Imaginary: |
| 3852 | case tok::kw_void: |
| 3853 | case tok::kw_char: |
| 3854 | case tok::kw_wchar_t: |
| 3855 | case tok::kw_char16_t: |
| 3856 | case tok::kw_char32_t: |
| 3857 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3858 | case tok::kw_half: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3859 | case tok::kw_float: |
| 3860 | case tok::kw_double: |
| 3861 | case tok::kw_bool: |
| 3862 | case tok::kw__Bool: |
| 3863 | case tok::kw__Decimal32: |
| 3864 | case tok::kw__Decimal64: |
| 3865 | case tok::kw__Decimal128: |
| 3866 | case tok::kw___vector: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3867 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3868 | // OpenCL specific types: |
| 3869 | case tok::kw_image1d_t: |
| 3870 | case tok::kw_image1d_array_t: |
| 3871 | case tok::kw_image1d_buffer_t: |
| 3872 | case tok::kw_image2d_t: |
| 3873 | case tok::kw_image2d_array_t: |
| 3874 | case tok::kw_image3d_t: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3875 | case tok::kw_sampler_t: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3876 | case tok::kw_event_t: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3877 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3878 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3879 | case tok::kw_class: |
| 3880 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3881 | case tok::kw___interface: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3882 | case tok::kw_union: |
| 3883 | // enum-specifier |
| 3884 | case tok::kw_enum: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3885 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3886 | // typedef-name |
| 3887 | case tok::annot_typename: |
| 3888 | return true; |
| 3889 | } |
| 3890 | } |
| 3891 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3892 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3893 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3894 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3895 | switch (Tok.getKind()) { |
| 3896 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3897 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3898 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3899 | if (TryAltiVecVectorToken()) |
| 3900 | return true; |
| 3901 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3902 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3903 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3904 | // recurse to handle whatever we get. |
| 3905 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3906 | return true; |
| 3907 | if (Tok.is(tok::identifier)) |
| 3908 | return false; |
| 3909 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3910 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3911 | case tok::coloncolon: // ::foo::bar |
| 3912 | if (NextToken().is(tok::kw_new) || // ::new |
| 3913 | NextToken().is(tok::kw_delete)) // ::delete |
| 3914 | return false; |
| 3915 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3916 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3917 | return true; |
| 3918 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3919 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3920 | // GNU attributes support. |
| 3921 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3922 | // GNU typeof support. |
| 3923 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3924 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3925 | // type-specifiers |
| 3926 | case tok::kw_short: |
| 3927 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3928 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3929 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3930 | case tok::kw_signed: |
| 3931 | case tok::kw_unsigned: |
| 3932 | case tok::kw__Complex: |
| 3933 | case tok::kw__Imaginary: |
| 3934 | case tok::kw_void: |
| 3935 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3936 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3937 | case tok::kw_char16_t: |
| 3938 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3939 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3940 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3941 | case tok::kw_float: |
| 3942 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3943 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3944 | case tok::kw__Bool: |
| 3945 | case tok::kw__Decimal32: |
| 3946 | case tok::kw__Decimal64: |
| 3947 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3948 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3949 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3950 | // OpenCL specific types: |
| 3951 | case tok::kw_image1d_t: |
| 3952 | case tok::kw_image1d_array_t: |
| 3953 | case tok::kw_image1d_buffer_t: |
| 3954 | case tok::kw_image2d_t: |
| 3955 | case tok::kw_image2d_array_t: |
| 3956 | case tok::kw_image3d_t: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3957 | case tok::kw_sampler_t: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3958 | case tok::kw_event_t: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3959 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3960 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3961 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3962 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3963 | case tok::kw___interface: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3964 | case tok::kw_union: |
| 3965 | // enum-specifier |
| 3966 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3967 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3968 | // type-qualifier |
| 3969 | case tok::kw_const: |
| 3970 | case tok::kw_volatile: |
| 3971 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3972 | |
John McCall | b8a8de3 | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 3973 | // Debugger support. |
| 3974 | case tok::kw___unknown_anytype: |
| 3975 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3976 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 3977 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3978 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3979 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 3980 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3981 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3982 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3983 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3984 | case tok::kw___cdecl: |
| 3985 | case tok::kw___stdcall: |
| 3986 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3987 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3988 | case tok::kw___w64: |
| 3989 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3990 | case tok::kw___ptr32: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3991 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3992 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3993 | |
| 3994 | case tok::kw___private: |
| 3995 | case tok::kw___local: |
| 3996 | case tok::kw___global: |
| 3997 | case tok::kw___constant: |
| 3998 | case tok::kw___read_only: |
| 3999 | case tok::kw___read_write: |
| 4000 | case tok::kw___write_only: |
| 4001 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4002 | return true; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4003 | |
| 4004 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4005 | return getLangOpts().OpenCL; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4006 | |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4007 | // C11 _Atomic |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4008 | case tok::kw__Atomic: |
| 4009 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4010 | } |
| 4011 | } |
| 4012 | |
| 4013 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 4014 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4015 | /// |
| 4016 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 4017 | /// this check is to disambiguate between an expression and a declaration. |
| 4018 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4019 | switch (Tok.getKind()) { |
| 4020 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4021 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4022 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4023 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4024 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4025 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4026 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4027 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4028 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4029 | if (TryAltiVecVectorToken()) |
| 4030 | return true; |
| 4031 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4032 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4033 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4034 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4035 | // recurse to handle whatever we get. |
| 4036 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4037 | return true; |
| 4038 | if (Tok.is(tok::identifier)) |
| 4039 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4040 | |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4041 | // 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] | 4042 | // by an identifier and then either ':' or ']', in a place where an |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4043 | // expression is permitted, then this is probably a class message send |
| 4044 | // missing the initial '['. In this case, we won't consider this to be |
| 4045 | // the start of a declaration. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4046 | if (DisambiguatingWithExpression && |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4047 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 4048 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4049 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4050 | return isDeclarationSpecifier(); |
| 4051 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4052 | case tok::coloncolon: // ::foo::bar |
| 4053 | if (NextToken().is(tok::kw_new) || // ::new |
| 4054 | NextToken().is(tok::kw_delete)) // ::delete |
| 4055 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4056 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4057 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4058 | // recurse to handle whatever we get. |
| 4059 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4060 | return true; |
| 4061 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4062 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4063 | // storage-class-specifier |
| 4064 | case tok::kw_typedef: |
| 4065 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 4066 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4067 | case tok::kw_static: |
| 4068 | case tok::kw_auto: |
| 4069 | case tok::kw_register: |
| 4070 | case tok::kw___thread: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4071 | case tok::kw_thread_local: |
| 4072 | case tok::kw__Thread_local: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4073 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 4074 | // Modules |
| 4075 | case tok::kw___module_private__: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4076 | |
John McCall | b8a8de3 | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4077 | // Debugger support |
| 4078 | case tok::kw___unknown_anytype: |
| 4079 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4080 | // type-specifiers |
| 4081 | case tok::kw_short: |
| 4082 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4083 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4084 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4085 | case tok::kw_signed: |
| 4086 | case tok::kw_unsigned: |
| 4087 | case tok::kw__Complex: |
| 4088 | case tok::kw__Imaginary: |
| 4089 | case tok::kw_void: |
| 4090 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4091 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4092 | case tok::kw_char16_t: |
| 4093 | case tok::kw_char32_t: |
| 4094 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4095 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4096 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4097 | case tok::kw_float: |
| 4098 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4099 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4100 | case tok::kw__Bool: |
| 4101 | case tok::kw__Decimal32: |
| 4102 | case tok::kw__Decimal64: |
| 4103 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4104 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4105 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 4106 | // OpenCL specific types: |
| 4107 | case tok::kw_image1d_t: |
| 4108 | case tok::kw_image1d_array_t: |
| 4109 | case tok::kw_image1d_buffer_t: |
| 4110 | case tok::kw_image2d_t: |
| 4111 | case tok::kw_image2d_array_t: |
| 4112 | case tok::kw_image3d_t: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4113 | case tok::kw_sampler_t: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4114 | case tok::kw_event_t: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 4115 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4116 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4117 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4118 | case tok::kw_struct: |
| 4119 | case tok::kw_union: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4120 | case tok::kw___interface: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4121 | // enum-specifier |
| 4122 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4123 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4124 | // type-qualifier |
| 4125 | case tok::kw_const: |
| 4126 | case tok::kw_volatile: |
| 4127 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4128 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4129 | // function-specifier |
| 4130 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4131 | case tok::kw_virtual: |
| 4132 | case tok::kw_explicit: |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4133 | case tok::kw__Noreturn: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4134 | |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 4135 | // alignment-specifier |
| 4136 | case tok::kw__Alignas: |
| 4137 | |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4138 | // friend keyword. |
| 4139 | case tok::kw_friend: |
| 4140 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 4141 | // static_assert-declaration |
| 4142 | case tok::kw__Static_assert: |
| 4143 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4144 | // GNU typeof support. |
| 4145 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4146 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4147 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4148 | case tok::kw___attribute: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4149 | |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4150 | // C++11 decltype and constexpr. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4151 | case tok::annot_decltype: |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4152 | case tok::kw_constexpr: |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 4153 | |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4154 | // C11 _Atomic |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4155 | case tok::kw__Atomic: |
| 4156 | return true; |
| 4157 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 4158 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4159 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4160 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4161 | |
Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 4162 | // typedef-name |
| 4163 | case tok::annot_typename: |
| 4164 | return !DisambiguatingWithExpression || |
| 4165 | !isStartOfObjCClassMessageMissingOpenBracket(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4166 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 4167 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4168 | case tok::kw___cdecl: |
| 4169 | case tok::kw___stdcall: |
| 4170 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4171 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4172 | case tok::kw___w64: |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4173 | case tok::kw___sptr: |
| 4174 | case tok::kw___uptr: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4175 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4176 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4177 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4178 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4179 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4180 | |
| 4181 | case tok::kw___private: |
| 4182 | case tok::kw___local: |
| 4183 | case tok::kw___global: |
| 4184 | case tok::kw___constant: |
| 4185 | case tok::kw___read_only: |
| 4186 | case tok::kw___read_write: |
| 4187 | case tok::kw___write_only: |
| 4188 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4189 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4190 | } |
| 4191 | } |
| 4192 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4193 | bool Parser::isConstructorDeclarator() { |
| 4194 | TentativeParsingAction TPA(*this); |
| 4195 | |
| 4196 | // Parse the C++ scope specifier. |
| 4197 | CXXScopeSpec SS; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4198 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4199 | /*EnteringContext=*/true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4200 | TPA.Revert(); |
| 4201 | return false; |
| 4202 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4203 | |
| 4204 | // Parse the constructor name. |
| 4205 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 4206 | // We already know that we have a constructor name; just consume |
| 4207 | // the token. |
| 4208 | ConsumeToken(); |
| 4209 | } else { |
| 4210 | TPA.Revert(); |
| 4211 | return false; |
| 4212 | } |
| 4213 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4214 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4215 | if (Tok.isNot(tok::l_paren)) { |
| 4216 | TPA.Revert(); |
| 4217 | return false; |
| 4218 | } |
| 4219 | ConsumeParen(); |
| 4220 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4221 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 4222 | // that we have a constructor. |
| 4223 | if (Tok.is(tok::r_paren) || |
| 4224 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4225 | TPA.Revert(); |
| 4226 | return true; |
| 4227 | } |
| 4228 | |
Richard Smith | 9ec2891 | 2013-09-06 00:12:20 +0000 | [diff] [blame] | 4229 | // A C++11 attribute here signals that we have a constructor, and is an |
| 4230 | // attribute on the first constructor parameter. |
| 4231 | if (getLangOpts().CPlusPlus11 && |
| 4232 | isCXX11AttributeSpecifier(/*Disambiguate*/ false, |
| 4233 | /*OuterMightBeMessageSend*/ true)) { |
| 4234 | TPA.Revert(); |
| 4235 | return true; |
| 4236 | } |
| 4237 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4238 | // If we need to, enter the specified scope. |
| 4239 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4240 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4241 | DeclScopeObj.EnterDeclaratorScope(); |
| 4242 | |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4243 | // Optionally skip Microsoft attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4244 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4245 | MaybeParseMicrosoftAttributes(Attrs); |
| 4246 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4247 | // Check whether the next token(s) are part of a declaration |
| 4248 | // specifier, in which case we have the start of a parameter and, |
| 4249 | // therefore, we know that this is a constructor. |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4250 | bool IsConstructor = false; |
| 4251 | if (isDeclarationSpecifier()) |
| 4252 | IsConstructor = true; |
| 4253 | else if (Tok.is(tok::identifier) || |
| 4254 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 4255 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 4256 | // This might be a parenthesized member name, but is more likely to |
| 4257 | // be a constructor declaration with an invalid argument type. Keep |
| 4258 | // looking. |
| 4259 | if (Tok.is(tok::annot_cxxscope)) |
| 4260 | ConsumeToken(); |
| 4261 | ConsumeToken(); |
| 4262 | |
| 4263 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4264 | // which must have one of the following syntactic forms (see the |
| 4265 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4266 | switch (Tok.getKind()) { |
| 4267 | case tok::l_paren: |
| 4268 | // C(X ( int)); |
| 4269 | case tok::l_square: |
| 4270 | // C(X [ 5]); |
| 4271 | // C(X [ [attribute]]); |
| 4272 | case tok::coloncolon: |
| 4273 | // C(X :: Y); |
| 4274 | // C(X :: *p); |
| 4275 | case tok::r_paren: |
| 4276 | // C(X ) |
| 4277 | // Assume this isn't a constructor, rather than assuming it's a |
| 4278 | // constructor with an unnamed parameter of an ill-formed type. |
| 4279 | break; |
| 4280 | |
| 4281 | default: |
| 4282 | IsConstructor = true; |
| 4283 | break; |
| 4284 | } |
| 4285 | } |
| 4286 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4287 | TPA.Revert(); |
| 4288 | return IsConstructor; |
| 4289 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4290 | |
| 4291 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4292 | /// type-qualifier-list: [C99 6.7.5] |
| 4293 | /// type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4294 | /// [vendor] attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4295 | /// [ only if VendorAttributesAllowed=true ] |
| 4296 | /// type-qualifier-list type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4297 | /// [vendor] type-qualifier-list attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4298 | /// [ only if VendorAttributesAllowed=true ] |
| 4299 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4300 | /// [ only if CXX11AttributesAllowed=true ] |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4301 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4302 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4303 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 4304 | bool VendorAttributesAllowed, |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4305 | bool CXX11AttributesAllowed, |
| 4306 | bool AtomicAllowed) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4307 | if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed && |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4308 | isCXX11AttributeSpecifier()) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4309 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4310 | ParseCXX11Attributes(attrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4311 | DS.takeAttributesFrom(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4312 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4313 | |
| 4314 | SourceLocation EndLoc; |
| 4315 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4316 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4317 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4318 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4319 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4320 | SourceLocation Loc = Tok.getLocation(); |
| 4321 | |
| 4322 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4323 | case tok::code_completion: |
| 4324 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 4325 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4326 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4327 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4328 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4329 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4330 | break; |
| 4331 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4332 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4333 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4334 | break; |
| 4335 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4336 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4337 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4338 | break; |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4339 | case tok::kw__Atomic: |
| 4340 | if (!AtomicAllowed) |
| 4341 | goto DoneWithTypeQuals; |
| 4342 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 4343 | getLangOpts()); |
| 4344 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4345 | |
| 4346 | // OpenCL qualifiers: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4347 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4348 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4349 | goto DoneWithTypeQuals; |
| 4350 | case tok::kw___private: |
| 4351 | case tok::kw___global: |
| 4352 | case tok::kw___local: |
| 4353 | case tok::kw___constant: |
| 4354 | case tok::kw___read_only: |
| 4355 | case tok::kw___write_only: |
| 4356 | case tok::kw___read_write: |
| 4357 | ParseOpenCLQualifiers(DS); |
| 4358 | break; |
| 4359 | |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4360 | case tok::kw___sptr: |
| 4361 | case tok::kw___uptr: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4362 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 4363 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4364 | case tok::kw___ptr32: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4365 | case tok::kw___cdecl: |
| 4366 | case tok::kw___stdcall: |
| 4367 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4368 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4369 | case tok::kw___unaligned: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4370 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4371 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4372 | continue; |
| 4373 | } |
| 4374 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4375 | case tok::kw___pascal: |
| 4376 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4377 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4378 | continue; |
| 4379 | } |
| 4380 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4381 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4382 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4383 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4384 | continue; // do *not* consume the next token! |
| 4385 | } |
| 4386 | // otherwise, FALL THROUGH! |
| 4387 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4388 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4389 | // If this is not a type-qualifier token, we're done reading type |
| 4390 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 4391 | DS.Finish(Diags, PP); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4392 | if (EndLoc.isValid()) |
| 4393 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4394 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4395 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4396 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4397 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 4398 | if (isInvalid) { |
| 4399 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4400 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4401 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4402 | EndLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4403 | } |
| 4404 | } |
| 4405 | |
| 4406 | |
| 4407 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 4408 | /// |
| 4409 | void Parser::ParseDeclarator(Declarator &D) { |
| 4410 | /// This implements the 'declarator' production in the C grammar, then checks |
| 4411 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4412 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4413 | } |
| 4414 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4415 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) { |
| 4416 | if (Kind == tok::star || Kind == tok::caret) |
| 4417 | return true; |
| 4418 | |
| 4419 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 4420 | if (!Lang.CPlusPlus) |
| 4421 | return false; |
| 4422 | |
| 4423 | return Kind == tok::amp || Kind == tok::ampamp; |
| 4424 | } |
| 4425 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4426 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 4427 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 4428 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4429 | /// ptr-operator production. |
| 4430 | /// |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4431 | /// 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] | 4432 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 4433 | /// isConstructorDeclarator. |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4434 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4435 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 4436 | /// [C] pointer[opt] direct-declarator |
| 4437 | /// [C++] direct-declarator |
| 4438 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4439 | /// |
| 4440 | /// pointer: [C99 6.7.5] |
| 4441 | /// '*' type-qualifier-list[opt] |
| 4442 | /// '*' type-qualifier-list[opt] pointer |
| 4443 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4444 | /// ptr-operator: |
| 4445 | /// '*' cv-qualifier-seq[opt] |
| 4446 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4447 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4448 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4449 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4450 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4451 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 4452 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 4453 | if (Diags.hasAllExtensionsSilenced()) |
| 4454 | D.setExtension(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4455 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4456 | // C++ member pointers start with a '::' or a nested-name. |
| 4457 | // Member pointers get special handling, since there's no place for the |
| 4458 | // scope spec in the generic path below. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4459 | if (getLangOpts().CPlusPlus && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 4460 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 4461 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4462 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4463 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4464 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4465 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4466 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 4467 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4468 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4469 | // The scope spec really belongs to the direct-declarator. |
Richard Smith | 6a502c4 | 2013-01-08 22:43:49 +0000 | [diff] [blame] | 4470 | if (D.mayHaveIdentifier()) |
| 4471 | D.getCXXScopeSpec() = SS; |
| 4472 | else |
| 4473 | AnnotateScopeToken(SS, true); |
| 4474 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4475 | if (DirectDeclParser) |
| 4476 | (this->*DirectDeclParser)(D); |
| 4477 | return; |
| 4478 | } |
| 4479 | |
| 4480 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4481 | D.SetRangeEnd(Loc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4482 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4483 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4484 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4485 | |
| 4486 | // Recurse to parse whatever is left. |
| 4487 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 4488 | |
| 4489 | // Sema will have to catch (syntactically invalid) pointers into global |
| 4490 | // scope. It has to catch pointers into namespace scope anyway. |
| 4491 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4492 | Loc), |
| 4493 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4494 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4495 | return; |
| 4496 | } |
| 4497 | } |
| 4498 | |
| 4499 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4500 | // Not a pointer, C++ reference, or block. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4501 | if (!isPtrOperatorToken(Kind, getLangOpts())) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4502 | if (DirectDeclParser) |
| 4503 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4504 | return; |
| 4505 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4506 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4507 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 4508 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4509 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4510 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4511 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 4512 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4513 | // Is a pointer. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4514 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4515 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4516 | // FIXME: GNU attributes are not allowed here in a new-type-id. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4517 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4518 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4519 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4520 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4521 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4522 | if (Kind == tok::star) |
| 4523 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 4524 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 4525 | DS.getConstSpecLoc(), |
| 4526 | DS.getVolatileSpecLoc(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4527 | DS.getRestrictSpecLoc()), |
| 4528 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4529 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4530 | else |
| 4531 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4532 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4533 | Loc), |
| 4534 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4535 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4536 | } else { |
| 4537 | // Is a reference |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4538 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4539 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4540 | // Complain about rvalue references in C++03, but then go on and build |
| 4541 | // the declarator. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4542 | if (Kind == tok::ampamp) |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4543 | Diag(Loc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4544 | diag::warn_cxx98_compat_rvalue_reference : |
| 4545 | diag::ext_rvalue_reference); |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4546 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4547 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 4548 | ParseTypeQualifierListOpt(DS); |
| 4549 | D.ExtendWithDeclSpec(DS); |
| 4550 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4551 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 4552 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 4553 | // template type argument, in which case the cv-qualifiers are ignored. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4554 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 4555 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 4556 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4557 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4558 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 4559 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4560 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4561 | // 'restrict' is permitted as an extension. |
| 4562 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 4563 | Diag(DS.getAtomicSpecLoc(), |
| 4564 | diag::err_invalid_reference_qualifier_application) << "_Atomic"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4565 | } |
| 4566 | |
| 4567 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4568 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4569 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4570 | if (D.getNumTypeObjects() > 0) { |
| 4571 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 4572 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 4573 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 4574 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 4575 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4576 | << II; |
| 4577 | else |
| 4578 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4579 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4580 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4581 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4582 | // can go ahead and build the (technically ill-formed) |
| 4583 | // declarator: reference collapsing will take care of it. |
| 4584 | } |
| 4585 | } |
| 4586 | |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4587 | // Remember that we parsed a reference type. |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4588 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4589 | Kind == tok::amp), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4590 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4591 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4592 | } |
| 4593 | } |
| 4594 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4595 | static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D, |
| 4596 | SourceLocation EllipsisLoc) { |
| 4597 | if (EllipsisLoc.isValid()) { |
| 4598 | FixItHint Insertion; |
| 4599 | if (!D.getEllipsisLoc().isValid()) { |
| 4600 | Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "..."); |
| 4601 | D.setEllipsisLoc(EllipsisLoc); |
| 4602 | } |
| 4603 | P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) |
| 4604 | << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName(); |
| 4605 | } |
| 4606 | } |
| 4607 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4608 | /// ParseDirectDeclarator |
| 4609 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4610 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4611 | /// '(' declarator ')' |
| 4612 | /// [GNU] '(' attributes declarator ')' |
| 4613 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4614 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4615 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4616 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4617 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4618 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 4619 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4620 | /// direct-declarator '(' parameter-type-list ')' |
| 4621 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4622 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4623 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 4624 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 4625 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4626 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 4627 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 4628 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4629 | /// [C++] declarator-id |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4630 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4631 | /// |
| 4632 | /// declarator-id: [C++ 8] |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4633 | /// '...'[opt] id-expression |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4634 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 4635 | /// |
| 4636 | /// id-expression: [C++ 5.1] |
| 4637 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4638 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4639 | /// |
| 4640 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4641 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4642 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4643 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4644 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 4645 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 4646 | /// |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4647 | /// Note, any additional constructs added here may need corresponding changes |
| 4648 | /// in isConstructorDeclarator. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4649 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4650 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4651 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4652 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4653 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4654 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4655 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4656 | D.getContext() == Declarator::MemberContext; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4657 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4658 | EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4659 | } |
| 4660 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4661 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4662 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 4663 | // Change the declaration context for name lookup, until this function |
| 4664 | // is exited (and the declarator has been parsed). |
| 4665 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4666 | } |
| 4667 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4668 | // C++0x [dcl.fct]p14: |
| 4669 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4670 | // of a parameter-declaration-clause without a preceding comma. In |
| 4671 | // this case, the ellipsis is parsed as part of the |
| 4672 | // abstract-declarator if the type of the parameter names a template |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4673 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 4674 | // as part of the parameter-declaration-clause. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4675 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4676 | !((D.getContext() == Declarator::PrototypeContext || |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 4677 | D.getContext() == Declarator::LambdaExprParameterContext || |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4678 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4679 | NextToken().is(tok::r_paren) && |
Richard Smith | 30f2a74 | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4680 | !D.hasGroupingParens() && |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4681 | !Actions.containsUnexpandedParameterPacks(D))) { |
| 4682 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 4683 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) { |
| 4684 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 4685 | // the user what they should have done. |
| 4686 | ParseDeclarator(D); |
| 4687 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4688 | return; |
| 4689 | } else |
| 4690 | D.setEllipsisLoc(EllipsisLoc); |
| 4691 | |
| 4692 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 4693 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 4694 | // the l_paren token. |
| 4695 | } |
| 4696 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4697 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 4698 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 4699 | // We found something that indicates the start of an unqualified-id. |
| 4700 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4701 | bool AllowConstructorName; |
| 4702 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 4703 | AllowConstructorName = false; |
| 4704 | else if (D.getCXXScopeSpec().isSet()) |
| 4705 | AllowConstructorName = |
| 4706 | (D.getContext() == Declarator::FileContext || |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 4707 | D.getContext() == Declarator::MemberContext); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4708 | else |
| 4709 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 4710 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4711 | SourceLocation TemplateKWLoc; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4712 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 4713 | /*EnteringContext=*/true, |
| 4714 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4715 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4716 | ParsedType(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4717 | TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4718 | D.getName()) || |
| 4719 | // Once we're past the identifier, if the scope was bad, mark the |
| 4720 | // whole declarator bad. |
| 4721 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4722 | D.SetIdentifier(0, Tok.getLocation()); |
| 4723 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4724 | } else { |
| 4725 | // Parsed the unqualified-id; update range information and move along. |
| 4726 | if (D.getSourceRange().getBegin().isInvalid()) |
| 4727 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 4728 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4729 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4730 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4731 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4732 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4733 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4734 | "There's a C++-specific check for tok::identifier above"); |
| 4735 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 4736 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 4737 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4738 | goto PastIdentifier; |
Richard Smith | a38253c | 2013-07-11 05:10:21 +0000 | [diff] [blame] | 4739 | } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) { |
Richard Smith | 8d1ab8a | 2013-10-13 22:12:28 +0000 | [diff] [blame] | 4740 | // A virt-specifier isn't treated as an identifier if it appears after a |
| 4741 | // trailing-return-type. |
| 4742 | if (D.getContext() != Declarator::TrailingReturnContext || |
| 4743 | !isCXX11VirtSpecifier(Tok)) { |
| 4744 | Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id) |
| 4745 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 4746 | D.SetIdentifier(0, Tok.getLocation()); |
| 4747 | ConsumeToken(); |
| 4748 | goto PastIdentifier; |
| 4749 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4750 | } |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4751 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4752 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4753 | // direct-declarator: '(' declarator ')' |
| 4754 | // direct-declarator: '(' attributes declarator ')' |
| 4755 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 4756 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4757 | |
| 4758 | // If the declarator was parenthesized, we entered the declarator |
| 4759 | // scope when parsing the parenthesized declarator, then exited |
| 4760 | // the scope already. Re-enter the scope, if we need to. |
| 4761 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4762 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4763 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4764 | if (!D.isInvalidType() && |
| 4765 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4766 | // Change the declaration context for name lookup, until this function |
| 4767 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4768 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4769 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4770 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4771 | // This could be something simple like "int" (in which case the declarator |
| 4772 | // portion is empty), if an abstract-declarator is allowed. |
| 4773 | D.SetIdentifier(0, Tok.getLocation()); |
Richard Smith | 30f2a74 | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4774 | |
| 4775 | // The grammar for abstract-pack-declarator does not allow grouping parens. |
| 4776 | // FIXME: Revisit this once core issue 1488 is resolved. |
| 4777 | if (D.hasEllipsis() && D.hasGroupingParens()) |
| 4778 | Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()), |
| 4779 | diag::ext_abstract_pack_declarator_parens); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4780 | } else { |
David Blaikie | e75d9cf | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 4781 | if (Tok.getKind() == tok::annot_pragma_parser_crash) |
David Blaikie | 377da4c | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 4782 | LLVM_BUILTIN_TRAP; |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 4783 | if (D.getContext() == Declarator::MemberContext) |
| 4784 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 4785 | << D.getDeclSpec().getSourceRange(); |
Richard Trieu | db55c04c | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 4786 | else if (getLangOpts().CPlusPlus) { |
| 4787 | if (Tok.is(tok::period) || Tok.is(tok::arrow)) |
| 4788 | Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow); |
Richard Trieu | efb288c | 2013-09-05 02:31:33 +0000 | [diff] [blame] | 4789 | else { |
| 4790 | SourceLocation Loc = D.getCXXScopeSpec().getEndLoc(); |
| 4791 | if (Tok.isAtStartOfLine() && Loc.isValid()) |
| 4792 | Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id) |
| 4793 | << getLangOpts().CPlusPlus; |
| 4794 | else |
| 4795 | Diag(Tok, diag::err_expected_unqualified_id) |
| 4796 | << getLangOpts().CPlusPlus; |
| 4797 | } |
Richard Trieu | db55c04c | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 4798 | } else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4799 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4800 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 4801 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4802 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4803 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4804 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4805 | assert(D.isPastIdentifier() && |
| 4806 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4807 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4808 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 4809 | if (D.hasName() && !D.getNumTypeObjects()) |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4810 | MaybeParseCXX11Attributes(D); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4811 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4812 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4813 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4814 | // Enter function-declaration scope, limiting any declarators to the |
| 4815 | // function prototype scope, including parameter declarators. |
| 4816 | ParseScope PrototypeScope(this, |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4817 | Scope::FunctionPrototypeScope|Scope::DeclScope| |
| 4818 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 4819 | ? Scope::FunctionDeclarationScope : 0)); |
| 4820 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4821 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 4822 | // In such a case, check if we actually have a function declarator; if it |
| 4823 | // is not, the declarator has been fully parsed. |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4824 | bool IsAmbiguous = false; |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 4825 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 4826 | // The name of the declarator, if any, is tentatively declared within |
| 4827 | // a possible direct initializer. |
| 4828 | TentativelyDeclaredIdentifiers.push_back(D.getIdentifier()); |
| 4829 | bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous); |
| 4830 | TentativelyDeclaredIdentifiers.pop_back(); |
| 4831 | if (!IsFunctionDecl) |
| 4832 | break; |
| 4833 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4834 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4835 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4836 | T.consumeOpen(); |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4837 | ParseFunctionDeclarator(D, attrs, T, IsAmbiguous); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4838 | PrototypeScope.Exit(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4839 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4840 | ParseBracketDeclarator(D); |
| 4841 | } else { |
| 4842 | break; |
| 4843 | } |
| 4844 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4845 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4846 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4847 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 4848 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4849 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4850 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 4851 | /// |
| 4852 | /// direct-declarator: |
| 4853 | /// '(' declarator ')' |
| 4854 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4855 | /// direct-declarator '(' parameter-type-list ')' |
| 4856 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4857 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4858 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4859 | /// |
| 4860 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4861 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4862 | T.consumeOpen(); |
| 4863 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4864 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4865 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4866 | // Eat any attributes before we look at whether this is a grouping or function |
| 4867 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 4868 | // the type being built up, for example: |
| 4869 | // int (__attribute__(()) *x)(long y) |
| 4870 | // If this ends up not being a grouping paren, the attribute applies to the |
| 4871 | // first argument, for example: |
| 4872 | // int (__attribute__(()) int x) |
| 4873 | // In either case, we need to eat any attributes to be able to determine what |
| 4874 | // sort of paren this is. |
| 4875 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4876 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4877 | bool RequiresArg = false; |
| 4878 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4879 | ParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4880 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4881 | // We require that the argument list (if this is a non-grouping paren) be |
| 4882 | // present even if the attribute list was empty. |
| 4883 | RequiresArg = true; |
| 4884 | } |
Chad Rosier | 9cab1c9 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 4885 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4886 | // Eat any Microsoft extensions. |
Chad Rosier | 9cab1c9 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 4887 | ParseMicrosoftTypeAttributes(attrs); |
| 4888 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4889 | // Eat any Borland extensions. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 4890 | if (Tok.is(tok::kw___pascal)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4891 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4892 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4893 | // If we haven't past the identifier yet (or where the identifier would be |
| 4894 | // stored, if this is an abstract declarator), then this is probably just |
| 4895 | // grouping parens. However, if this could be an abstract-declarator, then |
| 4896 | // this could also be the start of function arguments (consider 'void()'). |
| 4897 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4898 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4899 | if (!D.mayOmitIdentifier()) { |
| 4900 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 4901 | // paren, because we haven't seen the identifier yet. |
| 4902 | isGrouping = true; |
| 4903 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4904 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 4905 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4906 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 4907 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4908 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 4909 | // considered to be a type, not a K&R identifier-list. |
| 4910 | isGrouping = false; |
| 4911 | } else { |
| 4912 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 4913 | isGrouping = true; |
| 4914 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4915 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4916 | // If this is a grouping paren, handle: |
| 4917 | // direct-declarator: '(' declarator ')' |
| 4918 | // direct-declarator: '(' attributes declarator ')' |
| 4919 | if (isGrouping) { |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4920 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 4921 | D.setEllipsisLoc(SourceLocation()); |
| 4922 | |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4923 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4924 | D.setGroupingParens(true); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4925 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4926 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4927 | T.consumeClose(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4928 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4929 | T.getCloseLocation()), |
| 4930 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4931 | |
| 4932 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4933 | |
| 4934 | // An ellipsis cannot be placed outside parentheses. |
| 4935 | if (EllipsisLoc.isValid()) |
| 4936 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4937 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4938 | return; |
| 4939 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4940 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4941 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 4942 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4943 | // identifier (and remember where it would have been), then call into |
| 4944 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4945 | D.SetIdentifier(0, Tok.getLocation()); |
| 4946 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4947 | // Enter function-declaration scope, limiting any declarators to the |
| 4948 | // function prototype scope, including parameter declarators. |
| 4949 | ParseScope PrototypeScope(this, |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4950 | Scope::FunctionPrototypeScope | Scope::DeclScope | |
| 4951 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 4952 | ? Scope::FunctionDeclarationScope : 0)); |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4953 | ParseFunctionDeclarator(D, attrs, T, false, RequiresArg); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4954 | PrototypeScope.Exit(); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4955 | } |
| 4956 | |
| 4957 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 4958 | /// declarator D up to a paren, which indicates that we are parsing function |
| 4959 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4960 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4961 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 4962 | /// immediately after the open paren - they should be considered to be the |
| 4963 | /// first argument of a parameter. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4964 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4965 | /// If RequiresArg is true, then the first argument of the function is required |
| 4966 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4967 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4968 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 4969 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 4970 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 4971 | /// |
| 4972 | /// [C++11] exception-specification: |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4973 | /// dynamic-exception-specification |
| 4974 | /// noexcept-specification |
| 4975 | /// |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4976 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4977 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4978 | BalancedDelimiterTracker &Tracker, |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4979 | bool IsAmbiguous, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4980 | bool RequiresArg) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4981 | assert(getCurScope()->isFunctionPrototypeScope() && |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4982 | "Should call from a Function scope"); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4983 | // lparen is already consumed! |
| 4984 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 4985 | |
| 4986 | // This should be true when the function has typed arguments. |
| 4987 | // Otherwise, it is treated as a K&R-style function. |
| 4988 | bool HasProto = false; |
| 4989 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4990 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4991 | // Remember where we see an ellipsis, if any. |
| 4992 | SourceLocation EllipsisLoc; |
| 4993 | |
| 4994 | DeclSpec DS(AttrFactory); |
| 4995 | bool RefQualifierIsLValueRef = true; |
| 4996 | SourceLocation RefQualifierLoc; |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4997 | SourceLocation ConstQualifierLoc; |
| 4998 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4999 | ExceptionSpecificationType ESpecType = EST_None; |
| 5000 | SourceRange ESpecRange; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5001 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 5002 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5003 | ExprResult NoexceptExpr; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5004 | ParsedAttributes FnAttrs(AttrFactory); |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 5005 | TypeResult TrailingReturnType; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5006 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 5007 | Actions.ActOnStartFunctionDeclarator(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5008 | /* LocalEndLoc is the end location for the local FunctionTypeLoc. |
| 5009 | EndLoc is the end location for the function declarator. |
| 5010 | They differ for trailing return types. */ |
| 5011 | SourceLocation StartLoc, LocalEndLoc, EndLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5012 | SourceLocation LParenLoc, RParenLoc; |
| 5013 | LParenLoc = Tracker.getOpenLocation(); |
| 5014 | StartLoc = LParenLoc; |
| 5015 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5016 | if (isFunctionDeclaratorIdentifierList()) { |
| 5017 | if (RequiresArg) |
| 5018 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5019 | |
| 5020 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 5021 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5022 | Tracker.consumeClose(); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5023 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5024 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5025 | EndLoc = RParenLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5026 | } else { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5027 | if (Tok.isNot(tok::r_paren)) |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5028 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, |
| 5029 | EllipsisLoc); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5030 | else if (RequiresArg) |
| 5031 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5032 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5033 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5034 | |
| 5035 | // If we have the closing ')', eat it. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5036 | Tracker.consumeClose(); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5037 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5038 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5039 | EndLoc = RParenLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5040 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5041 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5042 | // FIXME: Accept these components in any order, and produce fixits to |
| 5043 | // correct the order if the user gets it wrong. Ideally we should deal |
| 5044 | // with the virt-specifier-seq and pure-specifier in the same way. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5045 | |
| 5046 | // Parse cv-qualifier-seq[opt]. |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5047 | ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false, |
| 5048 | /*CXX11AttributesAllowed*/ false, |
| 5049 | /*AtomicAllowed*/ false); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5050 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 5051 | EndLoc = DS.getSourceRange().getEnd(); |
| 5052 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 5053 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 5054 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5055 | |
| 5056 | // Parse ref-qualifier[opt]. |
| 5057 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5058 | Diag(Tok, getLangOpts().CPlusPlus11 ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5059 | diag::warn_cxx98_compat_ref_qualifier : |
| 5060 | diag::ext_ref_qualifier); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5061 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5062 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 5063 | RefQualifierLoc = ConsumeToken(); |
| 5064 | EndLoc = RefQualifierLoc; |
| 5065 | } |
| 5066 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5067 | // C++11 [expr.prim.general]p3: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5068 | // If a declaration declares a member function or member function |
| 5069 | // 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] | 5070 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5071 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5072 | // declarator. |
Richard Smith | d922779 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5073 | // FIXME: currently, "static" case isn't handled correctly. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5074 | bool IsCXX11MemberFunction = |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5075 | getLangOpts().CPlusPlus11 && |
Richard Smith | d922779 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5076 | (D.getContext() == Declarator::MemberContext |
| 5077 | ? !D.getDeclSpec().isFriendSpecified() |
| 5078 | : D.getContext() == Declarator::FileContext && |
| 5079 | D.getCXXScopeSpec().isValid() && |
| 5080 | Actions.CurContext->isRecord()); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5081 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 5082 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
Richard Smith | 7b19cb1 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 5083 | DS.getTypeQualifiers() | |
Richard Smith | 8404626 | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 5084 | (D.getDeclSpec().isConstexprSpecified() && |
| 5085 | !getLangOpts().CPlusPlus1y |
Richard Smith | 7b19cb1 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 5086 | ? Qualifiers::Const : 0), |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5087 | IsCXX11MemberFunction); |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5088 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5089 | // Parse exception-specification[opt]. |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5090 | ESpecType = tryParseExceptionSpecification(ESpecRange, |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 5091 | DynamicExceptions, |
| 5092 | DynamicExceptionRanges, |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5093 | NoexceptExpr); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5094 | if (ESpecType != EST_None) |
| 5095 | EndLoc = ESpecRange.getEnd(); |
| 5096 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5097 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 5098 | // after the exception-specification. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5099 | MaybeParseCXX11Attributes(FnAttrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5100 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5101 | // Parse trailing-return-type[opt]. |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5102 | LocalEndLoc = EndLoc; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5103 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5104 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5105 | if (D.getDeclSpec().getTypeSpecType() == TST_auto) |
| 5106 | StartLoc = D.getDeclSpec().getTypeSpecTypeLoc(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5107 | LocalEndLoc = Tok.getLocation(); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 5108 | SourceRange Range; |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 5109 | TrailingReturnType = ParseTrailingReturnType(Range); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5110 | EndLoc = Range.getEnd(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5111 | } |
| 5112 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5113 | } |
| 5114 | |
| 5115 | // Remember that we parsed a function type, and remember the attributes. |
| 5116 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5117 | IsAmbiguous, |
| 5118 | LParenLoc, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5119 | ParamInfo.data(), ParamInfo.size(), |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5120 | EllipsisLoc, RParenLoc, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5121 | DS.getTypeQualifiers(), |
| 5122 | RefQualifierIsLValueRef, |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 5123 | RefQualifierLoc, ConstQualifierLoc, |
| 5124 | VolatileQualifierLoc, |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 5125 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5126 | ESpecType, ESpecRange.getBegin(), |
| 5127 | DynamicExceptions.data(), |
| 5128 | DynamicExceptionRanges.data(), |
| 5129 | DynamicExceptions.size(), |
| 5130 | NoexceptExpr.isUsable() ? |
| 5131 | NoexceptExpr.get() : 0, |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5132 | StartLoc, LocalEndLoc, D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5133 | TrailingReturnType), |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5134 | FnAttrs, EndLoc); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 5135 | |
| 5136 | Actions.ActOnEndFunctionDeclarator(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5137 | } |
| 5138 | |
| 5139 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 5140 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 5141 | /// |
| 5142 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 5143 | /// abstract-declarators. |
| 5144 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5145 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5146 | && Tok.is(tok::identifier) |
| 5147 | && !TryAltiVecVectorToken() |
| 5148 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 5149 | // 6.7.5.3p11. |
| 5150 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 5151 | // Identifier lists follow a really simple grammar: the identifiers can |
| 5152 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 5153 | // identifier lists are really rare in the brave new modern world, and |
| 5154 | // it is very common for someone to typo a type in a non-K&R style |
| 5155 | // list. If we are presented with something like: "void foo(intptr x, |
| 5156 | // float y)", we don't want to start parsing the function declarator as |
| 5157 | // though it is a K&R style declarator just because intptr is an |
| 5158 | // invalid type. |
| 5159 | // |
| 5160 | // To handle this, we check to see if the token after the first |
| 5161 | // identifier is a "," or ")". Only then do we parse it as an |
| 5162 | // identifier list. |
| 5163 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 5164 | } |
| 5165 | |
| 5166 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 5167 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 5168 | /// |
| 5169 | /// After returning, ParamInfo will hold the parsed parameters. |
| 5170 | /// |
| 5171 | /// identifier-list: [C99 6.7.5] |
| 5172 | /// identifier |
| 5173 | /// identifier-list ',' identifier |
| 5174 | /// |
| 5175 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 5176 | Declarator &D, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5177 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5178 | // If there was no identifier specified for the declarator, either we are in |
| 5179 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 5180 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 5181 | // diagnose this. |
| 5182 | if (!D.getIdentifier()) |
| 5183 | Diag(Tok, diag::ext_ident_list_in_param); |
| 5184 | |
| 5185 | // Maintain an efficient lookup of params we have seen so far. |
| 5186 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 5187 | |
| 5188 | while (1) { |
| 5189 | // If this isn't an identifier, report the error and skip until ')'. |
| 5190 | if (Tok.isNot(tok::identifier)) { |
| 5191 | Diag(Tok, diag::err_expected_ident); |
| 5192 | SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 5193 | // Forget we parsed anything. |
| 5194 | ParamInfo.clear(); |
| 5195 | return; |
| 5196 | } |
| 5197 | |
| 5198 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 5199 | |
| 5200 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 5201 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 5202 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 5203 | |
| 5204 | // Verify that the argument identifier has not already been mentioned. |
| 5205 | if (!ParamsSoFar.insert(ParmII)) { |
| 5206 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 5207 | } else { |
| 5208 | // Remember this identifier in ParamInfo. |
| 5209 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 5210 | Tok.getLocation(), |
| 5211 | 0)); |
| 5212 | } |
| 5213 | |
| 5214 | // Eat the identifier. |
| 5215 | ConsumeToken(); |
| 5216 | |
| 5217 | // The list continues if we see a comma. |
| 5218 | if (Tok.isNot(tok::comma)) |
| 5219 | break; |
| 5220 | ConsumeToken(); |
| 5221 | } |
| 5222 | } |
| 5223 | |
| 5224 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 5225 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 5226 | /// identifier list. |
| 5227 | /// |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5228 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 5229 | /// caller parsed those arguments immediately after the open paren - they should |
| 5230 | /// be considered to be part of the first parameter. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5231 | /// |
| 5232 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 5233 | /// be the location of the ellipsis, if any was parsed. |
| 5234 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5235 | /// parameter-type-list: [C99 6.7.5] |
| 5236 | /// parameter-list |
| 5237 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5238 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5239 | /// |
| 5240 | /// parameter-list: [C99 6.7.5] |
| 5241 | /// parameter-declaration |
| 5242 | /// parameter-list ',' parameter-declaration |
| 5243 | /// |
| 5244 | /// parameter-declaration: [C99 6.7.5] |
| 5245 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5246 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5247 | /// [C++11] initializer-clause |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5248 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 5249 | /// declaration-specifiers abstract-declarator[opt] |
| 5250 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 5251 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5252 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5253 | /// [C++11] attribute-specifier-seq parameter-declaration |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5254 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5255 | void Parser::ParseParameterDeclarationClause( |
| 5256 | Declarator &D, |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5257 | ParsedAttributes &FirstArgAttrs, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5258 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5259 | SourceLocation &EllipsisLoc) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5260 | while (1) { |
| 5261 | if (Tok.is(tok::ellipsis)) { |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5262 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 5263 | // before deciding this was a parameter-declaration-clause. |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 5264 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5265 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5266 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5267 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5268 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5269 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5270 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5271 | |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5272 | // Parse any C++11 attributes. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5273 | MaybeParseCXX11Attributes(DS.getAttributes()); |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5274 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5275 | // Skip any Microsoft attributes before a param. |
Chad Rosier | 16f90bf | 2012-12-20 20:37:53 +0000 | [diff] [blame] | 5276 | MaybeParseMicrosoftAttributes(DS.getAttributes()); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5277 | |
| 5278 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5279 | |
| 5280 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5281 | // 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] | 5282 | // 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] | 5283 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 5284 | // too much hassle. |
| 5285 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5286 | |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 5287 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5288 | |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5289 | |
| 5290 | // Parse the declarator. This is "PrototypeContext" or |
| 5291 | // "LambdaExprParameterContext", because we must accept either |
| 5292 | // 'declarator' or 'abstract-declarator' here. |
| 5293 | Declarator ParmDeclarator(DS, |
| 5294 | D.getContext() == Declarator::LambdaExprContext ? |
| 5295 | Declarator::LambdaExprParameterContext : |
| 5296 | Declarator::PrototypeContext); |
| 5297 | ParseDeclarator(ParmDeclarator); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5298 | |
| 5299 | // Parse GNU attributes, if present. |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5300 | MaybeParseGNUAttributes(ParmDeclarator); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5301 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5302 | // Remember this parsed parameter in ParamInfo. |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5303 | IdentifierInfo *ParmII = ParmDeclarator.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5304 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5305 | // DefArgToks is used when the parsing of default arguments needs |
| 5306 | // to be delayed. |
| 5307 | CachedTokens *DefArgToks = 0; |
| 5308 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5309 | // If no parameter was specified, verify that *something* was specified, |
| 5310 | // otherwise we have a missing type and identifier. |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5311 | if (DS.isEmpty() && ParmDeclarator.getIdentifier() == 0 && |
| 5312 | ParmDeclarator.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5313 | // Completely missing, emit error. |
| 5314 | Diag(DSStart, diag::err_missing_param); |
| 5315 | } else { |
| 5316 | // Otherwise, we have something. Add it and let semantic analysis try |
| 5317 | // 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] | 5318 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5319 | // Inform the actions module about the parameter declarator, so it gets |
| 5320 | // added to the current scope. |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5321 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), |
| 5322 | ParmDeclarator); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5323 | // Parse the default argument, if any. We parse the default |
| 5324 | // arguments in all dialects; the semantic analysis in |
| 5325 | // ActOnParamDefaultArgument will reject the default argument in |
| 5326 | // C. |
| 5327 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5328 | SourceLocation EqualLoc = Tok.getLocation(); |
| 5329 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5330 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5331 | if (D.getContext() == Declarator::MemberContext) { |
| 5332 | // If we're inside a class definition, cache the tokens |
| 5333 | // corresponding to the default argument. We'll actually parse |
| 5334 | // them when we see the end of the class definition. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5335 | // FIXME: Can we use a smart pointer for Toks? |
| 5336 | DefArgToks = new CachedTokens; |
| 5337 | |
Richard Smith | 9bd3cdc | 2013-09-12 23:28:08 +0000 | [diff] [blame] | 5338 | if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5339 | delete DefArgToks; |
| 5340 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5341 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5342 | } else { |
| 5343 | // Mark the end of the default argument so that we know when to |
| 5344 | // stop when we parse it later on. |
| 5345 | Token DefArgEnd; |
| 5346 | DefArgEnd.startToken(); |
| 5347 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 5348 | DefArgEnd.setLocation(Tok.getLocation()); |
| 5349 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5350 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 5351 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5352 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5353 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5354 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5355 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5356 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5357 | // The argument isn't actually potentially evaluated unless it is |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5358 | // used. |
| 5359 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 5360 | Sema::PotentiallyEvaluatedIfUsed, |
| 5361 | Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5362 | |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5363 | ExprResult DefArgResult; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5364 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5365 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5366 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5367 | } else |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5368 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5369 | if (DefArgResult.isInvalid()) { |
| 5370 | Actions.ActOnParamDefaultArgumentError(Param); |
| 5371 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 5372 | } else { |
| 5373 | // Inform the actions module about the default argument |
| 5374 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5375 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5376 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5377 | } |
| 5378 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5379 | |
| 5380 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Faisal Vali | fad9e13 | 2013-09-26 19:54:12 +0000 | [diff] [blame] | 5381 | ParmDeclarator.getIdentifierLoc(), |
| 5382 | Param, DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5383 | } |
| 5384 | |
| 5385 | // 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] | 5386 | if (Tok.isNot(tok::comma)) { |
| 5387 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5388 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5389 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5390 | if (!getLangOpts().CPlusPlus) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5391 | // We have ellipsis without a preceding ',', which is ill-formed |
| 5392 | // in C. Complain and provide the fix. |
| 5393 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5394 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5395 | } |
| 5396 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5397 | |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5398 | break; |
| 5399 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5400 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5401 | // Consume the comma. |
| 5402 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5403 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5404 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 5405 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5406 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5407 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 5408 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 5409 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 5410 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 5411 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5412 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 5413 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5414 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5415 | if (CheckProhibitedCXX11Attribute()) |
| 5416 | return; |
| 5417 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5418 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 5419 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5420 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5421 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 5422 | // This code does a fast path to handle some of the most obvious cases. |
| 5423 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5424 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5425 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5426 | MaybeParseCXX11Attributes(attrs); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5427 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5428 | // Remember that we parsed the empty array type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5429 | ExprResult NumElements; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5430 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5431 | T.getOpenLocation(), |
| 5432 | T.getCloseLocation()), |
| 5433 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5434 | return; |
| 5435 | } else if (Tok.getKind() == tok::numeric_constant && |
| 5436 | GetLookAheadToken(1).is(tok::r_square)) { |
| 5437 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5438 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5439 | ConsumeToken(); |
| 5440 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5441 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5442 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5443 | MaybeParseCXX11Attributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5444 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5445 | // Remember that we parsed a array type, and remember its features. |
Nikola Smiljanic | ebf0fa8 | 2013-01-11 08:33:05 +0000 | [diff] [blame] | 5446 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5447 | ExprRes.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5448 | T.getOpenLocation(), |
| 5449 | T.getCloseLocation()), |
| 5450 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5451 | return; |
| 5452 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5453 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5454 | // If valid, this location is the position where we read the 'static' keyword. |
| 5455 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5456 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5457 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5458 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5459 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5460 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5461 | DeclSpec DS(AttrFactory); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5462 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5463 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5464 | // If we haven't already read 'static', check to see if there is one after the |
| 5465 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5466 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5467 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5468 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5469 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 5470 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5471 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5472 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5473 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 5474 | // 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] | 5475 | // the token after the star is a ']'. Since stars in arrays are |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5476 | // infrequent, use of lookahead is not costly here. |
| 5477 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 5478 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5479 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5480 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5481 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5482 | StaticLoc = SourceLocation(); // Drop the static. |
| 5483 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5484 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5485 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5486 | // Note, in C89, this production uses the constant-expr production instead |
| 5487 | // of assignment-expr. The only difference is that assignment-expr allows |
| 5488 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 5489 | // 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] | 5490 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5491 | // Parse the constant-expression or assignment-expression now (depending |
| 5492 | // on dialect). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5493 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5494 | NumElements = ParseConstantExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5495 | } else { |
| 5496 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 5497 | Sema::ConstantEvaluated); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5498 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5499 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5500 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5501 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5502 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 5503 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 5504 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5505 | // If the expression was invalid, skip it. |
| 5506 | SkipUntil(tok::r_square); |
| 5507 | return; |
| 5508 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5509 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5510 | T.consumeClose(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5511 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5512 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5513 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5514 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5515 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5516 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5517 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5518 | NumElements.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5519 | T.getOpenLocation(), |
| 5520 | T.getCloseLocation()), |
| 5521 | attrs, T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5522 | } |
| 5523 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5524 | /// [GNU] typeof-specifier: |
| 5525 | /// typeof ( expressions ) |
| 5526 | /// typeof ( type-name ) |
| 5527 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5528 | /// |
| 5529 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5530 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5531 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5532 | SourceLocation StartLoc = ConsumeToken(); |
| 5533 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5534 | const bool hasParens = Tok.is(tok::l_paren); |
| 5535 | |
Eli Friedman | 80bfa3d | 2012-09-26 04:34:21 +0000 | [diff] [blame] | 5536 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated, |
| 5537 | Sema::ReuseLambdaContextDecl); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5538 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5539 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5540 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5541 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5542 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 5543 | CastTy, CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5544 | if (hasParens) |
| 5545 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5546 | |
| 5547 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5548 | // FIXME: Not accurate, the range gets one token more than it should. |
| 5549 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5550 | else |
| 5551 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5552 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5553 | if (isCastExpr) { |
| 5554 | if (!CastTy) { |
| 5555 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5556 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 5557 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5558 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5559 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5560 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5561 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5562 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5563 | DiagID, CastTy)) |
| 5564 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5565 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5566 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5567 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5568 | // If we get here, the operand to the typeof was an expresion. |
| 5569 | if (Operand.isInvalid()) { |
| 5570 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 5571 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5572 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5573 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5574 | // We might need to transform the operand if it is potentially evaluated. |
| 5575 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 5576 | if (Operand.isInvalid()) { |
| 5577 | DS.SetTypeSpecError(); |
| 5578 | return; |
| 5579 | } |
| 5580 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5581 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5582 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5583 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5584 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5585 | DiagID, Operand.get())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5586 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5587 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5588 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 5589 | /// [C11] atomic-specifier: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5590 | /// _Atomic ( type-name ) |
| 5591 | /// |
| 5592 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5593 | assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) && |
| 5594 | "Not an atomic specifier"); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5595 | |
| 5596 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5597 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5598 | if (T.consumeOpen()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5599 | return; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5600 | |
| 5601 | TypeResult Result = ParseTypeName(); |
| 5602 | if (Result.isInvalid()) { |
| 5603 | SkipUntil(tok::r_paren); |
| 5604 | return; |
| 5605 | } |
| 5606 | |
| 5607 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5608 | T.consumeClose(); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5609 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5610 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5611 | return; |
| 5612 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5613 | DS.setTypeofParensRange(T.getRange()); |
| 5614 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5615 | |
| 5616 | const char *PrevSpec = 0; |
| 5617 | unsigned DiagID; |
| 5618 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
| 5619 | DiagID, Result.release())) |
| 5620 | Diag(StartLoc, DiagID) << PrevSpec; |
| 5621 | } |
| 5622 | |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5623 | |
| 5624 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 5625 | /// from TryAltiVecVectorToken. |
| 5626 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 5627 | Token Next = NextToken(); |
| 5628 | switch (Next.getKind()) { |
| 5629 | default: return false; |
| 5630 | case tok::kw_short: |
| 5631 | case tok::kw_long: |
| 5632 | case tok::kw_signed: |
| 5633 | case tok::kw_unsigned: |
| 5634 | case tok::kw_void: |
| 5635 | case tok::kw_char: |
| 5636 | case tok::kw_int: |
| 5637 | case tok::kw_float: |
| 5638 | case tok::kw_double: |
| 5639 | case tok::kw_bool: |
| 5640 | case tok::kw___pixel: |
| 5641 | Tok.setKind(tok::kw___vector); |
| 5642 | return true; |
| 5643 | case tok::identifier: |
| 5644 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5645 | Tok.setKind(tok::kw___vector); |
| 5646 | return true; |
| 5647 | } |
Bill Schmidt | 3e3d20b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5648 | if (Next.getIdentifierInfo() == Ident_bool) { |
| 5649 | Tok.setKind(tok::kw___vector); |
| 5650 | return true; |
| 5651 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5652 | return false; |
| 5653 | } |
| 5654 | } |
| 5655 | |
| 5656 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 5657 | const char *&PrevSpec, unsigned &DiagID, |
| 5658 | bool &isInvalid) { |
| 5659 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 5660 | Token Next = NextToken(); |
| 5661 | switch (Next.getKind()) { |
| 5662 | case tok::kw_short: |
| 5663 | case tok::kw_long: |
| 5664 | case tok::kw_signed: |
| 5665 | case tok::kw_unsigned: |
| 5666 | case tok::kw_void: |
| 5667 | case tok::kw_char: |
| 5668 | case tok::kw_int: |
| 5669 | case tok::kw_float: |
| 5670 | case tok::kw_double: |
| 5671 | case tok::kw_bool: |
| 5672 | case tok::kw___pixel: |
| 5673 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5674 | return true; |
| 5675 | case tok::identifier: |
| 5676 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5677 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5678 | return true; |
| 5679 | } |
Bill Schmidt | 3e3d20b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5680 | if (Next.getIdentifierInfo() == Ident_bool) { |
| 5681 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5682 | return true; |
| 5683 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5684 | break; |
| 5685 | default: |
| 5686 | break; |
| 5687 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 5688 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5689 | DS.isTypeAltiVecVector()) { |
| 5690 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 5691 | return true; |
Bill Schmidt | 3e3d20b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5692 | } else if ((Tok.getIdentifierInfo() == Ident_bool) && |
| 5693 | DS.isTypeAltiVecVector()) { |
| 5694 | isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID); |
| 5695 | return true; |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5696 | } |
| 5697 | return false; |
| 5698 | } |