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)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 175 | SkipUntil(tok::r_paren, false); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 176 | SourceLocation Loc = Tok.getLocation(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 177 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 178 | SkipUntil(tok::r_paren, false); |
| 179 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 180 | if (endLoc) |
| 181 | *endLoc = Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 182 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 183 | } |
| 184 | |
Douglas Gregor | 92eb7d8 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 185 | /// \brief Determine whether the given attribute has all expression arguments. |
| 186 | static bool attributeHasExprArgs(const IdentifierInfo &II) { |
| 187 | return llvm::StringSwitch<bool>(II.getName()) |
| 188 | #include "clang/Parse/AttrExprArgs.inc" |
| 189 | .Default(false); |
| 190 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 191 | |
Richard Smith | 8edabd9 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 192 | IdentifierLoc *Parser::ParseIdentifierLoc() { |
| 193 | assert(Tok.is(tok::identifier) && "expected an identifier"); |
| 194 | IdentifierLoc *IL = IdentifierLoc::create(Actions.Context, |
| 195 | Tok.getLocation(), |
| 196 | Tok.getIdentifierInfo()); |
| 197 | ConsumeToken(); |
| 198 | return IL; |
| 199 | } |
| 200 | |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 201 | /// Parse the arguments to a parameterized GNU attribute or |
| 202 | /// a C++11 attribute in "gnu" namespace. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 203 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 204 | SourceLocation AttrNameLoc, |
| 205 | ParsedAttributes &Attrs, |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 206 | SourceLocation *EndLoc, |
| 207 | IdentifierInfo *ScopeName, |
| 208 | SourceLocation ScopeLoc, |
| 209 | AttributeList::Syntax Syntax) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 210 | |
| 211 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 212 | |
| 213 | // Availability attributes have their own grammar. |
| 214 | if (AttrName->isStr("availability")) { |
| 215 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 216 | return; |
| 217 | } |
| 218 | // Thread safety attributes fit into the FIXME case above, so we |
| 219 | // just parse the arguments as a list of expressions |
| 220 | if (IsThreadSafetyAttribute(AttrName->getName())) { |
| 221 | ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 222 | return; |
| 223 | } |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 224 | // Type safety attributes have their own grammar. |
| 225 | if (AttrName->isStr("type_tag_for_datatype")) { |
| 226 | ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 227 | return; |
| 228 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 229 | |
| 230 | ConsumeParen(); // ignore the left paren loc for now |
| 231 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 232 | bool BuiltinType = false; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 233 | ArgsVector ArgExprs; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 234 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 235 | TypeResult T; |
| 236 | SourceRange TypeRange; |
| 237 | bool TypeParsed = false; |
| 238 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 239 | switch (Tok.getKind()) { |
| 240 | case tok::kw_char: |
| 241 | case tok::kw_wchar_t: |
| 242 | case tok::kw_char16_t: |
| 243 | case tok::kw_char32_t: |
| 244 | case tok::kw_bool: |
| 245 | case tok::kw_short: |
| 246 | case tok::kw_int: |
| 247 | case tok::kw_long: |
| 248 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 249 | case tok::kw___int128: |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 250 | case tok::kw_signed: |
| 251 | case tok::kw_unsigned: |
| 252 | case tok::kw_float: |
| 253 | case tok::kw_double: |
| 254 | case tok::kw_void: |
| 255 | case tok::kw_typeof: |
| 256 | // __attribute__(( vec_type_hint(char) )) |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 257 | BuiltinType = true; |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 258 | T = ParseTypeName(&TypeRange); |
| 259 | TypeParsed = true; |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 260 | break; |
| 261 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 262 | case tok::identifier: { |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 263 | if (AttrName->isStr("vec_type_hint")) { |
| 264 | T = ParseTypeName(&TypeRange); |
| 265 | TypeParsed = true; |
| 266 | break; |
| 267 | } |
Richard Smith | 4c6c411 | 2013-09-03 18:57:36 +0000 | [diff] [blame^] | 268 | // If this attribute doesn't want an 'identifier' argument, then this |
| 269 | // argument should be parsed as an expression. |
Douglas Gregor | 92eb7d8 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 270 | if (attributeHasExprArgs(*AttrName)) |
| 271 | break; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 272 | |
Richard Smith | 8edabd9 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 273 | ArgExprs.push_back(ParseIdentifierLoc()); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 274 | } break; |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 275 | |
| 276 | default: |
| 277 | break; |
| 278 | } |
| 279 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 280 | bool isInvalid = false; |
| 281 | bool isParmType = false; |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 282 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 283 | if (!BuiltinType && !AttrName->isStr("vec_type_hint") && |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 284 | (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) { |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 285 | // Eat the comma. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 286 | if (!ArgExprs.empty()) |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 287 | ConsumeToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 288 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 289 | // Parse the non-empty comma-separated list of expressions. |
| 290 | while (1) { |
| 291 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 292 | if (ArgExpr.isInvalid()) { |
| 293 | SkipUntil(tok::r_paren); |
| 294 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 295 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 296 | ArgExprs.push_back(ArgExpr.release()); |
| 297 | if (Tok.isNot(tok::comma)) |
| 298 | break; |
| 299 | ConsumeToken(); // Eat the comma, move to the next argument |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 300 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 301 | } |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 302 | else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) { |
| 303 | if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<", |
| 304 | tok::greater)) { |
Fariborz Jahanian | b224343 | 2011-10-18 23:13:50 +0000 | [diff] [blame] | 305 | while (Tok.is(tok::identifier)) { |
| 306 | ConsumeToken(); |
| 307 | if (Tok.is(tok::greater)) |
| 308 | break; |
| 309 | if (Tok.is(tok::comma)) { |
| 310 | ConsumeToken(); |
| 311 | continue; |
| 312 | } |
| 313 | } |
| 314 | if (Tok.isNot(tok::greater)) |
| 315 | Diag(Tok, diag::err_iboutletcollection_with_protocol); |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 316 | SkipUntil(tok::r_paren, false, true); // skip until ')' |
| 317 | } |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 318 | } else if (AttrName->isStr("vec_type_hint")) { |
| 319 | if (T.get() && !T.isInvalid()) |
| 320 | isParmType = true; |
| 321 | else { |
| 322 | if (Tok.is(tok::identifier)) |
| 323 | ConsumeToken(); |
| 324 | if (TypeParsed) |
| 325 | isInvalid = true; |
| 326 | } |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 327 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 328 | |
| 329 | SourceLocation RParen = Tok.getLocation(); |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 330 | if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen) && |
| 331 | !isInvalid) { |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 332 | SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc; |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 333 | if (isParmType) { |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 334 | Attrs.addNewTypeAttr(AttrName, SourceRange(AttrLoc, RParen), ScopeName, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 335 | ScopeLoc, T.get(), Syntax); |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 336 | } else { |
| 337 | AttributeList *attr = Attrs.addNew( |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 338 | AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc, |
| 339 | ArgExprs.data(), ArgExprs.size(), Syntax); |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 340 | if (BuiltinType && |
| 341 | attr->getKind() == AttributeList::AT_IBOutletCollection) |
| 342 | Diag(Tok, diag::err_iboutletcollection_builtintype); |
| 343 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 344 | } |
| 345 | } |
| 346 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 347 | /// \brief Parses a single argument for a declspec, including the |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 348 | /// surrounding parens. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 349 | void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 350 | SourceLocation AttrNameLoc, |
| 351 | ParsedAttributes &Attrs) |
| 352 | { |
| 353 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 354 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 355 | AttrName->getNameStart(), tok::r_paren)) |
| 356 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 357 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 358 | ExprResult ArgExpr(ParseConstantExpression()); |
| 359 | if (ArgExpr.isInvalid()) { |
| 360 | T.skipToEnd(); |
| 361 | return; |
| 362 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 363 | ArgsUnion ExprList = ArgExpr.take(); |
| 364 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1, |
| 365 | AttributeList::AS_Declspec); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 366 | |
| 367 | T.consumeClose(); |
| 368 | } |
| 369 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 370 | /// \brief Determines whether a declspec is a "simple" one requiring no |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 371 | /// arguments. |
| 372 | bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) { |
| 373 | return llvm::StringSwitch<bool>(Ident->getName()) |
| 374 | .Case("dllimport", true) |
| 375 | .Case("dllexport", true) |
| 376 | .Case("noreturn", true) |
| 377 | .Case("nothrow", true) |
| 378 | .Case("noinline", true) |
| 379 | .Case("naked", true) |
| 380 | .Case("appdomain", true) |
| 381 | .Case("process", true) |
| 382 | .Case("jitintrinsic", true) |
| 383 | .Case("noalias", true) |
| 384 | .Case("restrict", true) |
| 385 | .Case("novtable", true) |
| 386 | .Case("selectany", true) |
| 387 | .Case("thread", true) |
Aaron Ballman | 3ce0de6 | 2013-05-04 16:58:37 +0000 | [diff] [blame] | 388 | .Case("safebuffers", true ) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 389 | .Default(false); |
| 390 | } |
| 391 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 392 | /// \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] | 393 | /// parameters). Will return false if we properly handled the declspec, or |
| 394 | /// true if it is an unknown declspec. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 395 | void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 396 | SourceLocation Loc, |
| 397 | ParsedAttributes &Attrs) { |
| 398 | // Try to handle the easy case first -- these declspecs all take a single |
| 399 | // parameter as their argument. |
| 400 | if (llvm::StringSwitch<bool>(Ident->getName()) |
| 401 | .Case("uuid", true) |
| 402 | .Case("align", true) |
| 403 | .Case("allocate", true) |
| 404 | .Default(false)) { |
| 405 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 406 | } else if (Ident->getName() == "deprecated") { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 407 | // The deprecated declspec has an optional single argument, so we will |
| 408 | // 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] | 409 | // not. |
| 410 | if (Tok.getKind() == tok::l_paren) |
| 411 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 412 | else |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 413 | Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 414 | } else if (Ident->getName() == "property") { |
| 415 | // 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] | 416 | // assignment expressions as a parameter, but the lhs of the assignment |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 417 | // must be named get or put. |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 418 | if (Tok.isNot(tok::l_paren)) { |
| 419 | Diag(Tok.getLocation(), diag::err_expected_lparen_after) |
| 420 | << Ident->getNameStart(); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 421 | return; |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 422 | } |
| 423 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 424 | T.expectAndConsume(diag::err_expected_lparen_after, |
| 425 | Ident->getNameStart(), tok::r_paren); |
| 426 | |
| 427 | enum AccessorKind { |
| 428 | AK_Invalid = -1, |
| 429 | AK_Put = 0, AK_Get = 1 // indices into AccessorNames |
| 430 | }; |
| 431 | IdentifierInfo *AccessorNames[] = { 0, 0 }; |
| 432 | bool HasInvalidAccessor = false; |
| 433 | |
| 434 | // Parse the accessor specifications. |
| 435 | while (true) { |
| 436 | // Stop if this doesn't look like an accessor spec. |
| 437 | if (!Tok.is(tok::identifier)) { |
| 438 | // If the user wrote a completely empty list, use a special diagnostic. |
| 439 | if (Tok.is(tok::r_paren) && !HasInvalidAccessor && |
| 440 | AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) { |
| 441 | Diag(Loc, diag::err_ms_property_no_getter_or_putter); |
| 442 | break; |
| 443 | } |
| 444 | |
| 445 | Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor); |
| 446 | break; |
| 447 | } |
| 448 | |
| 449 | AccessorKind Kind; |
| 450 | SourceLocation KindLoc = Tok.getLocation(); |
| 451 | StringRef KindStr = Tok.getIdentifierInfo()->getName(); |
| 452 | if (KindStr == "get") { |
| 453 | Kind = AK_Get; |
| 454 | } else if (KindStr == "put") { |
| 455 | Kind = AK_Put; |
| 456 | |
| 457 | // Recover from the common mistake of using 'set' instead of 'put'. |
| 458 | } else if (KindStr == "set") { |
| 459 | Diag(KindLoc, diag::err_ms_property_has_set_accessor) |
| 460 | << FixItHint::CreateReplacement(KindLoc, "put"); |
| 461 | Kind = AK_Put; |
| 462 | |
| 463 | // Handle the mistake of forgetting the accessor kind by skipping |
| 464 | // this accessor. |
| 465 | } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) { |
| 466 | Diag(KindLoc, diag::err_ms_property_missing_accessor_kind); |
| 467 | ConsumeToken(); |
| 468 | HasInvalidAccessor = true; |
| 469 | goto next_property_accessor; |
| 470 | |
| 471 | // Otherwise, complain about the unknown accessor kind. |
| 472 | } else { |
| 473 | Diag(KindLoc, diag::err_ms_property_unknown_accessor); |
| 474 | HasInvalidAccessor = true; |
| 475 | Kind = AK_Invalid; |
| 476 | |
| 477 | // Try to keep parsing unless it doesn't look like an accessor spec. |
| 478 | if (!NextToken().is(tok::equal)) break; |
| 479 | } |
| 480 | |
| 481 | // Consume the identifier. |
| 482 | ConsumeToken(); |
| 483 | |
| 484 | // Consume the '='. |
| 485 | if (Tok.is(tok::equal)) { |
| 486 | ConsumeToken(); |
| 487 | } else { |
| 488 | Diag(Tok.getLocation(), diag::err_ms_property_expected_equal) |
| 489 | << KindStr; |
| 490 | break; |
| 491 | } |
| 492 | |
| 493 | // Expect the method name. |
| 494 | if (!Tok.is(tok::identifier)) { |
| 495 | Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name); |
| 496 | break; |
| 497 | } |
| 498 | |
| 499 | if (Kind == AK_Invalid) { |
| 500 | // Just drop invalid accessors. |
| 501 | } else if (AccessorNames[Kind] != NULL) { |
| 502 | // Complain about the repeated accessor, ignore it, and keep parsing. |
| 503 | Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr; |
| 504 | } else { |
| 505 | AccessorNames[Kind] = Tok.getIdentifierInfo(); |
| 506 | } |
| 507 | ConsumeToken(); |
| 508 | |
| 509 | next_property_accessor: |
| 510 | // Keep processing accessors until we run out. |
| 511 | if (Tok.is(tok::comma)) { |
| 512 | ConsumeAnyToken(); |
| 513 | continue; |
| 514 | |
| 515 | // If we run into the ')', stop without consuming it. |
| 516 | } else if (Tok.is(tok::r_paren)) { |
| 517 | break; |
| 518 | } else { |
| 519 | Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen); |
| 520 | break; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | // Only add the property attribute if it was well-formed. |
| 525 | if (!HasInvalidAccessor) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 526 | Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(), |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 527 | AccessorNames[AK_Get], AccessorNames[AK_Put], |
| 528 | AttributeList::AS_Declspec); |
| 529 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 530 | T.skipToEnd(); |
| 531 | } else { |
| 532 | // We don't recognize this as a valid declspec, but instead of creating the |
| 533 | // attribute and allowing sema to warn about it, we will warn here instead. |
| 534 | // This is because some attributes have multiple spellings, but we need to |
| 535 | // 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] | 536 | // attribute, we'd have to split the valid declspec spelling logic into |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 537 | // both locations. |
| 538 | Diag(Loc, diag::warn_ms_declspec_unknown) << Ident; |
| 539 | |
| 540 | // If there's an open paren, we should eat the open and close parens under |
| 541 | // the assumption that this unknown declspec has parameters. |
| 542 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 543 | if (!T.consumeOpen()) |
| 544 | T.skipToEnd(); |
| 545 | } |
| 546 | } |
| 547 | |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 548 | /// [MS] decl-specifier: |
| 549 | /// __declspec ( extended-decl-modifier-seq ) |
| 550 | /// |
| 551 | /// [MS] extended-decl-modifier-seq: |
| 552 | /// extended-decl-modifier[opt] |
| 553 | /// extended-decl-modifier extended-decl-modifier-seq |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 554 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) { |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 555 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 556 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 557 | ConsumeToken(); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 558 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 559 | if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec", |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 560 | tok::r_paren)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 561 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 562 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 563 | // An empty declspec is perfectly legal and should not warn. Additionally, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 564 | // you can specify multiple attributes per declspec. |
| 565 | while (Tok.getKind() != tok::r_paren) { |
| 566 | // We expect either a well-known identifier or a generic string. Anything |
| 567 | // else is a malformed declspec. |
| 568 | bool IsString = Tok.getKind() == tok::string_literal ? true : false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 569 | if (!IsString && Tok.getKind() != tok::identifier && |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 570 | Tok.getKind() != tok::kw_restrict) { |
| 571 | Diag(Tok, diag::err_ms_declspec_type); |
| 572 | T.skipToEnd(); |
| 573 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 574 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 575 | |
| 576 | IdentifierInfo *AttrName; |
| 577 | SourceLocation AttrNameLoc; |
| 578 | if (IsString) { |
| 579 | SmallString<8> StrBuffer; |
| 580 | bool Invalid = false; |
| 581 | StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid); |
| 582 | if (Invalid) { |
| 583 | T.skipToEnd(); |
| 584 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 585 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 586 | AttrName = PP.getIdentifierInfo(Str); |
| 587 | AttrNameLoc = ConsumeStringToken(); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 588 | } else { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 589 | AttrName = Tok.getIdentifierInfo(); |
| 590 | AttrNameLoc = ConsumeToken(); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 591 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 592 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 593 | if (IsString || IsSimpleMicrosoftDeclSpec(AttrName)) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 594 | // If we have a generic string, we will allow it because there is no |
| 595 | // documented list of allowable string declspecs, but we know they exist |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 596 | // (for instance, SAL declspecs in older versions of MSVC). |
| 597 | // |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 598 | // Alternatively, if the identifier is a simple one, then it requires no |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 599 | // arguments and can be turned into an attribute directly. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 600 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 601 | AttributeList::AS_Declspec); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 602 | else |
| 603 | ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 604 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 605 | T.consumeClose(); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 606 | } |
| 607 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 608 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 609 | // Treat these like attributes |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 610 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 611 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 612 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 613 | Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) || |
| 614 | Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 615 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 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); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 619 | } |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 620 | } |
| 621 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 622 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 623 | // Treat these like attributes |
| 624 | while (Tok.is(tok::kw___pascal)) { |
| 625 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 626 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 627 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 628 | AttributeList::AS_Keyword); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 629 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 630 | } |
| 631 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 632 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 633 | // Treat these like attributes |
| 634 | while (Tok.is(tok::kw___kernel)) { |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 635 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 636 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 637 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 638 | AttributeList::AS_Keyword); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 639 | } |
| 640 | } |
| 641 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 642 | void Parser::ParseOpenCLQualifiers(DeclSpec &DS) { |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 643 | // FIXME: The mapping from attribute spelling to semantics should be |
| 644 | // performed in Sema, not here. |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 645 | SourceLocation Loc = Tok.getLocation(); |
| 646 | switch(Tok.getKind()) { |
| 647 | // OpenCL qualifiers: |
| 648 | case tok::kw___private: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 649 | case tok::kw_private: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 650 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 651 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 652 | PP.getIdentifierInfo("address_space"), Loc, 0); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 653 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 654 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 655 | case tok::kw___global: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 656 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 657 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 658 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 659 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 660 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 661 | case tok::kw___local: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 662 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 663 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 664 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 665 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 666 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 667 | case tok::kw___constant: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 668 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 669 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 670 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 671 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 672 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 673 | case tok::kw___read_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 674 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 675 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 676 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 677 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 678 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 679 | case tok::kw___write_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 680 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 681 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 682 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 683 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 684 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 685 | case tok::kw___read_write: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 686 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 687 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 688 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 689 | break; |
| 690 | default: break; |
| 691 | } |
| 692 | } |
| 693 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 694 | /// \brief Parse a version number. |
| 695 | /// |
| 696 | /// version: |
| 697 | /// simple-integer |
| 698 | /// simple-integer ',' simple-integer |
| 699 | /// simple-integer ',' simple-integer ',' simple-integer |
| 700 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 701 | Range = Tok.getLocation(); |
| 702 | |
| 703 | if (!Tok.is(tok::numeric_constant)) { |
| 704 | Diag(Tok, diag::err_expected_version); |
| 705 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 706 | return VersionTuple(); |
| 707 | } |
| 708 | |
| 709 | // Parse the major (and possibly minor and subminor) versions, which |
| 710 | // are stored in the numeric constant. We utilize a quirk of the |
| 711 | // lexer, which is that it handles something like 1.2.3 as a single |
| 712 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 713 | SmallString<512> Buffer; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 714 | Buffer.resize(Tok.getLength()+1); |
| 715 | const char *ThisTokBegin = &Buffer[0]; |
| 716 | |
| 717 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 718 | bool Invalid = false; |
| 719 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 720 | if (Invalid) |
| 721 | return VersionTuple(); |
| 722 | |
| 723 | // Parse the major version. |
| 724 | unsigned AfterMajor = 0; |
| 725 | unsigned Major = 0; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 726 | while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 727 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 728 | ++AfterMajor; |
| 729 | } |
| 730 | |
| 731 | if (AfterMajor == 0) { |
| 732 | Diag(Tok, diag::err_expected_version); |
| 733 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 734 | return VersionTuple(); |
| 735 | } |
| 736 | |
| 737 | if (AfterMajor == ActualLength) { |
| 738 | ConsumeToken(); |
| 739 | |
| 740 | // We only had a single version component. |
| 741 | if (Major == 0) { |
| 742 | Diag(Tok, diag::err_zero_version); |
| 743 | return VersionTuple(); |
| 744 | } |
| 745 | |
| 746 | return VersionTuple(Major); |
| 747 | } |
| 748 | |
| 749 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 750 | Diag(Tok, diag::err_expected_version); |
| 751 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 752 | return VersionTuple(); |
| 753 | } |
| 754 | |
| 755 | // Parse the minor version. |
| 756 | unsigned AfterMinor = AfterMajor + 1; |
| 757 | unsigned Minor = 0; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 758 | while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 759 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 760 | ++AfterMinor; |
| 761 | } |
| 762 | |
| 763 | if (AfterMinor == ActualLength) { |
| 764 | ConsumeToken(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 765 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 766 | // We had major.minor. |
| 767 | if (Major == 0 && Minor == 0) { |
| 768 | Diag(Tok, diag::err_zero_version); |
| 769 | return VersionTuple(); |
| 770 | } |
| 771 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 772 | return VersionTuple(Major, Minor); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 773 | } |
| 774 | |
| 775 | // If what follows is not a '.', we have a problem. |
| 776 | if (ThisTokBegin[AfterMinor] != '.') { |
| 777 | Diag(Tok, diag::err_expected_version); |
| 778 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 779 | return VersionTuple(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | // Parse the subminor version. |
| 783 | unsigned AfterSubminor = AfterMinor + 1; |
| 784 | unsigned Subminor = 0; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 785 | while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 786 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 787 | ++AfterSubminor; |
| 788 | } |
| 789 | |
| 790 | if (AfterSubminor != ActualLength) { |
| 791 | Diag(Tok, diag::err_expected_version); |
| 792 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 793 | return VersionTuple(); |
| 794 | } |
| 795 | ConsumeToken(); |
| 796 | return VersionTuple(Major, Minor, Subminor); |
| 797 | } |
| 798 | |
| 799 | /// \brief Parse the contents of the "availability" attribute. |
| 800 | /// |
| 801 | /// availability-attribute: |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 802 | /// 'availability' '(' platform ',' version-arg-list, opt-message')' |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 803 | /// |
| 804 | /// platform: |
| 805 | /// identifier |
| 806 | /// |
| 807 | /// version-arg-list: |
| 808 | /// version-arg |
| 809 | /// version-arg ',' version-arg-list |
| 810 | /// |
| 811 | /// version-arg: |
| 812 | /// 'introduced' '=' version |
| 813 | /// 'deprecated' '=' version |
Douglas Gregor | 93a7067 | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 814 | /// 'obsoleted' = version |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 815 | /// 'unavailable' |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 816 | /// opt-message: |
| 817 | /// 'message' '=' <string> |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 818 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 819 | SourceLocation AvailabilityLoc, |
| 820 | ParsedAttributes &attrs, |
| 821 | SourceLocation *endLoc) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 822 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 823 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 824 | ExprResult MessageExpr; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 825 | |
| 826 | // Opening '('. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 827 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 828 | if (T.consumeOpen()) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 829 | Diag(Tok, diag::err_expected_lparen); |
| 830 | return; |
| 831 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 832 | |
| 833 | // Parse the platform name, |
| 834 | if (Tok.isNot(tok::identifier)) { |
| 835 | Diag(Tok, diag::err_availability_expected_platform); |
| 836 | SkipUntil(tok::r_paren); |
| 837 | return; |
| 838 | } |
Richard Smith | 8edabd9 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 839 | IdentifierLoc *Platform = ParseIdentifierLoc(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 840 | |
| 841 | // Parse the ',' following the platform name. |
| 842 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren)) |
| 843 | return; |
| 844 | |
| 845 | // If we haven't grabbed the pointers for the identifiers |
| 846 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 847 | if (!Ident_introduced) { |
| 848 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 849 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 850 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 851 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 852 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 856 | SourceLocation UnavailableLoc; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 857 | do { |
| 858 | if (Tok.isNot(tok::identifier)) { |
| 859 | Diag(Tok, diag::err_availability_expected_change); |
| 860 | SkipUntil(tok::r_paren); |
| 861 | return; |
| 862 | } |
| 863 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 864 | SourceLocation KeywordLoc = ConsumeToken(); |
| 865 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 866 | if (Keyword == Ident_unavailable) { |
| 867 | if (UnavailableLoc.isValid()) { |
| 868 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 869 | << Keyword << SourceRange(UnavailableLoc); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 870 | } |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 871 | UnavailableLoc = KeywordLoc; |
| 872 | |
| 873 | if (Tok.isNot(tok::comma)) |
| 874 | break; |
| 875 | |
| 876 | ConsumeToken(); |
| 877 | continue; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 880 | if (Tok.isNot(tok::equal)) { |
| 881 | Diag(Tok, diag::err_expected_equal_after) |
| 882 | << Keyword; |
| 883 | SkipUntil(tok::r_paren); |
| 884 | return; |
| 885 | } |
| 886 | ConsumeToken(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 887 | if (Keyword == Ident_message) { |
| 888 | if (!isTokenStringLiteral()) { |
Andy Gibbs | 97f8461 | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 889 | Diag(Tok, diag::err_expected_string_literal) |
| 890 | << /*Source='availability attribute'*/2; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 891 | SkipUntil(tok::r_paren); |
| 892 | return; |
| 893 | } |
| 894 | MessageExpr = ParseStringLiteralExpression(); |
| 895 | break; |
| 896 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 897 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 898 | SourceRange VersionRange; |
| 899 | VersionTuple Version = ParseVersionTuple(VersionRange); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 900 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 901 | if (Version.empty()) { |
| 902 | SkipUntil(tok::r_paren); |
| 903 | return; |
| 904 | } |
| 905 | |
| 906 | unsigned Index; |
| 907 | if (Keyword == Ident_introduced) |
| 908 | Index = Introduced; |
| 909 | else if (Keyword == Ident_deprecated) |
| 910 | Index = Deprecated; |
| 911 | else if (Keyword == Ident_obsoleted) |
| 912 | Index = Obsoleted; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 913 | else |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 914 | Index = Unknown; |
| 915 | |
| 916 | if (Index < Unknown) { |
| 917 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 918 | Diag(KeywordLoc, diag::err_availability_redundant) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 919 | << Keyword |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 920 | << SourceRange(Changes[Index].KeywordLoc, |
| 921 | Changes[Index].VersionRange.getEnd()); |
| 922 | } |
| 923 | |
| 924 | Changes[Index].KeywordLoc = KeywordLoc; |
| 925 | Changes[Index].Version = Version; |
| 926 | Changes[Index].VersionRange = VersionRange; |
| 927 | } else { |
| 928 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 929 | << Keyword << VersionRange; |
| 930 | } |
| 931 | |
| 932 | if (Tok.isNot(tok::comma)) |
| 933 | break; |
| 934 | |
| 935 | ConsumeToken(); |
| 936 | } while (true); |
| 937 | |
| 938 | // Closing ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 939 | if (T.consumeClose()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 940 | return; |
| 941 | |
| 942 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 943 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 944 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 945 | // The 'unavailable' availability cannot be combined with any other |
| 946 | // availability changes. Make sure that hasn't happened. |
| 947 | if (UnavailableLoc.isValid()) { |
| 948 | bool Complained = false; |
| 949 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 950 | if (Changes[Index].KeywordLoc.isValid()) { |
| 951 | if (!Complained) { |
| 952 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 953 | << SourceRange(Changes[Index].KeywordLoc, |
| 954 | Changes[Index].VersionRange.getEnd()); |
| 955 | Complained = true; |
| 956 | } |
| 957 | |
| 958 | // Clear out the availability. |
| 959 | Changes[Index] = AvailabilityChange(); |
| 960 | } |
| 961 | } |
| 962 | } |
| 963 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 964 | // Record this attribute |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 965 | attrs.addNew(&Availability, |
| 966 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Fariborz Jahanian | f96708d | 2012-01-23 23:38:32 +0000 | [diff] [blame] | 967 | 0, AvailabilityLoc, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 968 | Platform, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 969 | Changes[Introduced], |
| 970 | Changes[Deprecated], |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 971 | Changes[Obsoleted], |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 972 | UnavailableLoc, MessageExpr.take(), |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 973 | AttributeList::AS_GNU); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 974 | } |
| 975 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 976 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 977 | // Late Parsed Attributes: |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 978 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 979 | |
| 980 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 981 | |
| 982 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 983 | Self->ParseLexedAttributes(*Class); |
| 984 | } |
| 985 | |
| 986 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 987 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 991 | /// scope appropriately. |
| 992 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 993 | // Deal with templates |
| 994 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 995 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 996 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 997 | HasTemplateScope); |
| 998 | if (HasTemplateScope) |
| 999 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 1000 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1001 | // Set or update the scope flags. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1002 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1003 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1004 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 1005 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 1006 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1007 | // Enter the scope of nested classes |
| 1008 | if (!AlreadyHasClassScope) |
| 1009 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 1010 | Class.TagOrTemplate); |
Benjamin Kramer | 268efba | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 1011 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1012 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 1013 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 1014 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1015 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1016 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1017 | if (!AlreadyHasClassScope) |
| 1018 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 1019 | Class.TagOrTemplate); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1022 | |
| 1023 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 1024 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 1025 | bool EnterScope, bool OnDefinition) { |
DeLesley Hutchins | 161db02 | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1026 | assert(LAs.parseSoon() && |
| 1027 | "Attribute list should be marked for immediate parsing."); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1028 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 95526a4 | 2012-08-15 22:41:04 +0000 | [diff] [blame] | 1029 | if (D) |
| 1030 | LAs[i]->addDecl(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1031 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | d306cf7 | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 1032 | delete LAs[i]; |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1033 | } |
| 1034 | LAs.clear(); |
| 1035 | } |
| 1036 | |
| 1037 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1038 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 1039 | /// This will be called at the end of parsing a class declaration |
| 1040 | /// for each LateParsedAttribute. We consume the saved tokens and |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1041 | /// create an attribute with the arguments filled in. We add this |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1042 | /// to the Attribute list for the decl. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1043 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 1044 | bool EnterScope, bool OnDefinition) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1045 | // Save the current token position. |
| 1046 | SourceLocation OrigLoc = Tok.getLocation(); |
| 1047 | |
| 1048 | // Append the current token at the end of the new token stream so that it |
| 1049 | // doesn't get lost. |
| 1050 | LA.Toks.push_back(Tok); |
| 1051 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 1052 | // Consume the previously pushed token. |
Argyrios Kyrtzidis | ab2d09b | 2013-03-27 23:58:17 +0000 | [diff] [blame] | 1053 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1054 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1055 | if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) { |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1056 | // FIXME: Do not warn on C++11 attributes, once we start supporting |
| 1057 | // them here. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1058 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 1059 | << LA.AttrName.getName(); |
| 1060 | } |
| 1061 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1062 | ParsedAttributes Attrs(AttrFactory); |
| 1063 | SourceLocation endLoc; |
| 1064 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1065 | if (LA.Decls.size() > 0) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1066 | Decl *D = LA.Decls[0]; |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1067 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1068 | RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1069 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1070 | // Allow 'this' within late-parsed attributes. |
Richard Smith | cafeb94 | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 1071 | Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0, |
| 1072 | ND && ND->isCXXInstanceMember()); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1073 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1074 | if (LA.Decls.size() == 1) { |
| 1075 | // If the Decl is templatized, add template parameters to scope. |
| 1076 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 1077 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 1078 | if (HasTemplateScope) |
| 1079 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1080 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1081 | // If the Decl is on a function, add function parameters to the scope. |
| 1082 | bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
| 1083 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope); |
| 1084 | if (HasFunScope) |
| 1085 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1086 | |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1087 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 1088 | 0, SourceLocation(), AttributeList::AS_GNU); |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1089 | |
| 1090 | if (HasFunScope) { |
| 1091 | Actions.ActOnExitFunctionContext(); |
| 1092 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 1093 | } |
| 1094 | if (HasTemplateScope) { |
| 1095 | TempScope.Exit(); |
| 1096 | } |
| 1097 | } else { |
| 1098 | // If there are multiple decls, then the decl cannot be within the |
| 1099 | // function scope. |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1100 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 1101 | 0, SourceLocation(), AttributeList::AS_GNU); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1102 | } |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 1103 | } else { |
| 1104 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1107 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) { |
| 1108 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
| 1109 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1110 | |
| 1111 | if (Tok.getLocation() != OrigLoc) { |
| 1112 | // Due to a parsing error, we either went over the cached tokens or |
| 1113 | // there are still cached tokens left, so we skip the leftover tokens. |
| 1114 | // Since this is an uncommon situation that should be avoided, use the |
| 1115 | // expensive isBeforeInTranslationUnit call. |
| 1116 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 1117 | OrigLoc)) |
| 1118 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
Douglas Gregor | d78ef5b | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 1119 | ConsumeAnyToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1120 | } |
| 1121 | } |
| 1122 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1123 | /// \brief Wrapper around a case statement checking if AttrName is |
| 1124 | /// one of the thread safety attributes |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1125 | bool Parser::IsThreadSafetyAttribute(StringRef AttrName) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1126 | return llvm::StringSwitch<bool>(AttrName) |
| 1127 | .Case("guarded_by", true) |
| 1128 | .Case("guarded_var", true) |
| 1129 | .Case("pt_guarded_by", true) |
| 1130 | .Case("pt_guarded_var", true) |
| 1131 | .Case("lockable", true) |
| 1132 | .Case("scoped_lockable", true) |
| 1133 | .Case("no_thread_safety_analysis", true) |
| 1134 | .Case("acquired_after", true) |
| 1135 | .Case("acquired_before", true) |
| 1136 | .Case("exclusive_lock_function", true) |
| 1137 | .Case("shared_lock_function", true) |
| 1138 | .Case("exclusive_trylock_function", true) |
| 1139 | .Case("shared_trylock_function", true) |
| 1140 | .Case("unlock_function", true) |
| 1141 | .Case("lock_returned", true) |
| 1142 | .Case("locks_excluded", true) |
| 1143 | .Case("exclusive_locks_required", true) |
| 1144 | .Case("shared_locks_required", true) |
| 1145 | .Default(false); |
| 1146 | } |
| 1147 | |
| 1148 | /// \brief Parse the contents of thread safety attributes. These |
| 1149 | /// should always be parsed as an expression list. |
| 1150 | /// |
| 1151 | /// We need to special case the parsing due to the fact that if the first token |
| 1152 | /// of the first argument is an identifier, the main parse loop will store |
| 1153 | /// that token as a "parameter" and the rest of |
| 1154 | /// the arguments will be added to a list of "arguments". However, |
| 1155 | /// subsequent tokens in the first argument are lost. We instead parse each |
| 1156 | /// argument as an expression and add all arguments to the list of "arguments". |
| 1157 | /// In future, we will take advantage of this special case to also |
| 1158 | /// deal with some argument scoping issues here (for example, referring to a |
| 1159 | /// function parameter in the attribute on that function). |
| 1160 | void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName, |
| 1161 | SourceLocation AttrNameLoc, |
| 1162 | ParsedAttributes &Attrs, |
| 1163 | SourceLocation *EndLoc) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1164 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1165 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1166 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1167 | T.consumeOpen(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1168 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1169 | ArgsVector ArgExprs; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1170 | bool ArgExprsOk = true; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1171 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1172 | // now parse the list of expressions |
DeLesley Hutchins | 4805f15 | 2011-12-14 19:36:06 +0000 | [diff] [blame] | 1173 | while (Tok.isNot(tok::r_paren)) { |
DeLesley Hutchins | ed4330b | 2013-02-07 19:01:07 +0000 | [diff] [blame] | 1174 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1175 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 1176 | if (ArgExpr.isInvalid()) { |
| 1177 | ArgExprsOk = false; |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1178 | T.consumeClose(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1179 | break; |
| 1180 | } else { |
| 1181 | ArgExprs.push_back(ArgExpr.release()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1182 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1183 | if (Tok.isNot(tok::comma)) |
| 1184 | break; |
| 1185 | ConsumeToken(); // Eat the comma, move to the next argument |
| 1186 | } |
| 1187 | // Match the ')'. |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 1188 | if (ArgExprsOk && !T.consumeClose()) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1189 | Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, ArgExprs.data(), |
| 1190 | ArgExprs.size(), AttributeList::AS_GNU); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1191 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1192 | if (EndLoc) |
| 1193 | *EndLoc = T.getCloseLocation(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1196 | void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName, |
| 1197 | SourceLocation AttrNameLoc, |
| 1198 | ParsedAttributes &Attrs, |
| 1199 | SourceLocation *EndLoc) { |
| 1200 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 1201 | |
| 1202 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1203 | T.consumeOpen(); |
| 1204 | |
| 1205 | if (Tok.isNot(tok::identifier)) { |
| 1206 | Diag(Tok, diag::err_expected_ident); |
| 1207 | T.skipToEnd(); |
| 1208 | return; |
| 1209 | } |
Richard Smith | 8edabd9 | 2013-09-03 18:01:40 +0000 | [diff] [blame] | 1210 | IdentifierLoc *ArgumentKind = ParseIdentifierLoc(); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1211 | |
| 1212 | if (Tok.isNot(tok::comma)) { |
| 1213 | Diag(Tok, diag::err_expected_comma); |
| 1214 | T.skipToEnd(); |
| 1215 | return; |
| 1216 | } |
| 1217 | ConsumeToken(); |
| 1218 | |
| 1219 | SourceRange MatchingCTypeRange; |
| 1220 | TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange); |
| 1221 | if (MatchingCType.isInvalid()) { |
| 1222 | T.skipToEnd(); |
| 1223 | return; |
| 1224 | } |
| 1225 | |
| 1226 | bool LayoutCompatible = false; |
| 1227 | bool MustBeNull = false; |
| 1228 | while (Tok.is(tok::comma)) { |
| 1229 | ConsumeToken(); |
| 1230 | if (Tok.isNot(tok::identifier)) { |
| 1231 | Diag(Tok, diag::err_expected_ident); |
| 1232 | T.skipToEnd(); |
| 1233 | return; |
| 1234 | } |
| 1235 | IdentifierInfo *Flag = Tok.getIdentifierInfo(); |
| 1236 | if (Flag->isStr("layout_compatible")) |
| 1237 | LayoutCompatible = true; |
| 1238 | else if (Flag->isStr("must_be_null")) |
| 1239 | MustBeNull = true; |
| 1240 | else { |
| 1241 | Diag(Tok, diag::err_type_safety_unknown_flag) << Flag; |
| 1242 | T.skipToEnd(); |
| 1243 | return; |
| 1244 | } |
| 1245 | ConsumeToken(); // consume flag |
| 1246 | } |
| 1247 | |
| 1248 | if (!T.consumeClose()) { |
| 1249 | Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 1250 | ArgumentKind, MatchingCType.release(), |
| 1251 | LayoutCompatible, MustBeNull, |
| 1252 | AttributeList::AS_GNU); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | if (EndLoc) |
| 1256 | *EndLoc = T.getCloseLocation(); |
| 1257 | } |
| 1258 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1259 | /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets |
| 1260 | /// of a C++11 attribute-specifier in a location where an attribute is not |
| 1261 | /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this |
| 1262 | /// situation. |
| 1263 | /// |
| 1264 | /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if |
| 1265 | /// this doesn't appear to actually be an attribute-specifier, and the caller |
| 1266 | /// should try to parse it. |
| 1267 | bool Parser::DiagnoseProhibitedCXX11Attribute() { |
| 1268 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)); |
| 1269 | |
| 1270 | switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) { |
| 1271 | case CAK_NotAttributeSpecifier: |
| 1272 | // No diagnostic: we're in Obj-C++11 and this is not actually an attribute. |
| 1273 | return false; |
| 1274 | |
| 1275 | case CAK_InvalidAttributeSpecifier: |
| 1276 | Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute); |
| 1277 | return false; |
| 1278 | |
| 1279 | case CAK_AttributeSpecifier: |
| 1280 | // Parse and discard the attributes. |
| 1281 | SourceLocation BeginLoc = ConsumeBracket(); |
| 1282 | ConsumeBracket(); |
| 1283 | SkipUntil(tok::r_square, /*StopAtSemi*/ false); |
| 1284 | assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied"); |
| 1285 | SourceLocation EndLoc = ConsumeBracket(); |
| 1286 | Diag(BeginLoc, diag::err_attributes_not_allowed) |
| 1287 | << SourceRange(BeginLoc, EndLoc); |
| 1288 | return true; |
| 1289 | } |
Chandler Carruth | 2c6dbd7 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 1290 | llvm_unreachable("All cases handled above."); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Richard Smith | 975d52c | 2013-02-20 01:17:14 +0000 | [diff] [blame] | 1293 | /// \brief We have found the opening square brackets of a C++11 |
| 1294 | /// attribute-specifier in a location where an attribute is not permitted, but |
| 1295 | /// we know where the attributes ought to be written. Parse them anyway, and |
| 1296 | /// provide a fixit moving them to the right place. |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1297 | void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs, |
| 1298 | SourceLocation CorrectLocation) { |
| 1299 | assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) || |
| 1300 | Tok.is(tok::kw_alignas)); |
| 1301 | |
| 1302 | // Consume the attributes. |
| 1303 | SourceLocation Loc = Tok.getLocation(); |
| 1304 | ParseCXX11Attributes(Attrs); |
| 1305 | CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true); |
| 1306 | |
| 1307 | Diag(Loc, diag::err_attributes_not_allowed) |
| 1308 | << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange) |
| 1309 | << FixItHint::CreateRemoval(AttrRange); |
| 1310 | } |
| 1311 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1312 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 1313 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 1314 | << attrs.Range; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1317 | void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) { |
| 1318 | AttributeList *AttrList = attrs.getList(); |
| 1319 | while (AttrList) { |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1320 | if (AttrList->isCXX11Attribute()) { |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 1321 | Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr) |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1322 | << AttrList->getName(); |
| 1323 | AttrList->setInvalid(); |
| 1324 | } |
| 1325 | AttrList = AttrList->getNext(); |
| 1326 | } |
| 1327 | } |
| 1328 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1329 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 1330 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1331 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 1332 | /// location of the semicolon in DeclEnd. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1333 | /// |
| 1334 | /// declaration: [C99 6.7] |
| 1335 | /// block-declaration -> |
| 1336 | /// simple-declaration |
| 1337 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1338 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1339 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1340 | /// [C++] using-directive |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1341 | /// [C++] using-declaration |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1342 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1343 | /// others... [FIXME] |
| 1344 | /// |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1345 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 1346 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1347 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1348 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 1349 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | e8cff36 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 1350 | // Must temporarily exit the objective-c container scope for |
| 1351 | // parsing c none objective-c decls. |
| 1352 | ObjCDeclContextSwitch ObjCDC(*this); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1353 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1354 | Decl *SingleDecl = 0; |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1355 | Decl *OwnedType = 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1356 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1357 | case tok::kw_template: |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1358 | case tok::kw_export: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1359 | ProhibitAttributes(attrs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1360 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1361 | break; |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1362 | case tok::kw_inline: |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 1363 | // 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] | 1364 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1365 | ProhibitAttributes(attrs); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1366 | SourceLocation InlineLoc = ConsumeToken(); |
| 1367 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 1368 | break; |
| 1369 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1370 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1371 | true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1372 | case tok::kw_namespace: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1373 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1374 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1375 | break; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1376 | case tok::kw_using: |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1377 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1378 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1379 | break; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1380 | case tok::kw_static_assert: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1381 | case tok::kw__Static_assert: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1382 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1383 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1384 | break; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1385 | default: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1386 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1387 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1388 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1389 | // 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] | 1390 | // single decl, convert it now. Alias declarations can also declare a type; |
| 1391 | // include that too if it is present. |
| 1392 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
| 1395 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1396 | /// declaration-specifiers init-declarator-list[opt] ';' |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1397 | /// [C++11] attribute-specifier-seq decl-specifier-seq[opt] |
| 1398 | /// init-declarator-list ';' |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1399 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 1400 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1401 | /// |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1402 | /// for-range-declaration: [C++11 6.5p1: stmt.ranged] |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1403 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1404 | /// |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1405 | /// 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] | 1406 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1407 | /// |
| 1408 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 1409 | /// of a simple-declaration. If we find that we are, we also parse the |
| 1410 | /// for-range-initializer, and place it here. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1411 | Parser::DeclGroupPtrTy |
| 1412 | Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context, |
| 1413 | SourceLocation &DeclEnd, |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1414 | ParsedAttributesWithRange &Attrs, |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1415 | bool RequireSemi, ForRangeInit *FRI) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1416 | // Parse the common declaration-specifiers piece. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1417 | ParsingDeclSpec DS(*this); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1418 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1419 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1420 | getDeclSpecContextFromDeclaratorContext(Context)); |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1421 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1422 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1423 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1424 | if (Tok.is(tok::semi)) { |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1425 | ProhibitAttributes(Attrs); |
Argyrios Kyrtzidis | 5641b0d | 2012-05-16 23:49:15 +0000 | [diff] [blame] | 1426 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1427 | if (RequireSemi) ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1428 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1429 | DS); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1430 | DS.complete(TheDecl); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1431 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1432 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1433 | |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1434 | DS.takeAttributesFrom(Attrs); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1435 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1436 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1437 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1438 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1439 | /// for a declarator. |
| 1440 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1441 | switch (Tok.getKind()) { |
| 1442 | case tok::annot_cxxscope: |
| 1443 | case tok::annot_template_id: |
| 1444 | case tok::caret: |
| 1445 | case tok::code_completion: |
| 1446 | case tok::coloncolon: |
| 1447 | case tok::ellipsis: |
| 1448 | case tok::kw___attribute: |
| 1449 | case tok::kw_operator: |
| 1450 | case tok::l_paren: |
| 1451 | case tok::star: |
| 1452 | return true; |
| 1453 | |
| 1454 | case tok::amp: |
| 1455 | case tok::ampamp: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1456 | return getLangOpts().CPlusPlus; |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1457 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1458 | 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] | 1459 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 && |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1460 | NextToken().is(tok::l_square); |
| 1461 | |
| 1462 | 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] | 1463 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1464 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1465 | case tok::identifier: |
| 1466 | switch (NextToken().getKind()) { |
| 1467 | case tok::code_completion: |
| 1468 | case tok::coloncolon: |
| 1469 | case tok::comma: |
| 1470 | case tok::equal: |
| 1471 | case tok::equalequal: // Might be a typo for '='. |
| 1472 | case tok::kw_alignas: |
| 1473 | case tok::kw_asm: |
| 1474 | case tok::kw___attribute: |
| 1475 | case tok::l_brace: |
| 1476 | case tok::l_paren: |
| 1477 | case tok::l_square: |
| 1478 | case tok::less: |
| 1479 | case tok::r_brace: |
| 1480 | case tok::r_paren: |
| 1481 | case tok::r_square: |
| 1482 | case tok::semi: |
| 1483 | return true; |
| 1484 | |
| 1485 | case tok::colon: |
| 1486 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1487 | // and in block scope it's probably a label. Inside a class definition, |
| 1488 | // this is a bit-field. |
| 1489 | return Context == Declarator::MemberContext || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1490 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1491 | |
| 1492 | case tok::identifier: // Possible virt-specifier. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1493 | return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken()); |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1494 | |
| 1495 | default: |
| 1496 | return false; |
| 1497 | } |
| 1498 | |
| 1499 | default: |
| 1500 | return false; |
| 1501 | } |
| 1502 | } |
| 1503 | |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1504 | /// Skip until we reach something which seems like a sensible place to pick |
| 1505 | /// up parsing after a malformed declaration. This will sometimes stop sooner |
| 1506 | /// than SkipUntil(tok::r_brace) would, but will never stop later. |
| 1507 | void Parser::SkipMalformedDecl() { |
| 1508 | while (true) { |
| 1509 | switch (Tok.getKind()) { |
| 1510 | case tok::l_brace: |
| 1511 | // Skip until matching }, then stop. We've probably skipped over |
| 1512 | // a malformed class or function definition or similar. |
| 1513 | ConsumeBrace(); |
| 1514 | SkipUntil(tok::r_brace, /*StopAtSemi*/false); |
| 1515 | if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) { |
| 1516 | // This declaration isn't over yet. Keep skipping. |
| 1517 | continue; |
| 1518 | } |
| 1519 | if (Tok.is(tok::semi)) |
| 1520 | ConsumeToken(); |
| 1521 | return; |
| 1522 | |
| 1523 | case tok::l_square: |
| 1524 | ConsumeBracket(); |
| 1525 | SkipUntil(tok::r_square, /*StopAtSemi*/false); |
| 1526 | continue; |
| 1527 | |
| 1528 | case tok::l_paren: |
| 1529 | ConsumeParen(); |
| 1530 | SkipUntil(tok::r_paren, /*StopAtSemi*/false); |
| 1531 | continue; |
| 1532 | |
| 1533 | case tok::r_brace: |
| 1534 | return; |
| 1535 | |
| 1536 | case tok::semi: |
| 1537 | ConsumeToken(); |
| 1538 | return; |
| 1539 | |
| 1540 | case tok::kw_inline: |
| 1541 | // 'inline namespace' at the start of a line is almost certainly |
Jordan Rose | 94f29f4 | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1542 | // a good place to pick back up parsing, except in an Objective-C |
| 1543 | // @interface context. |
| 1544 | if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) && |
| 1545 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1546 | return; |
| 1547 | break; |
| 1548 | |
| 1549 | case tok::kw_namespace: |
| 1550 | // 'namespace' at the start of a line is almost certainly a good |
Jordan Rose | 94f29f4 | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1551 | // place to pick back up parsing, except in an Objective-C |
| 1552 | // @interface context. |
| 1553 | if (Tok.isAtStartOfLine() && |
| 1554 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
| 1555 | return; |
| 1556 | break; |
| 1557 | |
| 1558 | case tok::at: |
| 1559 | // @end is very much like } in Objective-C contexts. |
| 1560 | if (NextToken().isObjCAtKeyword(tok::objc_end) && |
| 1561 | ParsingInObjCContainer) |
| 1562 | return; |
| 1563 | break; |
| 1564 | |
| 1565 | case tok::minus: |
| 1566 | case tok::plus: |
| 1567 | // - and + probably start new method declarations in Objective-C contexts. |
| 1568 | if (Tok.isAtStartOfLine() && ParsingInObjCContainer) |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1569 | return; |
| 1570 | break; |
| 1571 | |
| 1572 | case tok::eof: |
| 1573 | return; |
| 1574 | |
| 1575 | default: |
| 1576 | break; |
| 1577 | } |
| 1578 | |
| 1579 | ConsumeAnyToken(); |
| 1580 | } |
| 1581 | } |
| 1582 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1583 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1584 | /// definition or a group of object declarations, actually parse the |
| 1585 | /// result. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1586 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1587 | unsigned Context, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1588 | bool AllowFunctionDefinitions, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1589 | SourceLocation *DeclEnd, |
| 1590 | ForRangeInit *FRI) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1591 | // Parse the first declarator. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1592 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1593 | ParseDeclarator(D); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1594 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1595 | // Bail out if the first declarator didn't seem well-formed. |
| 1596 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1597 | SkipMalformedDecl(); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1598 | return DeclGroupPtrTy(); |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1599 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1600 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1601 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1602 | // appropriate function scope after the function Decl has been constructed. |
DeLesley Hutchins | 161db02 | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1603 | // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList. |
| 1604 | LateParsedAttrList LateParsedAttrs(true); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1605 | if (D.isFunctionDeclarator()) |
| 1606 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1607 | |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1608 | // 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] | 1609 | if (D.isFunctionDeclarator() && |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1610 | // Look at the next token to make sure that this isn't a function |
| 1611 | // declaration. We have to check this because __attribute__ might be the |
| 1612 | // start of a function definition in GCC-extended K&R C. |
Fariborz Jahanian | be1d4ec | 2012-08-10 15:54:40 +0000 | [diff] [blame] | 1613 | !isDeclarationAfterDeclarator()) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1614 | |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1615 | if (AllowFunctionDefinitions) { |
| 1616 | if (isStartOfFunctionDefinition(D)) { |
| 1617 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1618 | Diag(Tok, diag::err_function_declared_typedef); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1619 | |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1620 | // Recover by treating the 'typedef' as spurious. |
| 1621 | DS.ClearStorageClassSpecs(); |
| 1622 | } |
| 1623 | |
| 1624 | Decl *TheDecl = |
| 1625 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
| 1626 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1627 | } |
| 1628 | |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1629 | if (isDeclarationSpecifier()) { |
| 1630 | // If there is an invalid declaration specifier right after the function |
| 1631 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1632 | // actually a body. Just fall through into the code that handles it as a |
| 1633 | // prototype, and let the top-level code handle the erroneous declspec |
| 1634 | // where it would otherwise expect a comma or semicolon. |
| 1635 | } else { |
| 1636 | Diag(Tok, diag::err_expected_fn_body); |
| 1637 | SkipUntil(tok::semi); |
| 1638 | return DeclGroupPtrTy(); |
| 1639 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1640 | } else { |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1641 | if (Tok.is(tok::l_brace)) { |
| 1642 | Diag(Tok, diag::err_function_definition_not_allowed); |
| 1643 | SkipUntil(tok::r_brace, true, true); |
| 1644 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1645 | } |
| 1646 | } |
| 1647 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1648 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1649 | return DeclGroupPtrTy(); |
| 1650 | |
| 1651 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1652 | // must parse and analyze the for-range-initializer before the declaration is |
| 1653 | // analyzed. |
Douglas Gregor | 12849d0 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1654 | // |
| 1655 | // Handle the Objective-C for-in loop variable similarly, although we |
| 1656 | // don't need to parse the container in advance. |
| 1657 | if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) { |
| 1658 | bool IsForRangeLoop = false; |
| 1659 | if (Tok.is(tok::colon)) { |
| 1660 | IsForRangeLoop = true; |
| 1661 | FRI->ColonLoc = ConsumeToken(); |
| 1662 | if (Tok.is(tok::l_brace)) |
| 1663 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1664 | else |
| 1665 | FRI->RangeExpr = ParseExpression(); |
| 1666 | } |
| 1667 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1668 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | 12849d0 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1669 | if (IsForRangeLoop) |
| 1670 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1671 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | 6895a64 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 1672 | D.complete(ThisDecl); |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1673 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1674 | } |
| 1675 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1676 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1677 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1678 | if (LateParsedAttrs.size() > 0) |
| 1679 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1680 | D.complete(FirstDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1681 | if (FirstDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1682 | DeclsInGroup.push_back(FirstDecl); |
| 1683 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1684 | bool ExpectSemi = Context != Declarator::ForContext; |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 1685 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1686 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1687 | // error, bail out. |
| 1688 | while (Tok.is(tok::comma)) { |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1689 | SourceLocation CommaLoc = ConsumeToken(); |
| 1690 | |
| 1691 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1692 | // This comma was followed by a line-break and something which can't be |
| 1693 | // the start of a declarator. The comma was probably a typo for a |
| 1694 | // semicolon. |
| 1695 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1696 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1697 | ExpectSemi = false; |
| 1698 | break; |
| 1699 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1700 | |
| 1701 | // Parse the next declarator. |
| 1702 | D.clear(); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1703 | D.setCommaLoc(CommaLoc); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1704 | |
| 1705 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1706 | // declaration, these would be part of the declspec. In subsequent |
| 1707 | // declarators, they become part of the declarator itself, so that they |
| 1708 | // don't apply to declarators after *this* one. Examples: |
| 1709 | // short __attribute__((common)) var; -> declspec |
| 1710 | // short var __attribute__((common)); -> declarator |
| 1711 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1712 | MaybeParseGNUAttributes(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1713 | |
| 1714 | ParseDeclarator(D); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1715 | if (!D.isInvalidType()) { |
| 1716 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1717 | D.complete(ThisDecl); |
| 1718 | if (ThisDecl) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1719 | DeclsInGroup.push_back(ThisDecl); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1720 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | if (DeclEnd) |
| 1724 | *DeclEnd = Tok.getLocation(); |
| 1725 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1726 | if (ExpectSemi && |
Chris Lattner | 8bb21d3 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 1727 | ExpectAndConsumeSemi(Context == Declarator::FileContext |
| 1728 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1729 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1730 | // Okay, there was no semicolon and one was expected. If we see a |
| 1731 | // declaration specifier, just assume it was missing and continue parsing. |
| 1732 | // Otherwise things are very confused and we skip to recover. |
| 1733 | if (!isDeclarationSpecifier()) { |
| 1734 | SkipUntil(tok::r_brace, true, true); |
| 1735 | if (Tok.is(tok::semi)) |
| 1736 | ConsumeToken(); |
| 1737 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1738 | } |
| 1739 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1740 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1741 | } |
| 1742 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1743 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1744 | /// declarator. Returns true on an error. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1745 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1746 | // If a simple-asm-expr is present, parse it. |
| 1747 | if (Tok.is(tok::kw_asm)) { |
| 1748 | SourceLocation Loc; |
| 1749 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1750 | if (AsmLabel.isInvalid()) { |
| 1751 | SkipUntil(tok::semi, true, true); |
| 1752 | return true; |
| 1753 | } |
| 1754 | |
| 1755 | D.setAsmLabel(AsmLabel.release()); |
| 1756 | D.SetRangeEnd(Loc); |
| 1757 | } |
| 1758 | |
| 1759 | MaybeParseGNUAttributes(D); |
| 1760 | return false; |
| 1761 | } |
| 1762 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1763 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1764 | /// declarator'. This method parses the remainder of the declaration |
| 1765 | /// (including any attributes or initializer, among other things) and |
| 1766 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1767 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1768 | /// init-declarator: [C99 6.7] |
| 1769 | /// declarator |
| 1770 | /// declarator '=' initializer |
| 1771 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1772 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1773 | /// [C++] declarator initializer[opt] |
| 1774 | /// |
| 1775 | /// [C++] initializer: |
| 1776 | /// [C++] '=' initializer-clause |
| 1777 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1778 | /// [C++0x] '=' 'default' [TODO] |
| 1779 | /// [C++0x] '=' 'delete' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1780 | /// [C++0x] braced-init-list |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1781 | /// |
| 1782 | /// According to the standard grammar, =default and =delete are function |
| 1783 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1784 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1785 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1786 | const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1787 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1788 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1789 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1790 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1791 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1792 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1793 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1794 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1795 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1796 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1797 | switch (TemplateInfo.Kind) { |
| 1798 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1799 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1800 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1801 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1802 | case ParsedTemplateInfo::Template: |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1803 | case ParsedTemplateInfo::ExplicitSpecialization: { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1804 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1805 | *TemplateInfo.TemplateParams, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1806 | D); |
Larisse Voufo | 06935f3 | 2013-08-06 03:43:07 +0000 | [diff] [blame] | 1807 | if (Tok.is(tok::semi) && |
| 1808 | Actions.HandleVariableRedeclaration(ThisDecl, D.getCXXScopeSpec())) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1809 | SkipUntil(tok::semi, true, true); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1810 | return 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1811 | } |
Larisse Voufo | 2521813 | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 1812 | if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl)) |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1813 | // Re-direct this decl to refer to the templated decl so that we can |
| 1814 | // initialize it. |
| 1815 | ThisDecl = VT->getTemplatedDecl(); |
| 1816 | break; |
| 1817 | } |
| 1818 | case ParsedTemplateInfo::ExplicitInstantiation: { |
| 1819 | if (Tok.is(tok::semi)) { |
| 1820 | DeclResult ThisRes = Actions.ActOnExplicitInstantiation( |
| 1821 | getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D); |
| 1822 | if (ThisRes.isInvalid()) { |
| 1823 | SkipUntil(tok::semi, true, true); |
| 1824 | return 0; |
| 1825 | } |
| 1826 | ThisDecl = ThisRes.get(); |
| 1827 | } else { |
| 1828 | // FIXME: This check should be for a variable template instantiation only. |
| 1829 | |
| 1830 | // Check that this is a valid instantiation |
| 1831 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 1832 | // If the declarator-id is not a template-id, issue a diagnostic and |
| 1833 | // recover by ignoring the 'template' keyword. |
| 1834 | Diag(Tok, diag::err_template_defn_explicit_instantiation) |
| 1835 | << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc); |
| 1836 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1837 | } else { |
| 1838 | SourceLocation LAngleLoc = |
| 1839 | PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
| 1840 | Diag(D.getIdentifierLoc(), |
| 1841 | diag::err_explicit_instantiation_with_definition) |
| 1842 | << SourceRange(TemplateInfo.TemplateLoc) |
| 1843 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
| 1844 | |
| 1845 | // Recover as if it were an explicit specialization. |
| 1846 | TemplateParameterLists FakedParamLists; |
| 1847 | FakedParamLists.push_back(Actions.ActOnTemplateParameterList( |
| 1848 | 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0, |
| 1849 | LAngleLoc)); |
| 1850 | |
| 1851 | ThisDecl = |
| 1852 | Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D); |
| 1853 | } |
| 1854 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1855 | break; |
| 1856 | } |
| 1857 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1858 | |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1859 | bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType(); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1860 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1861 | // Parse declarator '=' initializer. |
Richard Trieu | d6c7c67 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1862 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | fcaf27e | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1863 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1864 | ConsumeToken(); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1865 | |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1866 | if (Tok.is(tok::kw_delete)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1867 | if (D.isFunctionDeclarator()) |
| 1868 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1869 | << 1 /* delete */; |
| 1870 | else |
| 1871 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Sean Hunt | fe2695e | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1872 | } else if (Tok.is(tok::kw_default)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1873 | if (D.isFunctionDeclarator()) |
Sebastian Redl | ecfcd56 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1874 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1875 | << 0 /* default */; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1876 | else |
| 1877 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1878 | } else { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1879 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1880 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1881 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1882 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1883 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1884 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1885 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Peter Collingbourne | ec98f2f | 2012-07-27 12:56:09 +0000 | [diff] [blame] | 1886 | Actions.FinalizeDeclaration(ThisDecl); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1887 | cutOffParsing(); |
| 1888 | return 0; |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1889 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1890 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1891 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1892 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1893 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1894 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1895 | ExitScope(); |
| 1896 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1897 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1898 | if (Init.isInvalid()) { |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1899 | SkipUntil(tok::comma, true, true); |
| 1900 | Actions.ActOnInitializerError(ThisDecl); |
| 1901 | } else |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1902 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1903 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1904 | } |
| 1905 | } else if (Tok.is(tok::l_paren)) { |
| 1906 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1907 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1908 | T.consumeOpen(); |
| 1909 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1910 | ExprVector Exprs; |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1911 | CommaLocsTy CommaLocs; |
| 1912 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1913 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1914 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1915 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1918 | if (ParseExpressionList(Exprs, CommaLocs)) { |
David Blaikie | 3ea19c8 | 2012-10-10 23:15:05 +0000 | [diff] [blame] | 1919 | Actions.ActOnInitializerError(ThisDecl); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1920 | SkipUntil(tok::r_paren); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1921 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1922 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1923 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1924 | ExitScope(); |
| 1925 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1926 | } else { |
| 1927 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1928 | T.consumeClose(); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1929 | |
| 1930 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1931 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1932 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1933 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1934 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1935 | ExitScope(); |
| 1936 | } |
| 1937 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1938 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 1939 | T.getCloseLocation(), |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1940 | Exprs); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1941 | Actions.AddInitializerToDecl(ThisDecl, Initializer.take(), |
| 1942 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1943 | } |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1944 | } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) && |
Fariborz Jahanian | b0ed95c | 2012-07-03 23:22:13 +0000 | [diff] [blame] | 1945 | (!CurParsedObjCImpl || !D.isFunctionDeclarator())) { |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1946 | // Parse C++0x braced-init-list. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1947 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1948 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1949 | if (D.getCXXScopeSpec().isSet()) { |
| 1950 | EnterScope(0); |
| 1951 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1952 | } |
| 1953 | |
| 1954 | ExprResult Init(ParseBraceInitializer()); |
| 1955 | |
| 1956 | if (D.getCXXScopeSpec().isSet()) { |
| 1957 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1958 | ExitScope(); |
| 1959 | } |
| 1960 | |
| 1961 | if (Init.isInvalid()) { |
| 1962 | Actions.ActOnInitializerError(ThisDecl); |
| 1963 | } else |
| 1964 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1965 | /*DirectInit=*/true, TypeContainsAuto); |
| 1966 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1967 | } else { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1968 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1971 | Actions.FinalizeDeclaration(ThisDecl); |
| 1972 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1973 | return ThisDecl; |
| 1974 | } |
| 1975 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1976 | /// ParseSpecifierQualifierList |
| 1977 | /// specifier-qualifier-list: |
| 1978 | /// type-specifier specifier-qualifier-list[opt] |
| 1979 | /// type-qualifier specifier-qualifier-list[opt] |
| 1980 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 1981 | /// |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1982 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 1983 | DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1984 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1985 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1986 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1987 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1988 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1989 | // Validate declspec for type-name. |
| 1990 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 1991 | if ((DSC == DSC_type_specifier || DSC == DSC_trailing) && |
| 1992 | !DS.hasTypeSpecifier()) { |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1993 | Diag(Tok, diag::err_expected_type); |
| 1994 | DS.SetTypeSpecError(); |
| 1995 | } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 1996 | !DS.hasAttributes()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1997 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1998 | if (!DS.hasTypeSpecifier()) |
| 1999 | DS.SetTypeSpecError(); |
| 2000 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2001 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2002 | // Issue diagnostic and remove storage class if present. |
| 2003 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 2004 | if (DS.getStorageClassSpecLoc().isValid()) |
| 2005 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 2006 | else |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2007 | Diag(DS.getThreadStorageClassSpecLoc(), |
| 2008 | diag::err_typename_invalid_storageclass); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2009 | DS.ClearStorageClassSpecs(); |
| 2010 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2011 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2012 | // Issue diagnostic and remove function specfier if present. |
| 2013 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2014 | if (DS.isInlineSpecified()) |
| 2015 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 2016 | if (DS.isVirtualSpecified()) |
| 2017 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 2018 | if (DS.isExplicitSpecified()) |
| 2019 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2020 | DS.ClearFunctionSpecs(); |
| 2021 | } |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2022 | |
| 2023 | // Issue diagnostic and remove constexpr specfier if present. |
| 2024 | if (DS.isConstexprSpecified()) { |
| 2025 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 2026 | DS.ClearConstexprSpec(); |
| 2027 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2030 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 2031 | /// specified token is valid after the identifier in a declarator which |
| 2032 | /// immediately follows the declspec. For example, these things are valid: |
| 2033 | /// |
| 2034 | /// int x [ 4]; // direct-declarator |
| 2035 | /// int x ( int y); // direct-declarator |
| 2036 | /// int(int x ) // direct-declarator |
| 2037 | /// int x ; // simple-declaration |
| 2038 | /// int x = 17; // init-declarator-list |
| 2039 | /// int x , y; // init-declarator-list |
| 2040 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2041 | /// int x : 4; // struct-declarator |
Chris Lattner | c83c27a | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 2042 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2043 | /// |
| 2044 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 2045 | /// ')' happens to be valid anyway). |
| 2046 | /// int (x) |
| 2047 | /// |
| 2048 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 2049 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 2050 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2051 | 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] | 2052 | } |
| 2053 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2054 | |
| 2055 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 2056 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 2057 | /// the declspec has no type specifier. In this case, the declspec is either |
| 2058 | /// malformed or is "implicit int" (in K&R and C89). |
| 2059 | /// |
| 2060 | /// This method handles diagnosing this prettily and returns false if the |
| 2061 | /// declspec is done being processed. If it recovers and thinks there may be |
| 2062 | /// other pieces of declspec after it, it returns true. |
| 2063 | /// |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2064 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2065 | const ParsedTemplateInfo &TemplateInfo, |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2066 | AccessSpecifier AS, DeclSpecContext DSC, |
| 2067 | ParsedAttributesWithRange &Attrs) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2068 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2069 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2070 | SourceLocation Loc = Tok.getLocation(); |
| 2071 | // If we see an identifier that is not a type name, we normally would |
| 2072 | // parse it as the identifer being declared. However, when a typename |
| 2073 | // is typo'd or the definition is not included, this will incorrectly |
| 2074 | // parse the typename as the identifier name and fall over misparsing |
| 2075 | // later parts of the diagnostic. |
| 2076 | // |
| 2077 | // As such, we try to do some look-ahead in cases where this would |
| 2078 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 2079 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 2080 | // an identifier with implicit int, we'd get a parse error because the |
| 2081 | // next token is obviously invalid for a type. Parse these as a case |
| 2082 | // with an invalid type specifier. |
| 2083 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2084 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2085 | // 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] | 2086 | // error, do lookahead to try to do better recovery. This never applies |
| 2087 | // within a type specifier. Outside of C++, we allow this even if the |
| 2088 | // language doesn't "officially" support implicit int -- we support |
Richard Smith | 58eb370 | 2013-04-30 22:43:51 +0000 | [diff] [blame] | 2089 | // implicit int as an extension in C99 and C11. |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 2090 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
Richard Smith | 58eb370 | 2013-04-30 22:43:51 +0000 | [diff] [blame] | 2091 | !getLangOpts().CPlusPlus && |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2092 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2093 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 2094 | // we just avoid eating the identifier, so it will be parsed as the |
| 2095 | // identifier in the declarator. |
| 2096 | return false; |
| 2097 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2098 | |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2099 | if (getLangOpts().CPlusPlus && |
| 2100 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 2101 | // Don't require a type specifier if we have the 'auto' storage class |
| 2102 | // specifier in C++98 -- we'll promote it to a type specifier. |
| 2103 | return false; |
| 2104 | } |
| 2105 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2106 | // Otherwise, if we don't consume this token, we are going to emit an |
| 2107 | // error anyway. Try to recover from various common problems. Check |
| 2108 | // to see if this was a reference to a tag name without a tag specified. |
| 2109 | // 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] | 2110 | // |
| 2111 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 2112 | if (SS == 0) { |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2113 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2114 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2115 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2116 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2117 | default: break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2118 | case DeclSpec::TST_enum: |
| 2119 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 2120 | case DeclSpec::TST_union: |
| 2121 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 2122 | case DeclSpec::TST_struct: |
| 2123 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2124 | case DeclSpec::TST_interface: |
| 2125 | TagName="__interface"; FixitTagName = "__interface "; |
| 2126 | TagKind=tok::kw___interface;break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2127 | case DeclSpec::TST_class: |
| 2128 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2129 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2130 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2131 | if (TagName) { |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2132 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 2133 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 2134 | Sema::LookupOrdinaryName); |
| 2135 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2136 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2137 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 2138 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 2139 | |
| 2140 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 2141 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 2142 | I != IEnd; ++I) |
Kaelyn Uhrain | 392b3f5 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 2143 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2144 | << TokenName << TagName; |
| 2145 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2146 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2147 | // Parse this as a tag as if the missing tag were present. |
| 2148 | if (TagKind == tok::kw_enum) |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2149 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2150 | else |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2151 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2152 | /*EnteringContext*/ false, DSC_normal, Attrs); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2153 | return true; |
| 2154 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2155 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2156 | |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2157 | // Determine whether this identifier could plausibly be the name of something |
Richard Smith | 7514db2 | 2012-05-15 21:42:17 +0000 | [diff] [blame] | 2158 | // being declared (with a missing type). |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2159 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
| 2160 | (!SS || DSC == DSC_top_level || DSC == DSC_class)) { |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2161 | // Look ahead to the next token to try to figure out what this declaration |
| 2162 | // was supposed to be. |
| 2163 | switch (NextToken().getKind()) { |
| 2164 | case tok::comma: |
| 2165 | case tok::equal: |
| 2166 | case tok::kw_asm: |
| 2167 | case tok::l_brace: |
| 2168 | case tok::l_square: |
| 2169 | case tok::semi: |
| 2170 | // This looks like a variable declaration. The type is probably missing. |
| 2171 | // We're done parsing decl-specifiers. |
| 2172 | return false; |
| 2173 | |
| 2174 | case tok::l_paren: { |
| 2175 | // static x(4); // 'x' is not a type |
| 2176 | // x(int n); // 'x' is not a type |
| 2177 | // x (*p)[]; // 'x' is a type |
| 2178 | // |
| 2179 | // Since we're in an error case (or the rare 'implicit int in C++' MS |
| 2180 | // extension), we can afford to perform a tentative parse to determine |
| 2181 | // which case we're in. |
| 2182 | TentativeParsingAction PA(*this); |
| 2183 | ConsumeToken(); |
| 2184 | TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false); |
| 2185 | PA.Revert(); |
| 2186 | if (TPR == TPResult::False()) |
| 2187 | return false; |
| 2188 | // The identifier is followed by a parenthesized declarator. |
| 2189 | // It's supposed to be a type. |
| 2190 | break; |
| 2191 | } |
| 2192 | |
| 2193 | default: |
| 2194 | // This is probably supposed to be a type. This includes cases like: |
| 2195 | // int f(itn); |
| 2196 | // struct S { unsinged : 4; }; |
| 2197 | break; |
| 2198 | } |
| 2199 | } |
| 2200 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2201 | // 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] | 2202 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2203 | ParsedType T; |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2204 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2205 | if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2206 | // The action emitted a diagnostic, so we don't have to. |
| 2207 | if (T) { |
| 2208 | // The action has suggested that the type T could be used. Set that as |
| 2209 | // the type in the declaration specifiers, consume the would-be type |
| 2210 | // name token, and we're done. |
| 2211 | const char *PrevSpec; |
| 2212 | unsigned DiagID; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2213 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2214 | DS.SetRangeEnd(Tok.getLocation()); |
| 2215 | ConsumeToken(); |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2216 | // There may be other declaration specifiers after this. |
| 2217 | return true; |
| 2218 | } else if (II != Tok.getIdentifierInfo()) { |
| 2219 | // If no type was suggested, the correction is to a keyword |
| 2220 | Tok.setKind(II->getTokenID()); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2221 | // There may be other declaration specifiers after this. |
| 2222 | return true; |
| 2223 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2224 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2225 | // Fall through; the action had no suggestion for us. |
| 2226 | } else { |
| 2227 | // The action did not emit a diagnostic, so emit one now. |
| 2228 | SourceRange R; |
| 2229 | if (SS) R = SS->getRange(); |
| 2230 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 2231 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2232 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2233 | // Mark this as an error. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2234 | DS.SetTypeSpecError(); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2235 | DS.SetRangeEnd(Tok.getLocation()); |
| 2236 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2237 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2238 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 2239 | // avoid rippling error messages on subsequent uses of the same type, |
| 2240 | // could be useful if #include was forgotten. |
| 2241 | return false; |
| 2242 | } |
| 2243 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2244 | /// \brief Determine the declaration specifier context from the declarator |
| 2245 | /// context. |
| 2246 | /// |
| 2247 | /// \param Context the declarator context, which is one of the |
| 2248 | /// Declarator::TheContext enumerator values. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2249 | Parser::DeclSpecContext |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2250 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 2251 | if (Context == Declarator::MemberContext) |
| 2252 | return DSC_class; |
| 2253 | if (Context == Declarator::FileContext) |
| 2254 | return DSC_top_level; |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 2255 | if (Context == Declarator::TrailingReturnContext) |
| 2256 | return DSC_trailing; |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2257 | return DSC_normal; |
| 2258 | } |
| 2259 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2260 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 2261 | /// |
| 2262 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 2263 | /// type. Ideally, the type should be propagated directly into Sema. |
| 2264 | /// |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2265 | /// [C11] type-id |
| 2266 | /// [C11] constant-expression |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2267 | /// [C++0x] type-id ...[opt] |
| 2268 | /// [C++0x] assignment-expression ...[opt] |
| 2269 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 2270 | SourceLocation &EllipsisLoc) { |
| 2271 | ExprResult ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2272 | if (isTypeIdInParens()) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2273 | SourceLocation TypeLoc = Tok.getLocation(); |
| 2274 | ParsedType Ty = ParseTypeName().get(); |
| 2275 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2276 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 2277 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2278 | } else |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2279 | ER = ParseConstantExpression(); |
| 2280 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2281 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis)) |
Peter Collingbourne | fe9b2a8 | 2011-10-24 17:56:00 +0000 | [diff] [blame] | 2282 | EllipsisLoc = ConsumeToken(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2283 | |
| 2284 | return ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 2288 | /// attribute to Attrs. |
| 2289 | /// |
| 2290 | /// alignment-specifier: |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2291 | /// [C11] '_Alignas' '(' type-id ')' |
| 2292 | /// [C11] '_Alignas' '(' constant-expression ')' |
Richard Smith | 33f04a2 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2293 | /// [C++11] 'alignas' '(' type-id ...[opt] ')' |
| 2294 | /// [C++11] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2295 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2296 | SourceLocation *EndLoc) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2297 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 2298 | "Not an alignment-specifier!"); |
| 2299 | |
Richard Smith | 33f04a2 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2300 | IdentifierInfo *KWName = Tok.getIdentifierInfo(); |
| 2301 | SourceLocation KWLoc = ConsumeToken(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2302 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2303 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2304 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2305 | return; |
| 2306 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2307 | SourceLocation EllipsisLoc; |
| 2308 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2309 | if (ArgExpr.isInvalid()) { |
| 2310 | SkipUntil(tok::r_paren); |
| 2311 | return; |
| 2312 | } |
| 2313 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2314 | T.consumeClose(); |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2315 | if (EndLoc) |
| 2316 | *EndLoc = T.getCloseLocation(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2317 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2318 | ArgsVector ArgExprs; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2319 | ArgExprs.push_back(ArgExpr.release()); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2320 | Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1, |
| 2321 | AttributeList::AS_Keyword, EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2322 | } |
| 2323 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2324 | /// ParseDeclarationSpecifiers |
| 2325 | /// declaration-specifiers: [C99 6.7] |
| 2326 | /// storage-class-specifier declaration-specifiers[opt] |
| 2327 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2328 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2329 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2330 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2331 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2332 | /// |
| 2333 | /// storage-class-specifier: [C99 6.7.1] |
| 2334 | /// 'typedef' |
| 2335 | /// 'extern' |
| 2336 | /// 'static' |
| 2337 | /// 'auto' |
| 2338 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2339 | /// [C++] 'mutable' |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2340 | /// [C++11] 'thread_local' |
| 2341 | /// [C11] '_Thread_local' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2342 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2343 | /// function-specifier: [C99 6.7.4] |
| 2344 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2345 | /// [C++] 'virtual' |
| 2346 | /// [C++] 'explicit' |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2347 | /// [OpenCL] '__kernel' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2348 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2349 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2350 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2351 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 2352 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2353 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2354 | AccessSpecifier AS, |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2355 | DeclSpecContext DSContext, |
| 2356 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2357 | if (DS.getSourceRange().isInvalid()) { |
| 2358 | DS.SetRangeStart(Tok.getLocation()); |
| 2359 | DS.SetRangeEnd(Tok.getLocation()); |
| 2360 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2361 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2362 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2363 | bool AttrsLastTime = false; |
| 2364 | ParsedAttributesWithRange attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2365 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2366 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2367 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2368 | unsigned DiagID = 0; |
| 2369 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2370 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2371 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2372 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2373 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2374 | DoneWithDeclSpec: |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2375 | if (!AttrsLastTime) |
| 2376 | ProhibitAttributes(attrs); |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2377 | else { |
| 2378 | // Reject C++11 attributes that appertain to decl specifiers as |
| 2379 | // we don't support any C++11 attributes that appertain to decl |
| 2380 | // specifiers. This also conforms to what g++ 4.8 is doing. |
| 2381 | ProhibitCXX11Attributes(attrs); |
| 2382 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2383 | DS.takeAttributesFrom(attrs); |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2384 | } |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 2385 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2386 | // If this is not a declaration specifier token, we're done reading decl |
| 2387 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2388 | DS.Finish(Diags, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2389 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2390 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2391 | case tok::l_square: |
| 2392 | case tok::kw_alignas: |
Richard Smith | 672edb0 | 2013-02-22 09:15:49 +0000 | [diff] [blame] | 2393 | if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier()) |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2394 | goto DoneWithDeclSpec; |
| 2395 | |
| 2396 | ProhibitAttributes(attrs); |
| 2397 | // FIXME: It would be good to recover by accepting the attributes, |
| 2398 | // but attempting to do that now would cause serious |
| 2399 | // madness in terms of diagnostics. |
| 2400 | attrs.clear(); |
| 2401 | attrs.Range = SourceRange(); |
| 2402 | |
| 2403 | ParseCXX11Attributes(attrs); |
| 2404 | AttrsLastTime = true; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2405 | continue; |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2406 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2407 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2408 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2409 | if (DS.hasTypeSpecifier()) { |
| 2410 | bool AllowNonIdentifiers |
| 2411 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 2412 | Scope::BlockScope | |
| 2413 | Scope::TemplateParamScope | |
| 2414 | Scope::FunctionPrototypeScope | |
| 2415 | Scope::AtCatchScope)) == 0; |
| 2416 | bool AllowNestedNameSpecifiers |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2417 | = DSContext == DSC_top_level || |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2418 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 2419 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2420 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2421 | AllowNonIdentifiers, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2422 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2423 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2426 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 2427 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 2428 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2429 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2430 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2431 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2432 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 2433 | else if (CurParsedObjCImpl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2434 | CCC = Sema::PCC_ObjCImplementation; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2435 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2436 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2437 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2438 | } |
| 2439 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2440 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2441 | // C++ scope specifier. Annotate and loop, or bail out on error. |
Eli Friedman | 5a42820 | 2013-08-15 23:59:20 +0000 | [diff] [blame] | 2442 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2443 | if (!DS.hasTypeSpecifier()) |
| 2444 | DS.SetTypeSpecError(); |
| 2445 | goto DoneWithDeclSpec; |
| 2446 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 2447 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 2448 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2449 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2450 | |
| 2451 | case tok::annot_cxxscope: { |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2452 | if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector()) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2453 | goto DoneWithDeclSpec; |
| 2454 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2455 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 2456 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 2457 | Tok.getAnnotationRange(), |
| 2458 | SS); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2459 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2460 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2461 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2462 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2463 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2464 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2465 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2466 | |
| 2467 | // C++ [class.qual]p2: |
| 2468 | // In a lookup in which the constructor is an acceptable lookup |
| 2469 | // result and the nested-name-specifier nominates a class C: |
| 2470 | // |
| 2471 | // - if the name specified after the |
| 2472 | // nested-name-specifier, when looked up in C, is the |
| 2473 | // injected-class-name of C (Clause 9), or |
| 2474 | // |
| 2475 | // - if the name specified after the nested-name-specifier |
| 2476 | // is the same as the identifier or the |
| 2477 | // simple-template-id's template-name in the last |
| 2478 | // component of the nested-name-specifier, |
| 2479 | // |
| 2480 | // the name is instead considered to name the constructor of |
| 2481 | // class C. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2482 | // |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2483 | // Thus, if the template-name is actually the constructor |
| 2484 | // name, then the code is ill-formed; this interpretation is |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2485 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2486 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2487 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2488 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2489 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2490 | if (isConstructorDeclarator()) { |
| 2491 | // The user meant this to be an out-of-line constructor |
| 2492 | // definition, but template arguments are not allowed |
| 2493 | // there. Just allow this as a constructor; we'll |
| 2494 | // complain about it later. |
| 2495 | goto DoneWithDeclSpec; |
| 2496 | } |
| 2497 | |
| 2498 | // The user meant this to name a type, but it actually names |
| 2499 | // a constructor with some extraneous template |
| 2500 | // arguments. Complain, then parse it as a type as the user |
| 2501 | // intended. |
| 2502 | Diag(TemplateId->TemplateNameLoc, |
| 2503 | diag::err_out_of_line_template_id_names_constructor) |
| 2504 | << TemplateId->Name; |
| 2505 | } |
| 2506 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2507 | DS.getTypeSpecScope() = SS; |
| 2508 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2509 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2510 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2511 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2512 | continue; |
| 2513 | } |
| 2514 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2515 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2516 | DS.getTypeSpecScope() = SS; |
| 2517 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2518 | if (Tok.getAnnotationValue()) { |
| 2519 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2520 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2521 | Tok.getAnnotationEndLoc(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2522 | PrevSpec, DiagID, T); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2523 | if (isInvalid) |
| 2524 | break; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2525 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2526 | else |
| 2527 | DS.SetTypeSpecError(); |
| 2528 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2529 | ConsumeToken(); // The typename |
| 2530 | } |
| 2531 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2532 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2533 | goto DoneWithDeclSpec; |
| 2534 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2535 | // If we're in a context where the identifier could be a class name, |
| 2536 | // check whether this is a constructor declaration. |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2537 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2538 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2539 | &SS)) { |
| 2540 | if (isConstructorDeclarator()) |
| 2541 | goto DoneWithDeclSpec; |
| 2542 | |
| 2543 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 2544 | // of the class is qualified in a context where it could name |
| 2545 | // a constructor, its a constructor name. However, we've |
| 2546 | // looked at the declarator, and the user probably meant this |
| 2547 | // to be a type. Complain that it isn't supposed to be treated |
| 2548 | // as a type, then proceed to parse it as a type. |
| 2549 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 2550 | << Next.getIdentifierInfo(); |
| 2551 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2552 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2553 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 2554 | Next.getLocation(), |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2555 | getCurScope(), &SS, |
| 2556 | false, false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2557 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2558 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2559 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2560 | // If the referenced identifier is not a type, then this declspec is |
| 2561 | // erroneous: We already checked about that it has no type specifier, and |
| 2562 | // 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] | 2563 | // typename. |
David Blaikie | 7247c88 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 2564 | if (!TypeRep) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2565 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2566 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2567 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) { |
| 2568 | if (!Attrs.empty()) { |
| 2569 | AttrsLastTime = true; |
| 2570 | attrs.takeAllFrom(Attrs); |
| 2571 | } |
| 2572 | continue; |
| 2573 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2574 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2575 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2576 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2577 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2578 | ConsumeToken(); // The C++ scope. |
| 2579 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2580 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2581 | DiagID, TypeRep); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2582 | if (isInvalid) |
| 2583 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2584 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2585 | DS.SetRangeEnd(Tok.getLocation()); |
| 2586 | ConsumeToken(); // The typename. |
| 2587 | |
| 2588 | continue; |
| 2589 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2590 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2591 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2592 | if (Tok.getAnnotationValue()) { |
| 2593 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | c43271e | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 2594 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2595 | DiagID, T); |
| 2596 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2597 | DS.SetTypeSpecError(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2598 | |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 2599 | if (isInvalid) |
| 2600 | break; |
| 2601 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2602 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2603 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2604 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2605 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2606 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2607 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2608 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2609 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2610 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2611 | continue; |
| 2612 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2613 | |
Douglas Gregor | bfad915 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2614 | case tok::kw___is_signed: |
| 2615 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 2616 | // typically treats it as a trait. If we see __is_signed as it appears |
| 2617 | // in libstdc++, e.g., |
| 2618 | // |
| 2619 | // static const bool __is_signed; |
| 2620 | // |
| 2621 | // then treat __is_signed as an identifier rather than as a keyword. |
| 2622 | if (DS.getTypeSpecType() == TST_bool && |
| 2623 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
| 2624 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 2625 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
| 2626 | Tok.setKind(tok::identifier); |
| 2627 | } |
| 2628 | |
| 2629 | // We're done with the declaration-specifiers. |
| 2630 | goto DoneWithDeclSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2631 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2632 | // typedef-name |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2633 | case tok::kw_decltype: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2634 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2635 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 2636 | // so handle it as such. This is important for ctor parsing. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2637 | if (getLangOpts().CPlusPlus) { |
Eli Friedman | 5a42820 | 2013-08-15 23:59:20 +0000 | [diff] [blame] | 2638 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2639 | if (!DS.hasTypeSpecifier()) |
| 2640 | DS.SetTypeSpecError(); |
| 2641 | goto DoneWithDeclSpec; |
| 2642 | } |
| 2643 | if (!Tok.is(tok::identifier)) |
| 2644 | continue; |
| 2645 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2646 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2647 | // This identifier can only be a typedef name if we haven't already seen |
| 2648 | // a type-specifier. Without this check we misparse: |
| 2649 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 2650 | if (DS.hasTypeSpecifier()) |
| 2651 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2652 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2653 | // Check for need to substitute AltiVec keyword tokens. |
| 2654 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2655 | break; |
| 2656 | |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2657 | // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not |
| 2658 | // allow the use of a typedef name as a type specifier. |
| 2659 | if (DS.isTypeAltiVecVector()) |
| 2660 | goto DoneWithDeclSpec; |
| 2661 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2662 | ParsedType TypeRep = |
| 2663 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 2664 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2665 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2666 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 2667 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2668 | if (!TypeRep) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2669 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2670 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) { |
| 2671 | if (!Attrs.empty()) { |
| 2672 | AttrsLastTime = true; |
| 2673 | attrs.takeAllFrom(Attrs); |
| 2674 | } |
| 2675 | continue; |
| 2676 | } |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2677 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2678 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2679 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2680 | // If we're in a context where the identifier could be a class name, |
| 2681 | // check whether this is a constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2682 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2683 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2684 | isConstructorDeclarator()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2685 | goto DoneWithDeclSpec; |
| 2686 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2687 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2688 | DiagID, TypeRep); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2689 | if (isInvalid) |
| 2690 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2691 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2692 | DS.SetRangeEnd(Tok.getLocation()); |
| 2693 | ConsumeToken(); // The identifier |
| 2694 | |
| 2695 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2696 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2697 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2698 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2699 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2700 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2701 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2702 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2703 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2704 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2705 | |
| 2706 | // type-name |
| 2707 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2708 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2709 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2710 | // This template-id does not refer to a type name, so we're |
| 2711 | // done with the type-specifiers. |
| 2712 | goto DoneWithDeclSpec; |
| 2713 | } |
| 2714 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2715 | // If we're in a context where the template-id could be a |
| 2716 | // constructor name or specialization, check whether this is a |
| 2717 | // constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2718 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2719 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2720 | isConstructorDeclarator()) |
| 2721 | goto DoneWithDeclSpec; |
| 2722 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2723 | // Turn the template-id annotation token into a type annotation |
| 2724 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2725 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2726 | continue; |
| 2727 | } |
| 2728 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2729 | // GNU attributes support. |
| 2730 | case tok::kw___attribute: |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2731 | ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2732 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2733 | |
| 2734 | // Microsoft declspec support. |
| 2735 | case tok::kw___declspec: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2736 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2737 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2738 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2739 | // Microsoft single token adornments. |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2740 | case tok::kw___forceinline: { |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2741 | isInvalid = DS.setFunctionSpecInline(Loc); |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2742 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2743 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 2744 | // FIXME: This does not work correctly if it is set to be a declspec |
| 2745 | // attribute, and a GNU attribute is simply incorrect. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame] | 2746 | DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 2747 | AttributeList::AS_GNU); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2748 | break; |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2749 | } |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2750 | |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 2751 | case tok::kw___sptr: |
| 2752 | case tok::kw___uptr: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2753 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2754 | case tok::kw___ptr32: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2755 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2756 | case tok::kw___cdecl: |
| 2757 | case tok::kw___stdcall: |
| 2758 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2759 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2760 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2761 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2762 | continue; |
| 2763 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2764 | // Borland single token adornments. |
| 2765 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2766 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2767 | continue; |
| 2768 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2769 | // OpenCL single token adornments. |
| 2770 | case tok::kw___kernel: |
| 2771 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2772 | continue; |
| 2773 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2774 | // storage-class-specifier |
| 2775 | case tok::kw_typedef: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2776 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
| 2777 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2778 | break; |
| 2779 | case tok::kw_extern: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2780 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2781 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2782 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
| 2783 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2784 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2785 | case tok::kw___private_extern__: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2786 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
| 2787 | Loc, PrevSpec, DiagID); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2788 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2789 | case tok::kw_static: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2790 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2791 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2792 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
| 2793 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2794 | break; |
| 2795 | case tok::kw_auto: |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2796 | if (getLangOpts().CPlusPlus11) { |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2797 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2798 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2799 | PrevSpec, DiagID); |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2800 | if (!isInvalid) |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2801 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2802 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2803 | } else |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2804 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 2805 | DiagID); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2806 | } else |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2807 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2808 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2809 | break; |
| 2810 | case tok::kw_register: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2811 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
| 2812 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2813 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2814 | case tok::kw_mutable: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2815 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
| 2816 | PrevSpec, DiagID); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2817 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2818 | case tok::kw___thread: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2819 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc, |
| 2820 | PrevSpec, DiagID); |
| 2821 | break; |
| 2822 | case tok::kw_thread_local: |
| 2823 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc, |
| 2824 | PrevSpec, DiagID); |
| 2825 | break; |
| 2826 | case tok::kw__Thread_local: |
| 2827 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local, |
| 2828 | Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2829 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2830 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2831 | // function-specifier |
| 2832 | case tok::kw_inline: |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2833 | isInvalid = DS.setFunctionSpecInline(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2834 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2835 | case tok::kw_virtual: |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2836 | isInvalid = DS.setFunctionSpecVirtual(Loc); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2837 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2838 | case tok::kw_explicit: |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2839 | isInvalid = DS.setFunctionSpecExplicit(Loc); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2840 | break; |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 2841 | case tok::kw__Noreturn: |
| 2842 | if (!getLangOpts().C11) |
| 2843 | Diag(Loc, diag::ext_c11_noreturn); |
| 2844 | isInvalid = DS.setFunctionSpecNoreturn(Loc); |
| 2845 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2846 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2847 | // alignment-specifier |
| 2848 | case tok::kw__Alignas: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2849 | if (!getLangOpts().C11) |
Jordan Rose | f70a886 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2850 | Diag(Tok, diag::ext_c11_alignment) << Tok.getName(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2851 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2852 | continue; |
| 2853 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2854 | // friend |
| 2855 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2856 | if (DSContext == DSC_class) |
| 2857 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 2858 | else { |
| 2859 | PrevSpec = ""; // not actually used by the diagnostic |
| 2860 | DiagID = diag::err_friend_invalid_in_context; |
| 2861 | isInvalid = true; |
| 2862 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2863 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2864 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2865 | // Modules |
| 2866 | case tok::kw___module_private__: |
| 2867 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 2868 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2869 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2870 | // constexpr |
| 2871 | case tok::kw_constexpr: |
| 2872 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 2873 | break; |
| 2874 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2875 | // type-specifier |
| 2876 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2877 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 2878 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2879 | break; |
| 2880 | case tok::kw_long: |
| 2881 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2882 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2883 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2884 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2885 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2886 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2887 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2888 | case tok::kw___int64: |
| 2889 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2890 | DiagID); |
| 2891 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2892 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2893 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 2894 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2895 | break; |
| 2896 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2897 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2898 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2899 | break; |
| 2900 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2901 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2902 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2903 | break; |
| 2904 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2905 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2906 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2907 | break; |
| 2908 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2909 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 2910 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2911 | break; |
| 2912 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2913 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 2914 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2915 | break; |
| 2916 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2917 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 2918 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2919 | break; |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 2920 | case tok::kw___int128: |
| 2921 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
| 2922 | DiagID); |
| 2923 | break; |
| 2924 | case tok::kw_half: |
| 2925 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
| 2926 | DiagID); |
| 2927 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2928 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2929 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 2930 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2931 | break; |
| 2932 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2933 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 2934 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2935 | break; |
| 2936 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2937 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 2938 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2939 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2940 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2941 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 2942 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2943 | break; |
| 2944 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2945 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 2946 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2947 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2948 | case tok::kw_bool: |
| 2949 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2950 | if (Tok.is(tok::kw_bool) && |
| 2951 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 2952 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 2953 | PrevSpec = ""; // Not used by the diagnostic. |
| 2954 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2955 | // For better error recovery. |
| 2956 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2957 | isInvalid = true; |
| 2958 | } else { |
| 2959 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 2960 | DiagID); |
| 2961 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2962 | break; |
| 2963 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2964 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2965 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2966 | break; |
| 2967 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2968 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2969 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2970 | break; |
| 2971 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2972 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2973 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2974 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2975 | case tok::kw___vector: |
| 2976 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2977 | break; |
| 2978 | case tok::kw___pixel: |
| 2979 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2980 | break; |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 2981 | case tok::kw_image1d_t: |
| 2982 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc, |
| 2983 | PrevSpec, DiagID); |
| 2984 | break; |
| 2985 | case tok::kw_image1d_array_t: |
| 2986 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc, |
| 2987 | PrevSpec, DiagID); |
| 2988 | break; |
| 2989 | case tok::kw_image1d_buffer_t: |
| 2990 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc, |
| 2991 | PrevSpec, DiagID); |
| 2992 | break; |
| 2993 | case tok::kw_image2d_t: |
| 2994 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc, |
| 2995 | PrevSpec, DiagID); |
| 2996 | break; |
| 2997 | case tok::kw_image2d_array_t: |
| 2998 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc, |
| 2999 | PrevSpec, DiagID); |
| 3000 | break; |
| 3001 | case tok::kw_image3d_t: |
| 3002 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc, |
| 3003 | PrevSpec, DiagID); |
| 3004 | break; |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3005 | case tok::kw_sampler_t: |
| 3006 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc, |
| 3007 | PrevSpec, DiagID); |
| 3008 | break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3009 | case tok::kw_event_t: |
| 3010 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc, |
| 3011 | PrevSpec, DiagID); |
| 3012 | break; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3013 | case tok::kw___unknown_anytype: |
| 3014 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 3015 | PrevSpec, DiagID); |
| 3016 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3017 | |
| 3018 | // class-specifier: |
| 3019 | case tok::kw_class: |
| 3020 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3021 | case tok::kw___interface: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3022 | case tok::kw_union: { |
| 3023 | tok::TokenKind Kind = Tok.getKind(); |
| 3024 | ConsumeToken(); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3025 | |
| 3026 | // These are attributes following class specifiers. |
| 3027 | // To produce better diagnostic, we parse them when |
| 3028 | // parsing class specifier. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3029 | ParsedAttributesWithRange Attributes(AttrFactory); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3030 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3031 | EnteringContext, DSContext, Attributes); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3032 | |
| 3033 | // If there are attributes following class specifier, |
| 3034 | // take them over and handle them here. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3035 | if (!Attributes.empty()) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3036 | AttrsLastTime = true; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3037 | attrs.takeAllFrom(Attributes); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3038 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3039 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3040 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3041 | |
| 3042 | // enum-specifier: |
| 3043 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3044 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3045 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3046 | continue; |
| 3047 | |
| 3048 | // cv-qualifier: |
| 3049 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3050 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, 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 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3054 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3055 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3056 | break; |
| 3057 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3058 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3059 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3060 | break; |
| 3061 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3062 | // C++ typename-specifier: |
| 3063 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3064 | if (TryAnnotateTypeOrScopeToken()) { |
| 3065 | DS.SetTypeSpecError(); |
| 3066 | goto DoneWithDeclSpec; |
| 3067 | } |
| 3068 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3069 | continue; |
| 3070 | break; |
| 3071 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3072 | // GNU typeof support. |
| 3073 | case tok::kw_typeof: |
| 3074 | ParseTypeofSpecifier(DS); |
| 3075 | continue; |
| 3076 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3077 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 3078 | ParseDecltypeSpecifier(DS); |
| 3079 | continue; |
| 3080 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3081 | case tok::kw___underlying_type: |
| 3082 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3083 | continue; |
| 3084 | |
| 3085 | case tok::kw__Atomic: |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3086 | // C11 6.7.2.4/4: |
| 3087 | // If the _Atomic keyword is immediately followed by a left parenthesis, |
| 3088 | // it is interpreted as a type specifier (with a type name), not as a |
| 3089 | // type qualifier. |
| 3090 | if (NextToken().is(tok::l_paren)) { |
| 3091 | ParseAtomicSpecifier(DS); |
| 3092 | continue; |
| 3093 | } |
| 3094 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 3095 | getLangOpts()); |
| 3096 | break; |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3097 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3098 | // OpenCL qualifiers: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3099 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3100 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3101 | goto DoneWithDeclSpec; |
| 3102 | case tok::kw___private: |
| 3103 | case tok::kw___global: |
| 3104 | case tok::kw___local: |
| 3105 | case tok::kw___constant: |
| 3106 | case tok::kw___read_only: |
| 3107 | case tok::kw___write_only: |
| 3108 | case tok::kw___read_write: |
| 3109 | ParseOpenCLQualifiers(DS); |
| 3110 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3111 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 3112 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3113 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3114 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 3115 | // but we support it. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3116 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3117 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3118 | |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 3119 | if (!ParseObjCProtocolQualifiers(DS)) |
| 3120 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 3121 | << FixItHint::CreateInsertion(Loc, "id") |
| 3122 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3123 | |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 3124 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 3125 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 3126 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3127 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3128 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3129 | if (isInvalid) { |
| 3130 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3131 | assert(DiagID); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3132 | |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 3133 | if (DiagID == diag::ext_duplicate_declspec) |
| 3134 | Diag(Tok, DiagID) |
| 3135 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 3136 | else |
| 3137 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3138 | } |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3139 | |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 3140 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3141 | if (DiagID != diag::err_bool_redeclaration) |
| 3142 | ConsumeToken(); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3143 | |
| 3144 | AttrsLastTime = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3145 | } |
| 3146 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 3147 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3148 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 3149 | /// semicolon. |
| 3150 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3151 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3152 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3153 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3154 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3155 | /// struct-declarator-list: |
| 3156 | /// struct-declarator |
| 3157 | /// struct-declarator-list ',' struct-declarator |
| 3158 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 3159 | /// struct-declarator: |
| 3160 | /// declarator |
| 3161 | /// [GNU] declarator attributes[opt] |
| 3162 | /// declarator[opt] ':' constant-expression |
| 3163 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 3164 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3165 | void Parser:: |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3166 | ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3167 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3168 | if (Tok.is(tok::kw___extension__)) { |
| 3169 | // __extension__ silences extension warnings in the subexpression. |
| 3170 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3171 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3172 | return ParseStructDeclaration(DS, Fields); |
| 3173 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3174 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3175 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3176 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3177 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3178 | // If there are no declarators, this is a free-standing declaration |
| 3179 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3180 | if (Tok.is(tok::semi)) { |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3181 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
| 3182 | DS); |
| 3183 | DS.complete(TheDecl); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3184 | return; |
| 3185 | } |
| 3186 | |
| 3187 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3188 | bool FirstDeclarator = true; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3189 | SourceLocation CommaLoc; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3190 | while (1) { |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3191 | ParsingFieldDeclarator DeclaratorInfo(*this, DS); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3192 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3193 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3194 | // Attributes are only allowed here on successive declarators. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3195 | if (!FirstDeclarator) |
| 3196 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3197 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3198 | /// struct-declarator: declarator |
| 3199 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3200 | if (Tok.isNot(tok::colon)) { |
| 3201 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 3202 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3203 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3204 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3205 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3206 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3207 | ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3208 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3209 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3210 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 3211 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 3212 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3213 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3214 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3215 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3216 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3217 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3218 | // We're done with this declarator; invoke the callback. |
Eli Friedman | 817a886 | 2012-08-08 23:35:12 +0000 | [diff] [blame] | 3219 | Fields.invoke(DeclaratorInfo); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3220 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3221 | // If we don't have a comma, it is either the end of the list (a ';') |
| 3222 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3223 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3224 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3225 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3226 | // Consume the comma. |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3227 | CommaLoc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3228 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3229 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3230 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3231 | } |
| 3232 | |
| 3233 | /// ParseStructUnionBody |
| 3234 | /// struct-contents: |
| 3235 | /// struct-declaration-list |
| 3236 | /// [EXT] empty |
| 3237 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 3238 | /// struct-declaration-list: |
| 3239 | /// struct-declaration |
| 3240 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3241 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3242 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3243 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3244 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3245 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 3246 | "parsing struct/union body"); |
Andy Gibbs | f50f3f7 | 2013-04-03 09:31:19 +0000 | [diff] [blame] | 3247 | assert(!getLangOpts().CPlusPlus && "C++ declarations not supported"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3248 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3249 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3250 | if (T.consumeOpen()) |
| 3251 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3252 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3253 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3254 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3255 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3256 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3257 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3258 | // 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] | 3259 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3260 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3261 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3262 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3263 | if (Tok.is(tok::semi)) { |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3264 | ConsumeExtraSemi(InsideStruct, TagType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3265 | continue; |
| 3266 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3267 | |
Andy Gibbs | 74b9fa1 | 2013-04-03 09:46:04 +0000 | [diff] [blame] | 3268 | // Parse _Static_assert declaration. |
| 3269 | if (Tok.is(tok::kw__Static_assert)) { |
| 3270 | SourceLocation DeclEnd; |
| 3271 | ParseStaticAssertDeclaration(DeclEnd); |
| 3272 | continue; |
| 3273 | } |
| 3274 | |
Argyrios Kyrtzidis | bd95745 | 2013-04-18 01:42:35 +0000 | [diff] [blame] | 3275 | if (Tok.is(tok::annot_pragma_pack)) { |
| 3276 | HandlePragmaPack(); |
| 3277 | continue; |
| 3278 | } |
| 3279 | |
| 3280 | if (Tok.is(tok::annot_pragma_align)) { |
| 3281 | HandlePragmaAlign(); |
| 3282 | continue; |
| 3283 | } |
| 3284 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3285 | if (!Tok.is(tok::at)) { |
| 3286 | struct CFieldCallback : FieldCallback { |
| 3287 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3288 | Decl *TagDecl; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3289 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3290 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3291 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3292 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3293 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 3294 | |
Eli Friedman | dcdff46 | 2012-08-08 23:53:27 +0000 | [diff] [blame] | 3295 | void invoke(ParsingFieldDeclarator &FD) { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3296 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3297 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 3298 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 3299 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3300 | FieldDecls.push_back(Field); |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3301 | FD.complete(Field); |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3302 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3303 | } Callback(*this, TagDecl, FieldDecls); |
| 3304 | |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3305 | // Parse all the comma separated declarators. |
| 3306 | ParsingDeclSpec DS(*this); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3307 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3308 | } else { // Handle @defs |
| 3309 | ConsumeToken(); |
| 3310 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 3311 | Diag(Tok, diag::err_unexpected_at); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3312 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3313 | continue; |
| 3314 | } |
| 3315 | ConsumeToken(); |
| 3316 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 3317 | if (!Tok.is(tok::identifier)) { |
| 3318 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3319 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3320 | continue; |
| 3321 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3322 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3323 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3324 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3325 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 3326 | ConsumeToken(); |
| 3327 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3328 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3329 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3330 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3331 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3332 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3333 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3334 | break; |
| 3335 | } else { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3336 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 3337 | // 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] | 3338 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3339 | // If we stopped at a ';', eat it. |
| 3340 | if (Tok.is(tok::semi)) ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3341 | } |
| 3342 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3343 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3344 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3345 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3346 | ParsedAttributes attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3347 | // If attributes exist after struct contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3348 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 3349 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3350 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 3351 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3352 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3353 | attrs.getList()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3354 | StructScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3355 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 3356 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3357 | } |
| 3358 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3359 | /// ParseEnumSpecifier |
| 3360 | /// enum-specifier: [C99 6.7.2.2] |
| 3361 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3362 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3363 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3364 | /// '}' attributes[opt] |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 3365 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3366 | /// '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3367 | /// 'enum' identifier |
| 3368 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3369 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3370 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 3371 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3372 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3373 | /// enum-head: [C++11] |
| 3374 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 3375 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 3376 | /// identifier enum-base[opt] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3377 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3378 | /// enum-key: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3379 | /// 'enum' |
| 3380 | /// 'enum' 'class' |
| 3381 | /// 'enum' 'struct' |
| 3382 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3383 | /// enum-base: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3384 | /// ':' type-specifier-seq |
| 3385 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3386 | /// [C++] elaborated-type-specifier: |
| 3387 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 3388 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3389 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 3390 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3391 | AccessSpecifier AS, DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3392 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3393 | if (Tok.is(tok::code_completion)) { |
| 3394 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3395 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3396 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3397 | } |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3398 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3399 | // If attributes exist after tag, parse them. |
| 3400 | ParsedAttributesWithRange attrs(AttrFactory); |
| 3401 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3402 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3403 | |
| 3404 | // If declspecs exist after tag, parse them. |
| 3405 | while (Tok.is(tok::kw___declspec)) |
| 3406 | ParseMicrosoftDeclSpec(attrs); |
| 3407 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3408 | SourceLocation ScopedEnumKWLoc; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3409 | bool IsScopedUsingClassTag = false; |
| 3410 | |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3411 | // In C++11, recognize 'enum class' and 'enum struct'. |
Richard Trieu | ed5a292 | 2013-04-23 02:47:36 +0000 | [diff] [blame] | 3412 | if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) { |
| 3413 | Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum |
| 3414 | : diag::ext_scoped_enum); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3415 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3416 | ScopedEnumKWLoc = ConsumeToken(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3417 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3418 | // Attributes are not allowed between these keywords. Diagnose, |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3419 | // but then just treat them like they appeared in the right place. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3420 | ProhibitAttributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3421 | |
| 3422 | // They are allowed afterwards, though. |
| 3423 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3424 | MaybeParseCXX11Attributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3425 | while (Tok.is(tok::kw___declspec)) |
| 3426 | ParseMicrosoftDeclSpec(attrs); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3427 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3428 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3429 | // C++11 [temp.explicit]p12: |
| 3430 | // The usual access controls do not apply to names used to specify |
| 3431 | // explicit instantiations. |
| 3432 | // We extend this to also cover explicit specializations. Note that |
| 3433 | // we don't suppress if this turns out to be an elaborated type |
| 3434 | // specifier. |
| 3435 | bool shouldDelayDiagsInTag = |
| 3436 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 3437 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 3438 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3439 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3440 | // Enum definitions should not be parsed in a trailing-return-type. |
| 3441 | bool AllowDeclaration = DSC != DSC_trailing; |
| 3442 | |
| 3443 | bool AllowFixedUnderlyingType = AllowDeclaration && |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3444 | (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt || |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3445 | getLangOpts().ObjC2); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3446 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3447 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3448 | if (getLangOpts().CPlusPlus) { |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3449 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 3450 | // if a fixed underlying type is allowed. |
| 3451 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3452 | |
| 3453 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Richard Smith | 725fe0e | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 3454 | /*EnteringContext=*/true)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3455 | return; |
| 3456 | |
| 3457 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3458 | Diag(Tok, diag::err_expected_ident); |
| 3459 | if (Tok.isNot(tok::l_brace)) { |
| 3460 | // Has no name and is not a definition. |
| 3461 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3462 | SkipUntil(tok::comma, true); |
| 3463 | return; |
| 3464 | } |
| 3465 | } |
| 3466 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3467 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3468 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3469 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3470 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3471 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3472 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3473 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3474 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3475 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3476 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3477 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3478 | // If an identifier is present, consume and remember it. |
| 3479 | IdentifierInfo *Name = 0; |
| 3480 | SourceLocation NameLoc; |
| 3481 | if (Tok.is(tok::identifier)) { |
| 3482 | Name = Tok.getIdentifierInfo(); |
| 3483 | NameLoc = ConsumeToken(); |
| 3484 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3485 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3486 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3487 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 3488 | // declaration of a scoped enumeration. |
| 3489 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3490 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3491 | IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3492 | } |
| 3493 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3494 | // Okay, end the suppression area. We'll decide whether to emit the |
| 3495 | // diagnostics in a second. |
| 3496 | if (shouldDelayDiagsInTag) |
| 3497 | diagsFromTag.done(); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3498 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3499 | TypeResult BaseType; |
| 3500 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3501 | // Parse the fixed underlying type. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3502 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3503 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3504 | bool PossibleBitfield = false; |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3505 | if (CanBeBitfield) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3506 | // If we're in class scope, this can either be an enum declaration with |
| 3507 | // an underlying type, or a declaration of a bitfield member. We try to |
| 3508 | // use a simple disambiguation scheme first to catch the common cases |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3509 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 3510 | // anything that's a simple-type-specifier followed by '(' as an |
| 3511 | // expression. This suffices because function types are not valid |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3512 | // underlying types anyway. |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 3513 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 3514 | Sema::ConstantEvaluated); |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3515 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3516 | // 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] | 3517 | // bit-field. This is the common case. |
| 3518 | if (TPR == TPResult::True()) |
| 3519 | PossibleBitfield = true; |
| 3520 | // If the next token starts a type-specifier-seq, it may be either a |
| 3521 | // 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] | 3522 | // 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] | 3523 | // fixed underlying type. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3524 | else if (TPR == TPResult::False() && |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3525 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 3526 | // Consume the ':'. |
| 3527 | ConsumeToken(); |
| 3528 | } else { |
| 3529 | // We have the start of a type-specifier-seq, so we have to perform |
| 3530 | // tentative parsing to determine whether we have an expression or a |
| 3531 | // type. |
| 3532 | TentativeParsingAction TPA(*this); |
| 3533 | |
| 3534 | // Consume the ':'. |
| 3535 | ConsumeToken(); |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3536 | |
| 3537 | // If we see a type specifier followed by an open-brace, we have an |
| 3538 | // ambiguity between an underlying type and a C++11 braced |
| 3539 | // function-style cast. Resolve this by always treating it as an |
| 3540 | // underlying type. |
| 3541 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 3542 | // this case. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3543 | if ((getLangOpts().CPlusPlus && |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3544 | isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3545 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3546 | // We'll parse this as a bitfield later. |
| 3547 | PossibleBitfield = true; |
| 3548 | TPA.Revert(); |
| 3549 | } else { |
| 3550 | // We have a type-specifier-seq. |
| 3551 | TPA.Commit(); |
| 3552 | } |
| 3553 | } |
| 3554 | } else { |
| 3555 | // Consume the ':'. |
| 3556 | ConsumeToken(); |
| 3557 | } |
| 3558 | |
| 3559 | if (!PossibleBitfield) { |
| 3560 | SourceRange Range; |
| 3561 | BaseType = ParseTypeName(&Range); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3562 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3563 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3564 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Eli Friedman | cef3a7b | 2012-11-02 01:34:28 +0000 | [diff] [blame] | 3565 | } else if (!getLangOpts().ObjC2) { |
| 3566 | if (getLangOpts().CPlusPlus) |
| 3567 | Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range; |
| 3568 | else |
| 3569 | Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range; |
| 3570 | } |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3571 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3572 | } |
| 3573 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3574 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 3575 | // friend declaration, and cannot have an accompanying definition. If we have |
| 3576 | // 'enum foo;', then this is a forward declaration. If we have |
| 3577 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 3578 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3579 | // |
| 3580 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 3581 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 3582 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 3583 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3584 | Sema::TagUseKind TUK; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3585 | if (!AllowDeclaration) { |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3586 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3587 | } else if (Tok.is(tok::l_brace)) { |
| 3588 | if (DS.isFriendSpecified()) { |
| 3589 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
| 3590 | << SourceRange(DS.getFriendSpecLoc()); |
| 3591 | ConsumeBrace(); |
| 3592 | SkipUntil(tok::r_brace); |
| 3593 | TUK = Sema::TUK_Friend; |
| 3594 | } else { |
| 3595 | TUK = Sema::TUK_Definition; |
| 3596 | } |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3597 | } else if (DSC != DSC_type_specifier && |
| 3598 | (Tok.is(tok::semi) || |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3599 | (Tok.isAtStartOfLine() && |
| 3600 | !isValidAfterTypeSpecifier(CanBeBitfield)))) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3601 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
| 3602 | if (Tok.isNot(tok::semi)) { |
| 3603 | // A semicolon was missing after this declaration. Diagnose and recover. |
| 3604 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, |
| 3605 | "enum"); |
| 3606 | PP.EnterToken(Tok); |
| 3607 | Tok.setKind(tok::semi); |
| 3608 | } |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3609 | } else { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3610 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3611 | } |
| 3612 | |
| 3613 | // If this is an elaborated type specifier, and we delayed |
| 3614 | // diagnostics before, just merge them into the current pool. |
| 3615 | if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) { |
| 3616 | diagsFromTag.redelay(); |
| 3617 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3618 | |
| 3619 | MultiTemplateParamsArg TParams; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3620 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3621 | TUK != Sema::TUK_Reference) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3622 | if (!getLangOpts().CPlusPlus11 || !SS.isSet()) { |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3623 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3624 | Diag(Tok, diag::err_enum_template); |
| 3625 | SkipUntil(tok::comma, true); |
| 3626 | return; |
| 3627 | } |
| 3628 | |
| 3629 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 3630 | // Enumerations can't be explicitly instantiated. |
| 3631 | DS.SetTypeSpecError(); |
| 3632 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 3633 | return; |
| 3634 | } |
| 3635 | |
| 3636 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 3637 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 3638 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3639 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3640 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3641 | if (TUK == Sema::TUK_Reference) |
| 3642 | ProhibitAttributes(attrs); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3643 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3644 | if (!Name && TUK != Sema::TUK_Definition) { |
| 3645 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3646 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3647 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3648 | SkipUntil(tok::comma, true); |
| 3649 | return; |
| 3650 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3651 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3652 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3653 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3654 | const char *PrevSpec = 0; |
| 3655 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3656 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3657 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3658 | AS, DS.getModulePrivateSpecLoc(), TParams, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3659 | Owned, IsDependent, ScopedEnumKWLoc, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3660 | IsScopedUsingClassTag, BaseType); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3661 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3662 | if (IsDependent) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3663 | // This enum has a dependent nested-name-specifier. Handle it as a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3664 | // dependent tag. |
| 3665 | if (!Name) { |
| 3666 | DS.SetTypeSpecError(); |
| 3667 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 3668 | return; |
| 3669 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3670 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3671 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3672 | TUK, SS, Name, StartLoc, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3673 | NameLoc); |
| 3674 | if (Type.isInvalid()) { |
| 3675 | DS.SetTypeSpecError(); |
| 3676 | return; |
| 3677 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3678 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3679 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 3680 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3681 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3682 | Diag(StartLoc, DiagID) << PrevSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3683 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3684 | return; |
| 3685 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3686 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3687 | if (!TagDecl) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3688 | // The action failed to produce an enumeration tag. If this is a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3689 | // definition, consume the entire definition. |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3690 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3691 | ConsumeBrace(); |
| 3692 | SkipUntil(tok::r_brace); |
| 3693 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3694 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3695 | DS.SetTypeSpecError(); |
| 3696 | return; |
| 3697 | } |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3698 | |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3699 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3700 | ParseEnumBody(StartLoc, TagDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3701 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3702 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3703 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3704 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3705 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3706 | } |
| 3707 | |
| 3708 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3709 | /// enumerator-list: |
| 3710 | /// enumerator |
| 3711 | /// enumerator-list ',' enumerator |
| 3712 | /// enumerator: |
| 3713 | /// enumeration-constant |
| 3714 | /// enumeration-constant '=' constant-expression |
| 3715 | /// enumeration-constant: |
| 3716 | /// identifier |
| 3717 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3718 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3719 | // Enter the scope of the enum body and start the definition. |
| 3720 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3721 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3722 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3723 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3724 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3725 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3726 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3727 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3728 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3729 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3730 | SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3731 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3732 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3733 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3734 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3735 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3736 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3737 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3738 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3739 | // If attributes exist after the enumerator, parse them. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3740 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3741 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3742 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3743 | ProhibitAttributes(attrs); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3744 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3745 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3746 | ExprResult AssignedVal; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 3747 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3748 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3749 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3750 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3751 | AssignedVal = ParseConstantExpression(); |
| 3752 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3753 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3754 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3755 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3756 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3757 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3758 | LastEnumConstDecl, |
| 3759 | IdentLoc, Ident, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3760 | attrs.getList(), EqualLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3761 | AssignedVal.release()); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3762 | PD.complete(EnumConstDecl); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3763 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3764 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3765 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3766 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3767 | if (Tok.is(tok::identifier)) { |
| 3768 | // We're missing a comma between enumerators. |
| 3769 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3770 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3771 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3772 | continue; |
| 3773 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3774 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3775 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3776 | break; |
| 3777 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3778 | |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3779 | if (Tok.isNot(tok::identifier)) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3780 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11) |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3781 | Diag(CommaLoc, getLangOpts().CPlusPlus ? |
| 3782 | diag::ext_enumerator_list_comma_cxx : |
| 3783 | diag::ext_enumerator_list_comma_c) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3784 | << FixItHint::CreateRemoval(CommaLoc); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3785 | else if (getLangOpts().CPlusPlus11) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3786 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3787 | << FixItHint::CreateRemoval(CommaLoc); |
| 3788 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3789 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3790 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3791 | // Eat the }. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3792 | T.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3793 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3794 | // If attributes exist after the identifier list, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3795 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3796 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3797 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3798 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 3799 | EnumDecl, EnumConstantDecls, |
| 3800 | getCurScope(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3801 | attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3802 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3803 | EnumScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3804 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3805 | T.getCloseLocation()); |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3806 | |
| 3807 | // The next token must be valid after an enum definition. If not, a ';' |
| 3808 | // was probably forgotten. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3809 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
| 3810 | if (!isValidAfterTypeSpecifier(CanBeBitfield)) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3811 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum"); |
| 3812 | // Push this token back into the preprocessor and change our current token |
| 3813 | // to ';' so that the rest of the code recovers as though there were an |
| 3814 | // ';' after the definition. |
| 3815 | PP.EnterToken(Tok); |
| 3816 | Tok.setKind(tok::semi); |
| 3817 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3818 | } |
| 3819 | |
| 3820 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3821 | /// start of a type-qualifier-list. |
| 3822 | bool Parser::isTypeQualifier() const { |
| 3823 | switch (Tok.getKind()) { |
| 3824 | default: return false; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3825 | |
| 3826 | // type-qualifier only in OpenCL |
| 3827 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3828 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3829 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3830 | // type-qualifier |
| 3831 | case tok::kw_const: |
| 3832 | case tok::kw_volatile: |
| 3833 | case tok::kw_restrict: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3834 | case tok::kw___private: |
| 3835 | case tok::kw___local: |
| 3836 | case tok::kw___global: |
| 3837 | case tok::kw___constant: |
| 3838 | case tok::kw___read_only: |
| 3839 | case tok::kw___read_write: |
| 3840 | case tok::kw___write_only: |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3841 | return true; |
| 3842 | } |
| 3843 | } |
| 3844 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3845 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3846 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3847 | /// specifier or if we're not sure. |
| 3848 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3849 | switch (Tok.getKind()) { |
| 3850 | default: return false; |
| 3851 | // type-specifiers |
| 3852 | case tok::kw_short: |
| 3853 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3854 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3855 | case tok::kw___int128: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3856 | case tok::kw_signed: |
| 3857 | case tok::kw_unsigned: |
| 3858 | case tok::kw__Complex: |
| 3859 | case tok::kw__Imaginary: |
| 3860 | case tok::kw_void: |
| 3861 | case tok::kw_char: |
| 3862 | case tok::kw_wchar_t: |
| 3863 | case tok::kw_char16_t: |
| 3864 | case tok::kw_char32_t: |
| 3865 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3866 | case tok::kw_half: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3867 | case tok::kw_float: |
| 3868 | case tok::kw_double: |
| 3869 | case tok::kw_bool: |
| 3870 | case tok::kw__Bool: |
| 3871 | case tok::kw__Decimal32: |
| 3872 | case tok::kw__Decimal64: |
| 3873 | case tok::kw__Decimal128: |
| 3874 | case tok::kw___vector: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3875 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3876 | // OpenCL specific types: |
| 3877 | case tok::kw_image1d_t: |
| 3878 | case tok::kw_image1d_array_t: |
| 3879 | case tok::kw_image1d_buffer_t: |
| 3880 | case tok::kw_image2d_t: |
| 3881 | case tok::kw_image2d_array_t: |
| 3882 | case tok::kw_image3d_t: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3883 | case tok::kw_sampler_t: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3884 | case tok::kw_event_t: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3885 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3886 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3887 | case tok::kw_class: |
| 3888 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3889 | case tok::kw___interface: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3890 | case tok::kw_union: |
| 3891 | // enum-specifier |
| 3892 | case tok::kw_enum: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3893 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3894 | // typedef-name |
| 3895 | case tok::annot_typename: |
| 3896 | return true; |
| 3897 | } |
| 3898 | } |
| 3899 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3900 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3901 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3902 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3903 | switch (Tok.getKind()) { |
| 3904 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3905 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3906 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3907 | if (TryAltiVecVectorToken()) |
| 3908 | return true; |
| 3909 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3910 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3911 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3912 | // recurse to handle whatever we get. |
| 3913 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3914 | return true; |
| 3915 | if (Tok.is(tok::identifier)) |
| 3916 | return false; |
| 3917 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3918 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3919 | case tok::coloncolon: // ::foo::bar |
| 3920 | if (NextToken().is(tok::kw_new) || // ::new |
| 3921 | NextToken().is(tok::kw_delete)) // ::delete |
| 3922 | return false; |
| 3923 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3924 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3925 | return true; |
| 3926 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3927 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3928 | // GNU attributes support. |
| 3929 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3930 | // GNU typeof support. |
| 3931 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3932 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3933 | // type-specifiers |
| 3934 | case tok::kw_short: |
| 3935 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3936 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3937 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3938 | case tok::kw_signed: |
| 3939 | case tok::kw_unsigned: |
| 3940 | case tok::kw__Complex: |
| 3941 | case tok::kw__Imaginary: |
| 3942 | case tok::kw_void: |
| 3943 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3944 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3945 | case tok::kw_char16_t: |
| 3946 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3947 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3948 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3949 | case tok::kw_float: |
| 3950 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3951 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3952 | case tok::kw__Bool: |
| 3953 | case tok::kw__Decimal32: |
| 3954 | case tok::kw__Decimal64: |
| 3955 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3956 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3957 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3958 | // OpenCL specific types: |
| 3959 | case tok::kw_image1d_t: |
| 3960 | case tok::kw_image1d_array_t: |
| 3961 | case tok::kw_image1d_buffer_t: |
| 3962 | case tok::kw_image2d_t: |
| 3963 | case tok::kw_image2d_array_t: |
| 3964 | case tok::kw_image3d_t: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3965 | case tok::kw_sampler_t: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3966 | case tok::kw_event_t: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3967 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3968 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3969 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3970 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3971 | case tok::kw___interface: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3972 | case tok::kw_union: |
| 3973 | // enum-specifier |
| 3974 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3975 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3976 | // type-qualifier |
| 3977 | case tok::kw_const: |
| 3978 | case tok::kw_volatile: |
| 3979 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3980 | |
John McCall | b8a8de3 | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 3981 | // Debugger support. |
| 3982 | case tok::kw___unknown_anytype: |
| 3983 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3984 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 3985 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3986 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3987 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 3988 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3989 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3990 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3991 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3992 | case tok::kw___cdecl: |
| 3993 | case tok::kw___stdcall: |
| 3994 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3995 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3996 | case tok::kw___w64: |
| 3997 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3998 | case tok::kw___ptr32: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3999 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4000 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4001 | |
| 4002 | case tok::kw___private: |
| 4003 | case tok::kw___local: |
| 4004 | case tok::kw___global: |
| 4005 | case tok::kw___constant: |
| 4006 | case tok::kw___read_only: |
| 4007 | case tok::kw___read_write: |
| 4008 | case tok::kw___write_only: |
| 4009 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4010 | return true; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4011 | |
| 4012 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4013 | return getLangOpts().OpenCL; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4014 | |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4015 | // C11 _Atomic |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4016 | case tok::kw__Atomic: |
| 4017 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4018 | } |
| 4019 | } |
| 4020 | |
| 4021 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 4022 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4023 | /// |
| 4024 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 4025 | /// this check is to disambiguate between an expression and a declaration. |
| 4026 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4027 | switch (Tok.getKind()) { |
| 4028 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4029 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4030 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4031 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4032 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4033 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4034 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4035 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4036 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4037 | if (TryAltiVecVectorToken()) |
| 4038 | return true; |
| 4039 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4040 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4041 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4042 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4043 | // recurse to handle whatever we get. |
| 4044 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4045 | return true; |
| 4046 | if (Tok.is(tok::identifier)) |
| 4047 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4048 | |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4049 | // 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] | 4050 | // by an identifier and then either ':' or ']', in a place where an |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4051 | // expression is permitted, then this is probably a class message send |
| 4052 | // missing the initial '['. In this case, we won't consider this to be |
| 4053 | // the start of a declaration. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4054 | if (DisambiguatingWithExpression && |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4055 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 4056 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4057 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4058 | return isDeclarationSpecifier(); |
| 4059 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4060 | case tok::coloncolon: // ::foo::bar |
| 4061 | if (NextToken().is(tok::kw_new) || // ::new |
| 4062 | NextToken().is(tok::kw_delete)) // ::delete |
| 4063 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4064 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4065 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4066 | // recurse to handle whatever we get. |
| 4067 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4068 | return true; |
| 4069 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4070 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4071 | // storage-class-specifier |
| 4072 | case tok::kw_typedef: |
| 4073 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 4074 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4075 | case tok::kw_static: |
| 4076 | case tok::kw_auto: |
| 4077 | case tok::kw_register: |
| 4078 | case tok::kw___thread: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4079 | case tok::kw_thread_local: |
| 4080 | case tok::kw__Thread_local: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4081 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 4082 | // Modules |
| 4083 | case tok::kw___module_private__: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4084 | |
John McCall | b8a8de3 | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4085 | // Debugger support |
| 4086 | case tok::kw___unknown_anytype: |
| 4087 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4088 | // type-specifiers |
| 4089 | case tok::kw_short: |
| 4090 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4091 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4092 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4093 | case tok::kw_signed: |
| 4094 | case tok::kw_unsigned: |
| 4095 | case tok::kw__Complex: |
| 4096 | case tok::kw__Imaginary: |
| 4097 | case tok::kw_void: |
| 4098 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4099 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4100 | case tok::kw_char16_t: |
| 4101 | case tok::kw_char32_t: |
| 4102 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4103 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4104 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4105 | case tok::kw_float: |
| 4106 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4107 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4108 | case tok::kw__Bool: |
| 4109 | case tok::kw__Decimal32: |
| 4110 | case tok::kw__Decimal64: |
| 4111 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4112 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4113 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 4114 | // OpenCL specific types: |
| 4115 | case tok::kw_image1d_t: |
| 4116 | case tok::kw_image1d_array_t: |
| 4117 | case tok::kw_image1d_buffer_t: |
| 4118 | case tok::kw_image2d_t: |
| 4119 | case tok::kw_image2d_array_t: |
| 4120 | case tok::kw_image3d_t: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4121 | case tok::kw_sampler_t: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4122 | case tok::kw_event_t: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 4123 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4124 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4125 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4126 | case tok::kw_struct: |
| 4127 | case tok::kw_union: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4128 | case tok::kw___interface: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4129 | // enum-specifier |
| 4130 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4131 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4132 | // type-qualifier |
| 4133 | case tok::kw_const: |
| 4134 | case tok::kw_volatile: |
| 4135 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4136 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4137 | // function-specifier |
| 4138 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4139 | case tok::kw_virtual: |
| 4140 | case tok::kw_explicit: |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4141 | case tok::kw__Noreturn: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4142 | |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 4143 | // alignment-specifier |
| 4144 | case tok::kw__Alignas: |
| 4145 | |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4146 | // friend keyword. |
| 4147 | case tok::kw_friend: |
| 4148 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 4149 | // static_assert-declaration |
| 4150 | case tok::kw__Static_assert: |
| 4151 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4152 | // GNU typeof support. |
| 4153 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4154 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4155 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4156 | case tok::kw___attribute: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4157 | |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4158 | // C++11 decltype and constexpr. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4159 | case tok::annot_decltype: |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4160 | case tok::kw_constexpr: |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 4161 | |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4162 | // C11 _Atomic |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4163 | case tok::kw__Atomic: |
| 4164 | return true; |
| 4165 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 4166 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4167 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4168 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4169 | |
Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 4170 | // typedef-name |
| 4171 | case tok::annot_typename: |
| 4172 | return !DisambiguatingWithExpression || |
| 4173 | !isStartOfObjCClassMessageMissingOpenBracket(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4174 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 4175 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4176 | case tok::kw___cdecl: |
| 4177 | case tok::kw___stdcall: |
| 4178 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4179 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4180 | case tok::kw___w64: |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4181 | case tok::kw___sptr: |
| 4182 | case tok::kw___uptr: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4183 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4184 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4185 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4186 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4187 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4188 | |
| 4189 | case tok::kw___private: |
| 4190 | case tok::kw___local: |
| 4191 | case tok::kw___global: |
| 4192 | case tok::kw___constant: |
| 4193 | case tok::kw___read_only: |
| 4194 | case tok::kw___read_write: |
| 4195 | case tok::kw___write_only: |
| 4196 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4197 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4198 | } |
| 4199 | } |
| 4200 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4201 | bool Parser::isConstructorDeclarator() { |
| 4202 | TentativeParsingAction TPA(*this); |
| 4203 | |
| 4204 | // Parse the C++ scope specifier. |
| 4205 | CXXScopeSpec SS; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4206 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4207 | /*EnteringContext=*/true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4208 | TPA.Revert(); |
| 4209 | return false; |
| 4210 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4211 | |
| 4212 | // Parse the constructor name. |
| 4213 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 4214 | // We already know that we have a constructor name; just consume |
| 4215 | // the token. |
| 4216 | ConsumeToken(); |
| 4217 | } else { |
| 4218 | TPA.Revert(); |
| 4219 | return false; |
| 4220 | } |
| 4221 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4222 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4223 | if (Tok.isNot(tok::l_paren)) { |
| 4224 | TPA.Revert(); |
| 4225 | return false; |
| 4226 | } |
| 4227 | ConsumeParen(); |
| 4228 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4229 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 4230 | // that we have a constructor. |
| 4231 | if (Tok.is(tok::r_paren) || |
| 4232 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4233 | TPA.Revert(); |
| 4234 | return true; |
| 4235 | } |
| 4236 | |
| 4237 | // If we need to, enter the specified scope. |
| 4238 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4239 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4240 | DeclScopeObj.EnterDeclaratorScope(); |
| 4241 | |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4242 | // Optionally skip Microsoft attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4243 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4244 | MaybeParseMicrosoftAttributes(Attrs); |
| 4245 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4246 | // Check whether the next token(s) are part of a declaration |
| 4247 | // specifier, in which case we have the start of a parameter and, |
| 4248 | // therefore, we know that this is a constructor. |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4249 | bool IsConstructor = false; |
| 4250 | if (isDeclarationSpecifier()) |
| 4251 | IsConstructor = true; |
| 4252 | else if (Tok.is(tok::identifier) || |
| 4253 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 4254 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 4255 | // This might be a parenthesized member name, but is more likely to |
| 4256 | // be a constructor declaration with an invalid argument type. Keep |
| 4257 | // looking. |
| 4258 | if (Tok.is(tok::annot_cxxscope)) |
| 4259 | ConsumeToken(); |
| 4260 | ConsumeToken(); |
| 4261 | |
| 4262 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4263 | // which must have one of the following syntactic forms (see the |
| 4264 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4265 | switch (Tok.getKind()) { |
| 4266 | case tok::l_paren: |
| 4267 | // C(X ( int)); |
| 4268 | case tok::l_square: |
| 4269 | // C(X [ 5]); |
| 4270 | // C(X [ [attribute]]); |
| 4271 | case tok::coloncolon: |
| 4272 | // C(X :: Y); |
| 4273 | // C(X :: *p); |
| 4274 | case tok::r_paren: |
| 4275 | // C(X ) |
| 4276 | // Assume this isn't a constructor, rather than assuming it's a |
| 4277 | // constructor with an unnamed parameter of an ill-formed type. |
| 4278 | break; |
| 4279 | |
| 4280 | default: |
| 4281 | IsConstructor = true; |
| 4282 | break; |
| 4283 | } |
| 4284 | } |
| 4285 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4286 | TPA.Revert(); |
| 4287 | return IsConstructor; |
| 4288 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4289 | |
| 4290 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4291 | /// type-qualifier-list: [C99 6.7.5] |
| 4292 | /// type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4293 | /// [vendor] attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4294 | /// [ only if VendorAttributesAllowed=true ] |
| 4295 | /// type-qualifier-list type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4296 | /// [vendor] type-qualifier-list attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4297 | /// [ only if VendorAttributesAllowed=true ] |
| 4298 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4299 | /// [ only if CXX11AttributesAllowed=true ] |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4300 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4301 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4302 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 4303 | bool VendorAttributesAllowed, |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4304 | bool CXX11AttributesAllowed, |
| 4305 | bool AtomicAllowed) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4306 | if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed && |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4307 | isCXX11AttributeSpecifier()) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4308 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4309 | ParseCXX11Attributes(attrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4310 | DS.takeAttributesFrom(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4311 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4312 | |
| 4313 | SourceLocation EndLoc; |
| 4314 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4315 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4316 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4317 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4318 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4319 | SourceLocation Loc = Tok.getLocation(); |
| 4320 | |
| 4321 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4322 | case tok::code_completion: |
| 4323 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 4324 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4325 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4326 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4327 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4328 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4329 | break; |
| 4330 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4331 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4332 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4333 | break; |
| 4334 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4335 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4336 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4337 | break; |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4338 | case tok::kw__Atomic: |
| 4339 | if (!AtomicAllowed) |
| 4340 | goto DoneWithTypeQuals; |
| 4341 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 4342 | getLangOpts()); |
| 4343 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4344 | |
| 4345 | // OpenCL qualifiers: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4346 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4347 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4348 | goto DoneWithTypeQuals; |
| 4349 | case tok::kw___private: |
| 4350 | case tok::kw___global: |
| 4351 | case tok::kw___local: |
| 4352 | case tok::kw___constant: |
| 4353 | case tok::kw___read_only: |
| 4354 | case tok::kw___write_only: |
| 4355 | case tok::kw___read_write: |
| 4356 | ParseOpenCLQualifiers(DS); |
| 4357 | break; |
| 4358 | |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4359 | case tok::kw___sptr: |
| 4360 | case tok::kw___uptr: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4361 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 4362 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4363 | case tok::kw___ptr32: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4364 | case tok::kw___cdecl: |
| 4365 | case tok::kw___stdcall: |
| 4366 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4367 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4368 | case tok::kw___unaligned: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4369 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4370 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4371 | continue; |
| 4372 | } |
| 4373 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4374 | case tok::kw___pascal: |
| 4375 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4376 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4377 | continue; |
| 4378 | } |
| 4379 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4380 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4381 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4382 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4383 | continue; // do *not* consume the next token! |
| 4384 | } |
| 4385 | // otherwise, FALL THROUGH! |
| 4386 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4387 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4388 | // If this is not a type-qualifier token, we're done reading type |
| 4389 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 4390 | DS.Finish(Diags, PP); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4391 | if (EndLoc.isValid()) |
| 4392 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4393 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4394 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4395 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4396 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 4397 | if (isInvalid) { |
| 4398 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4399 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4400 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4401 | EndLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4402 | } |
| 4403 | } |
| 4404 | |
| 4405 | |
| 4406 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 4407 | /// |
| 4408 | void Parser::ParseDeclarator(Declarator &D) { |
| 4409 | /// This implements the 'declarator' production in the C grammar, then checks |
| 4410 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4411 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4412 | } |
| 4413 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4414 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) { |
| 4415 | if (Kind == tok::star || Kind == tok::caret) |
| 4416 | return true; |
| 4417 | |
| 4418 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 4419 | if (!Lang.CPlusPlus) |
| 4420 | return false; |
| 4421 | |
| 4422 | return Kind == tok::amp || Kind == tok::ampamp; |
| 4423 | } |
| 4424 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4425 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 4426 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 4427 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4428 | /// ptr-operator production. |
| 4429 | /// |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4430 | /// 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] | 4431 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 4432 | /// isConstructorDeclarator. |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4433 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4434 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 4435 | /// [C] pointer[opt] direct-declarator |
| 4436 | /// [C++] direct-declarator |
| 4437 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4438 | /// |
| 4439 | /// pointer: [C99 6.7.5] |
| 4440 | /// '*' type-qualifier-list[opt] |
| 4441 | /// '*' type-qualifier-list[opt] pointer |
| 4442 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4443 | /// ptr-operator: |
| 4444 | /// '*' cv-qualifier-seq[opt] |
| 4445 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4446 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4447 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4448 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4449 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4450 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 4451 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 4452 | if (Diags.hasAllExtensionsSilenced()) |
| 4453 | D.setExtension(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4454 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4455 | // C++ member pointers start with a '::' or a nested-name. |
| 4456 | // Member pointers get special handling, since there's no place for the |
| 4457 | // scope spec in the generic path below. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4458 | if (getLangOpts().CPlusPlus && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 4459 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 4460 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4461 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4462 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4463 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4464 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4465 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 4466 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4467 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4468 | // The scope spec really belongs to the direct-declarator. |
Richard Smith | 6a502c4 | 2013-01-08 22:43:49 +0000 | [diff] [blame] | 4469 | if (D.mayHaveIdentifier()) |
| 4470 | D.getCXXScopeSpec() = SS; |
| 4471 | else |
| 4472 | AnnotateScopeToken(SS, true); |
| 4473 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4474 | if (DirectDeclParser) |
| 4475 | (this->*DirectDeclParser)(D); |
| 4476 | return; |
| 4477 | } |
| 4478 | |
| 4479 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4480 | D.SetRangeEnd(Loc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4481 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4482 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4483 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4484 | |
| 4485 | // Recurse to parse whatever is left. |
| 4486 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 4487 | |
| 4488 | // Sema will have to catch (syntactically invalid) pointers into global |
| 4489 | // scope. It has to catch pointers into namespace scope anyway. |
| 4490 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4491 | Loc), |
| 4492 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4493 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4494 | return; |
| 4495 | } |
| 4496 | } |
| 4497 | |
| 4498 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4499 | // Not a pointer, C++ reference, or block. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4500 | if (!isPtrOperatorToken(Kind, getLangOpts())) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4501 | if (DirectDeclParser) |
| 4502 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4503 | return; |
| 4504 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4505 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4506 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 4507 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4508 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4509 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4510 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 4511 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4512 | // Is a pointer. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4513 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4514 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4515 | // FIXME: GNU attributes are not allowed here in a new-type-id. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4516 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4517 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4518 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4519 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4520 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4521 | if (Kind == tok::star) |
| 4522 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 4523 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 4524 | DS.getConstSpecLoc(), |
| 4525 | DS.getVolatileSpecLoc(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4526 | DS.getRestrictSpecLoc()), |
| 4527 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4528 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4529 | else |
| 4530 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4531 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4532 | Loc), |
| 4533 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4534 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4535 | } else { |
| 4536 | // Is a reference |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4537 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4538 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4539 | // Complain about rvalue references in C++03, but then go on and build |
| 4540 | // the declarator. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4541 | if (Kind == tok::ampamp) |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4542 | Diag(Loc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4543 | diag::warn_cxx98_compat_rvalue_reference : |
| 4544 | diag::ext_rvalue_reference); |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4545 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4546 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 4547 | ParseTypeQualifierListOpt(DS); |
| 4548 | D.ExtendWithDeclSpec(DS); |
| 4549 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4550 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 4551 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 4552 | // template type argument, in which case the cv-qualifiers are ignored. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4553 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 4554 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 4555 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4556 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4557 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 4558 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4559 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4560 | // 'restrict' is permitted as an extension. |
| 4561 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 4562 | Diag(DS.getAtomicSpecLoc(), |
| 4563 | diag::err_invalid_reference_qualifier_application) << "_Atomic"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4564 | } |
| 4565 | |
| 4566 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4567 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4568 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4569 | if (D.getNumTypeObjects() > 0) { |
| 4570 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 4571 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 4572 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 4573 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 4574 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4575 | << II; |
| 4576 | else |
| 4577 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4578 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4579 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4580 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4581 | // can go ahead and build the (technically ill-formed) |
| 4582 | // declarator: reference collapsing will take care of it. |
| 4583 | } |
| 4584 | } |
| 4585 | |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4586 | // Remember that we parsed a reference type. |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4587 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4588 | Kind == tok::amp), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4589 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4590 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4591 | } |
| 4592 | } |
| 4593 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4594 | static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D, |
| 4595 | SourceLocation EllipsisLoc) { |
| 4596 | if (EllipsisLoc.isValid()) { |
| 4597 | FixItHint Insertion; |
| 4598 | if (!D.getEllipsisLoc().isValid()) { |
| 4599 | Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "..."); |
| 4600 | D.setEllipsisLoc(EllipsisLoc); |
| 4601 | } |
| 4602 | P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) |
| 4603 | << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName(); |
| 4604 | } |
| 4605 | } |
| 4606 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4607 | /// ParseDirectDeclarator |
| 4608 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4609 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4610 | /// '(' declarator ')' |
| 4611 | /// [GNU] '(' attributes declarator ')' |
| 4612 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4613 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4614 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4615 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4616 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4617 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 4618 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4619 | /// direct-declarator '(' parameter-type-list ')' |
| 4620 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4621 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4622 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 4623 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 4624 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4625 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 4626 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 4627 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4628 | /// [C++] declarator-id |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4629 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4630 | /// |
| 4631 | /// declarator-id: [C++ 8] |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4632 | /// '...'[opt] id-expression |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4633 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 4634 | /// |
| 4635 | /// id-expression: [C++ 5.1] |
| 4636 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4637 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4638 | /// |
| 4639 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4640 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4641 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4642 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4643 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 4644 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 4645 | /// |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4646 | /// Note, any additional constructs added here may need corresponding changes |
| 4647 | /// in isConstructorDeclarator. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4648 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4649 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4650 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4651 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4652 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4653 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4654 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4655 | D.getContext() == Declarator::MemberContext; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4656 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4657 | EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4658 | } |
| 4659 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4660 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4661 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 4662 | // Change the declaration context for name lookup, until this function |
| 4663 | // is exited (and the declarator has been parsed). |
| 4664 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4665 | } |
| 4666 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4667 | // C++0x [dcl.fct]p14: |
| 4668 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4669 | // of a parameter-declaration-clause without a preceding comma. In |
| 4670 | // this case, the ellipsis is parsed as part of the |
| 4671 | // abstract-declarator if the type of the parameter names a template |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4672 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 4673 | // as part of the parameter-declaration-clause. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4674 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4675 | !((D.getContext() == Declarator::PrototypeContext || |
| 4676 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4677 | NextToken().is(tok::r_paren) && |
Richard Smith | 30f2a74 | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4678 | !D.hasGroupingParens() && |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4679 | !Actions.containsUnexpandedParameterPacks(D))) { |
| 4680 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 4681 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) { |
| 4682 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 4683 | // the user what they should have done. |
| 4684 | ParseDeclarator(D); |
| 4685 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4686 | return; |
| 4687 | } else |
| 4688 | D.setEllipsisLoc(EllipsisLoc); |
| 4689 | |
| 4690 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 4691 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 4692 | // the l_paren token. |
| 4693 | } |
| 4694 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4695 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 4696 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 4697 | // We found something that indicates the start of an unqualified-id. |
| 4698 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4699 | bool AllowConstructorName; |
| 4700 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 4701 | AllowConstructorName = false; |
| 4702 | else if (D.getCXXScopeSpec().isSet()) |
| 4703 | AllowConstructorName = |
| 4704 | (D.getContext() == Declarator::FileContext || |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 4705 | D.getContext() == Declarator::MemberContext); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4706 | else |
| 4707 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 4708 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4709 | SourceLocation TemplateKWLoc; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4710 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 4711 | /*EnteringContext=*/true, |
| 4712 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4713 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4714 | ParsedType(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4715 | TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4716 | D.getName()) || |
| 4717 | // Once we're past the identifier, if the scope was bad, mark the |
| 4718 | // whole declarator bad. |
| 4719 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4720 | D.SetIdentifier(0, Tok.getLocation()); |
| 4721 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4722 | } else { |
| 4723 | // Parsed the unqualified-id; update range information and move along. |
| 4724 | if (D.getSourceRange().getBegin().isInvalid()) |
| 4725 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 4726 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4727 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4728 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4729 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4730 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4731 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4732 | "There's a C++-specific check for tok::identifier above"); |
| 4733 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 4734 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 4735 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4736 | goto PastIdentifier; |
Richard Smith | a38253c | 2013-07-11 05:10:21 +0000 | [diff] [blame] | 4737 | } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) { |
| 4738 | Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id) |
| 4739 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 4740 | D.SetIdentifier(0, Tok.getLocation()); |
| 4741 | ConsumeToken(); |
| 4742 | goto PastIdentifier; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4743 | } |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4744 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4745 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4746 | // direct-declarator: '(' declarator ')' |
| 4747 | // direct-declarator: '(' attributes declarator ')' |
| 4748 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 4749 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4750 | |
| 4751 | // If the declarator was parenthesized, we entered the declarator |
| 4752 | // scope when parsing the parenthesized declarator, then exited |
| 4753 | // the scope already. Re-enter the scope, if we need to. |
| 4754 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4755 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4756 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4757 | if (!D.isInvalidType() && |
| 4758 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4759 | // Change the declaration context for name lookup, until this function |
| 4760 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4761 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4762 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4763 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4764 | // This could be something simple like "int" (in which case the declarator |
| 4765 | // portion is empty), if an abstract-declarator is allowed. |
| 4766 | D.SetIdentifier(0, Tok.getLocation()); |
Richard Smith | 30f2a74 | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4767 | |
| 4768 | // The grammar for abstract-pack-declarator does not allow grouping parens. |
| 4769 | // FIXME: Revisit this once core issue 1488 is resolved. |
| 4770 | if (D.hasEllipsis() && D.hasGroupingParens()) |
| 4771 | Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()), |
| 4772 | diag::ext_abstract_pack_declarator_parens); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4773 | } else { |
David Blaikie | e75d9cf | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 4774 | if (Tok.getKind() == tok::annot_pragma_parser_crash) |
David Blaikie | 377da4c | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 4775 | LLVM_BUILTIN_TRAP; |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 4776 | if (D.getContext() == Declarator::MemberContext) |
| 4777 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 4778 | << D.getDeclSpec().getSourceRange(); |
Richard Trieu | db55c04c | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 4779 | else if (getLangOpts().CPlusPlus) { |
| 4780 | if (Tok.is(tok::period) || Tok.is(tok::arrow)) |
| 4781 | Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow); |
| 4782 | else |
| 4783 | Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus; |
| 4784 | } else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4785 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4786 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 4787 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4788 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4789 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4790 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4791 | assert(D.isPastIdentifier() && |
| 4792 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4793 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4794 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 4795 | if (D.hasName() && !D.getNumTypeObjects()) |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4796 | MaybeParseCXX11Attributes(D); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4797 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4798 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4799 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4800 | // Enter function-declaration scope, limiting any declarators to the |
| 4801 | // function prototype scope, including parameter declarators. |
| 4802 | ParseScope PrototypeScope(this, |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4803 | Scope::FunctionPrototypeScope|Scope::DeclScope| |
| 4804 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 4805 | ? Scope::FunctionDeclarationScope : 0)); |
| 4806 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4807 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 4808 | // In such a case, check if we actually have a function declarator; if it |
| 4809 | // is not, the declarator has been fully parsed. |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4810 | bool IsAmbiguous = false; |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 4811 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 4812 | // The name of the declarator, if any, is tentatively declared within |
| 4813 | // a possible direct initializer. |
| 4814 | TentativelyDeclaredIdentifiers.push_back(D.getIdentifier()); |
| 4815 | bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous); |
| 4816 | TentativelyDeclaredIdentifiers.pop_back(); |
| 4817 | if (!IsFunctionDecl) |
| 4818 | break; |
| 4819 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4820 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4821 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4822 | T.consumeOpen(); |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4823 | ParseFunctionDeclarator(D, attrs, T, IsAmbiguous); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4824 | PrototypeScope.Exit(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4825 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4826 | ParseBracketDeclarator(D); |
| 4827 | } else { |
| 4828 | break; |
| 4829 | } |
| 4830 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4831 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4832 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4833 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 4834 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4835 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4836 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 4837 | /// |
| 4838 | /// direct-declarator: |
| 4839 | /// '(' declarator ')' |
| 4840 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4841 | /// direct-declarator '(' parameter-type-list ')' |
| 4842 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4843 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4844 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4845 | /// |
| 4846 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4847 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4848 | T.consumeOpen(); |
| 4849 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4850 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4851 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4852 | // Eat any attributes before we look at whether this is a grouping or function |
| 4853 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 4854 | // the type being built up, for example: |
| 4855 | // int (__attribute__(()) *x)(long y) |
| 4856 | // If this ends up not being a grouping paren, the attribute applies to the |
| 4857 | // first argument, for example: |
| 4858 | // int (__attribute__(()) int x) |
| 4859 | // In either case, we need to eat any attributes to be able to determine what |
| 4860 | // sort of paren this is. |
| 4861 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4862 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4863 | bool RequiresArg = false; |
| 4864 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4865 | ParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4866 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4867 | // We require that the argument list (if this is a non-grouping paren) be |
| 4868 | // present even if the attribute list was empty. |
| 4869 | RequiresArg = true; |
| 4870 | } |
Chad Rosier | 9cab1c9 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 4871 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4872 | // Eat any Microsoft extensions. |
Chad Rosier | 9cab1c9 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 4873 | ParseMicrosoftTypeAttributes(attrs); |
| 4874 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4875 | // Eat any Borland extensions. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 4876 | if (Tok.is(tok::kw___pascal)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4877 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4878 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4879 | // If we haven't past the identifier yet (or where the identifier would be |
| 4880 | // stored, if this is an abstract declarator), then this is probably just |
| 4881 | // grouping parens. However, if this could be an abstract-declarator, then |
| 4882 | // this could also be the start of function arguments (consider 'void()'). |
| 4883 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4884 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4885 | if (!D.mayOmitIdentifier()) { |
| 4886 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 4887 | // paren, because we haven't seen the identifier yet. |
| 4888 | isGrouping = true; |
| 4889 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4890 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 4891 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4892 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 4893 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4894 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 4895 | // considered to be a type, not a K&R identifier-list. |
| 4896 | isGrouping = false; |
| 4897 | } else { |
| 4898 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 4899 | isGrouping = true; |
| 4900 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4901 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4902 | // If this is a grouping paren, handle: |
| 4903 | // direct-declarator: '(' declarator ')' |
| 4904 | // direct-declarator: '(' attributes declarator ')' |
| 4905 | if (isGrouping) { |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4906 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 4907 | D.setEllipsisLoc(SourceLocation()); |
| 4908 | |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4909 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4910 | D.setGroupingParens(true); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4911 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4912 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4913 | T.consumeClose(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4914 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4915 | T.getCloseLocation()), |
| 4916 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4917 | |
| 4918 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4919 | |
| 4920 | // An ellipsis cannot be placed outside parentheses. |
| 4921 | if (EllipsisLoc.isValid()) |
| 4922 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4923 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4924 | return; |
| 4925 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4926 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4927 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 4928 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4929 | // identifier (and remember where it would have been), then call into |
| 4930 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4931 | D.SetIdentifier(0, Tok.getLocation()); |
| 4932 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4933 | // Enter function-declaration scope, limiting any declarators to the |
| 4934 | // function prototype scope, including parameter declarators. |
| 4935 | ParseScope PrototypeScope(this, |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4936 | Scope::FunctionPrototypeScope | Scope::DeclScope | |
| 4937 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 4938 | ? Scope::FunctionDeclarationScope : 0)); |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4939 | ParseFunctionDeclarator(D, attrs, T, false, RequiresArg); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4940 | PrototypeScope.Exit(); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4941 | } |
| 4942 | |
| 4943 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 4944 | /// declarator D up to a paren, which indicates that we are parsing function |
| 4945 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4946 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4947 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 4948 | /// immediately after the open paren - they should be considered to be the |
| 4949 | /// first argument of a parameter. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4950 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4951 | /// If RequiresArg is true, then the first argument of the function is required |
| 4952 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4953 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4954 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 4955 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 4956 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 4957 | /// |
| 4958 | /// [C++11] exception-specification: |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4959 | /// dynamic-exception-specification |
| 4960 | /// noexcept-specification |
| 4961 | /// |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4962 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4963 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4964 | BalancedDelimiterTracker &Tracker, |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4965 | bool IsAmbiguous, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4966 | bool RequiresArg) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4967 | assert(getCurScope()->isFunctionPrototypeScope() && |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4968 | "Should call from a Function scope"); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4969 | // lparen is already consumed! |
| 4970 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 4971 | |
| 4972 | // This should be true when the function has typed arguments. |
| 4973 | // Otherwise, it is treated as a K&R-style function. |
| 4974 | bool HasProto = false; |
| 4975 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4976 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4977 | // Remember where we see an ellipsis, if any. |
| 4978 | SourceLocation EllipsisLoc; |
| 4979 | |
| 4980 | DeclSpec DS(AttrFactory); |
| 4981 | bool RefQualifierIsLValueRef = true; |
| 4982 | SourceLocation RefQualifierLoc; |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4983 | SourceLocation ConstQualifierLoc; |
| 4984 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4985 | ExceptionSpecificationType ESpecType = EST_None; |
| 4986 | SourceRange ESpecRange; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4987 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 4988 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4989 | ExprResult NoexceptExpr; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4990 | ParsedAttributes FnAttrs(AttrFactory); |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 4991 | TypeResult TrailingReturnType; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4992 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4993 | Actions.ActOnStartFunctionDeclarator(); |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 4994 | |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4995 | /* LocalEndLoc is the end location for the local FunctionTypeLoc. |
| 4996 | EndLoc is the end location for the function declarator. |
| 4997 | They differ for trailing return types. */ |
| 4998 | SourceLocation StartLoc, LocalEndLoc, EndLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4999 | SourceLocation LParenLoc, RParenLoc; |
| 5000 | LParenLoc = Tracker.getOpenLocation(); |
| 5001 | StartLoc = LParenLoc; |
| 5002 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5003 | if (isFunctionDeclaratorIdentifierList()) { |
| 5004 | if (RequiresArg) |
| 5005 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5006 | |
| 5007 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 5008 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5009 | Tracker.consumeClose(); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5010 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5011 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5012 | EndLoc = RParenLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5013 | } else { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5014 | if (Tok.isNot(tok::r_paren)) |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 5015 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5016 | else if (RequiresArg) |
| 5017 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5018 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5019 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5020 | |
| 5021 | // If we have the closing ')', eat it. |
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 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5027 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5028 | // FIXME: Accept these components in any order, and produce fixits to |
| 5029 | // correct the order if the user gets it wrong. Ideally we should deal |
| 5030 | // with the virt-specifier-seq and pure-specifier in the same way. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5031 | |
| 5032 | // Parse cv-qualifier-seq[opt]. |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5033 | ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false, |
| 5034 | /*CXX11AttributesAllowed*/ false, |
| 5035 | /*AtomicAllowed*/ false); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5036 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 5037 | EndLoc = DS.getSourceRange().getEnd(); |
| 5038 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 5039 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 5040 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5041 | |
| 5042 | // Parse ref-qualifier[opt]. |
| 5043 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5044 | Diag(Tok, getLangOpts().CPlusPlus11 ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5045 | diag::warn_cxx98_compat_ref_qualifier : |
| 5046 | diag::ext_ref_qualifier); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5047 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5048 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 5049 | RefQualifierLoc = ConsumeToken(); |
| 5050 | EndLoc = RefQualifierLoc; |
| 5051 | } |
| 5052 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5053 | // C++11 [expr.prim.general]p3: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5054 | // If a declaration declares a member function or member function |
| 5055 | // 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] | 5056 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5057 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5058 | // declarator. |
Richard Smith | d922779 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5059 | // FIXME: currently, "static" case isn't handled correctly. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5060 | bool IsCXX11MemberFunction = |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5061 | getLangOpts().CPlusPlus11 && |
Richard Smith | d922779 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5062 | (D.getContext() == Declarator::MemberContext |
| 5063 | ? !D.getDeclSpec().isFriendSpecified() |
| 5064 | : D.getContext() == Declarator::FileContext && |
| 5065 | D.getCXXScopeSpec().isValid() && |
| 5066 | Actions.CurContext->isRecord()); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5067 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 5068 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
Richard Smith | 7b19cb1 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 5069 | DS.getTypeQualifiers() | |
Richard Smith | 8404626 | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 5070 | (D.getDeclSpec().isConstexprSpecified() && |
| 5071 | !getLangOpts().CPlusPlus1y |
Richard Smith | 7b19cb1 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 5072 | ? Qualifiers::Const : 0), |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5073 | IsCXX11MemberFunction); |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5074 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5075 | // Parse exception-specification[opt]. |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5076 | ESpecType = tryParseExceptionSpecification(ESpecRange, |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 5077 | DynamicExceptions, |
| 5078 | DynamicExceptionRanges, |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5079 | NoexceptExpr); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5080 | if (ESpecType != EST_None) |
| 5081 | EndLoc = ESpecRange.getEnd(); |
| 5082 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5083 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 5084 | // after the exception-specification. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5085 | MaybeParseCXX11Attributes(FnAttrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5086 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5087 | // Parse trailing-return-type[opt]. |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5088 | LocalEndLoc = EndLoc; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5089 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5090 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5091 | if (D.getDeclSpec().getTypeSpecType() == TST_auto) |
| 5092 | StartLoc = D.getDeclSpec().getTypeSpecTypeLoc(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5093 | LocalEndLoc = Tok.getLocation(); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 5094 | SourceRange Range; |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 5095 | TrailingReturnType = ParseTrailingReturnType(Range); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5096 | EndLoc = Range.getEnd(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5097 | } |
| 5098 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5099 | } |
| 5100 | |
| 5101 | // Remember that we parsed a function type, and remember the attributes. |
| 5102 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5103 | IsAmbiguous, |
| 5104 | LParenLoc, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5105 | ParamInfo.data(), ParamInfo.size(), |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5106 | EllipsisLoc, RParenLoc, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5107 | DS.getTypeQualifiers(), |
| 5108 | RefQualifierIsLValueRef, |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 5109 | RefQualifierLoc, ConstQualifierLoc, |
| 5110 | VolatileQualifierLoc, |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 5111 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5112 | ESpecType, ESpecRange.getBegin(), |
| 5113 | DynamicExceptions.data(), |
| 5114 | DynamicExceptionRanges.data(), |
| 5115 | DynamicExceptions.size(), |
| 5116 | NoexceptExpr.isUsable() ? |
| 5117 | NoexceptExpr.get() : 0, |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5118 | StartLoc, LocalEndLoc, D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5119 | TrailingReturnType), |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5120 | FnAttrs, EndLoc); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 5121 | |
| 5122 | Actions.ActOnEndFunctionDeclarator(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5123 | } |
| 5124 | |
| 5125 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 5126 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 5127 | /// |
| 5128 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 5129 | /// abstract-declarators. |
| 5130 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5131 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5132 | && Tok.is(tok::identifier) |
| 5133 | && !TryAltiVecVectorToken() |
| 5134 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 5135 | // 6.7.5.3p11. |
| 5136 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 5137 | // Identifier lists follow a really simple grammar: the identifiers can |
| 5138 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 5139 | // identifier lists are really rare in the brave new modern world, and |
| 5140 | // it is very common for someone to typo a type in a non-K&R style |
| 5141 | // list. If we are presented with something like: "void foo(intptr x, |
| 5142 | // float y)", we don't want to start parsing the function declarator as |
| 5143 | // though it is a K&R style declarator just because intptr is an |
| 5144 | // invalid type. |
| 5145 | // |
| 5146 | // To handle this, we check to see if the token after the first |
| 5147 | // identifier is a "," or ")". Only then do we parse it as an |
| 5148 | // identifier list. |
| 5149 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 5150 | } |
| 5151 | |
| 5152 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 5153 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 5154 | /// |
| 5155 | /// After returning, ParamInfo will hold the parsed parameters. |
| 5156 | /// |
| 5157 | /// identifier-list: [C99 6.7.5] |
| 5158 | /// identifier |
| 5159 | /// identifier-list ',' identifier |
| 5160 | /// |
| 5161 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 5162 | Declarator &D, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5163 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5164 | // If there was no identifier specified for the declarator, either we are in |
| 5165 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 5166 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 5167 | // diagnose this. |
| 5168 | if (!D.getIdentifier()) |
| 5169 | Diag(Tok, diag::ext_ident_list_in_param); |
| 5170 | |
| 5171 | // Maintain an efficient lookup of params we have seen so far. |
| 5172 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 5173 | |
| 5174 | while (1) { |
| 5175 | // If this isn't an identifier, report the error and skip until ')'. |
| 5176 | if (Tok.isNot(tok::identifier)) { |
| 5177 | Diag(Tok, diag::err_expected_ident); |
| 5178 | SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 5179 | // Forget we parsed anything. |
| 5180 | ParamInfo.clear(); |
| 5181 | return; |
| 5182 | } |
| 5183 | |
| 5184 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 5185 | |
| 5186 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 5187 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 5188 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 5189 | |
| 5190 | // Verify that the argument identifier has not already been mentioned. |
| 5191 | if (!ParamsSoFar.insert(ParmII)) { |
| 5192 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 5193 | } else { |
| 5194 | // Remember this identifier in ParamInfo. |
| 5195 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 5196 | Tok.getLocation(), |
| 5197 | 0)); |
| 5198 | } |
| 5199 | |
| 5200 | // Eat the identifier. |
| 5201 | ConsumeToken(); |
| 5202 | |
| 5203 | // The list continues if we see a comma. |
| 5204 | if (Tok.isNot(tok::comma)) |
| 5205 | break; |
| 5206 | ConsumeToken(); |
| 5207 | } |
| 5208 | } |
| 5209 | |
| 5210 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 5211 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 5212 | /// identifier list. |
| 5213 | /// |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5214 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 5215 | /// caller parsed those arguments immediately after the open paren - they should |
| 5216 | /// be considered to be part of the first parameter. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5217 | /// |
| 5218 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 5219 | /// be the location of the ellipsis, if any was parsed. |
| 5220 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5221 | /// parameter-type-list: [C99 6.7.5] |
| 5222 | /// parameter-list |
| 5223 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5224 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5225 | /// |
| 5226 | /// parameter-list: [C99 6.7.5] |
| 5227 | /// parameter-declaration |
| 5228 | /// parameter-list ',' parameter-declaration |
| 5229 | /// |
| 5230 | /// parameter-declaration: [C99 6.7.5] |
| 5231 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5232 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5233 | /// [C++11] initializer-clause |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5234 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 5235 | /// declaration-specifiers abstract-declarator[opt] |
| 5236 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 5237 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5238 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5239 | /// [C++11] attribute-specifier-seq parameter-declaration |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5240 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5241 | void Parser::ParseParameterDeclarationClause( |
| 5242 | Declarator &D, |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5243 | ParsedAttributes &FirstArgAttrs, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5244 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5245 | SourceLocation &EllipsisLoc) { |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 5246 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5247 | while (1) { |
| 5248 | if (Tok.is(tok::ellipsis)) { |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5249 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 5250 | // before deciding this was a parameter-declaration-clause. |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 5251 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5252 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5253 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5254 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5255 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5256 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5257 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5258 | |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5259 | // Parse any C++11 attributes. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5260 | MaybeParseCXX11Attributes(DS.getAttributes()); |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5261 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5262 | // Skip any Microsoft attributes before a param. |
Chad Rosier | 16f90bf | 2012-12-20 20:37:53 +0000 | [diff] [blame] | 5263 | MaybeParseMicrosoftAttributes(DS.getAttributes()); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5264 | |
| 5265 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5266 | |
| 5267 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5268 | // 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] | 5269 | // 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] | 5270 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 5271 | // too much hassle. |
| 5272 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5273 | |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 5274 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5275 | |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 5276 | // Parse the declarator. This is "PrototypeContext", because we must |
| 5277 | // accept either 'declarator' or 'abstract-declarator' here. |
| 5278 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 5279 | ParseDeclarator(ParmDecl); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5280 | |
| 5281 | // Parse GNU attributes, if present. |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 5282 | MaybeParseGNUAttributes(ParmDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5283 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5284 | // Remember this parsed parameter in ParamInfo. |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 5285 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5286 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5287 | // DefArgToks is used when the parsing of default arguments needs |
| 5288 | // to be delayed. |
| 5289 | CachedTokens *DefArgToks = 0; |
| 5290 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5291 | // If no parameter was specified, verify that *something* was specified, |
| 5292 | // otherwise we have a missing type and identifier. |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 5293 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 5294 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5295 | // Completely missing, emit error. |
| 5296 | Diag(DSStart, diag::err_missing_param); |
| 5297 | } else { |
| 5298 | // Otherwise, we have something. Add it and let semantic analysis try |
| 5299 | // 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] | 5300 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5301 | // Inform the actions module about the parameter declarator, so it gets |
| 5302 | // added to the current scope. |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 5303 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); |
| 5304 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5305 | // Parse the default argument, if any. We parse the default |
| 5306 | // arguments in all dialects; the semantic analysis in |
| 5307 | // ActOnParamDefaultArgument will reject the default argument in |
| 5308 | // C. |
| 5309 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5310 | SourceLocation EqualLoc = Tok.getLocation(); |
| 5311 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5312 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5313 | if (D.getContext() == Declarator::MemberContext) { |
| 5314 | // If we're inside a class definition, cache the tokens |
| 5315 | // corresponding to the default argument. We'll actually parse |
| 5316 | // them when we see the end of the class definition. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5317 | // FIXME: Can we use a smart pointer for Toks? |
| 5318 | DefArgToks = new CachedTokens; |
| 5319 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5320 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 5321 | /*StopAtSemi=*/true, |
| 5322 | /*ConsumeFinalToken=*/false)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5323 | delete DefArgToks; |
| 5324 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5325 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5326 | } else { |
| 5327 | // Mark the end of the default argument so that we know when to |
| 5328 | // stop when we parse it later on. |
| 5329 | Token DefArgEnd; |
| 5330 | DefArgEnd.startToken(); |
| 5331 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 5332 | DefArgEnd.setLocation(Tok.getLocation()); |
| 5333 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5334 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 5335 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5336 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5337 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5338 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5339 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5340 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5341 | // The argument isn't actually potentially evaluated unless it is |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5342 | // used. |
| 5343 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 5344 | Sema::PotentiallyEvaluatedIfUsed, |
| 5345 | Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5346 | |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5347 | ExprResult DefArgResult; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5348 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5349 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5350 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5351 | } else |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5352 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5353 | if (DefArgResult.isInvalid()) { |
| 5354 | Actions.ActOnParamDefaultArgumentError(Param); |
| 5355 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 5356 | } else { |
| 5357 | // Inform the actions module about the default argument |
| 5358 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5359 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5360 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5361 | } |
| 5362 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5363 | |
| 5364 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 5365 | ParmDecl.getIdentifierLoc(), Param, |
| 5366 | DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5367 | } |
| 5368 | |
| 5369 | // 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] | 5370 | if (Tok.isNot(tok::comma)) { |
| 5371 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5372 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5373 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5374 | if (!getLangOpts().CPlusPlus) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5375 | // We have ellipsis without a preceding ',', which is ill-formed |
| 5376 | // in C. Complain and provide the fix. |
| 5377 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5378 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5379 | } |
| 5380 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5381 | |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5382 | break; |
| 5383 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5384 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5385 | // Consume the comma. |
| 5386 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5387 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5388 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 5389 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5390 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5391 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 5392 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 5393 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 5394 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 5395 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5396 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 5397 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5398 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5399 | if (CheckProhibitedCXX11Attribute()) |
| 5400 | return; |
| 5401 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5402 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 5403 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5404 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5405 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 5406 | // This code does a fast path to handle some of the most obvious cases. |
| 5407 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5408 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5409 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5410 | MaybeParseCXX11Attributes(attrs); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5411 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5412 | // Remember that we parsed the empty array type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5413 | ExprResult NumElements; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5414 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5415 | T.getOpenLocation(), |
| 5416 | T.getCloseLocation()), |
| 5417 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5418 | return; |
| 5419 | } else if (Tok.getKind() == tok::numeric_constant && |
| 5420 | GetLookAheadToken(1).is(tok::r_square)) { |
| 5421 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5422 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5423 | ConsumeToken(); |
| 5424 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5425 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5426 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5427 | MaybeParseCXX11Attributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5428 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5429 | // Remember that we parsed a array type, and remember its features. |
Nikola Smiljanic | ebf0fa8 | 2013-01-11 08:33:05 +0000 | [diff] [blame] | 5430 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5431 | ExprRes.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5432 | T.getOpenLocation(), |
| 5433 | T.getCloseLocation()), |
| 5434 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5435 | return; |
| 5436 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5437 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5438 | // If valid, this location is the position where we read the 'static' keyword. |
| 5439 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5440 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5441 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5442 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5443 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5444 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5445 | DeclSpec DS(AttrFactory); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5446 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5447 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5448 | // If we haven't already read 'static', check to see if there is one after the |
| 5449 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5450 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5451 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5452 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5453 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 5454 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5455 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5456 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5457 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 5458 | // 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] | 5459 | // the token after the star is a ']'. Since stars in arrays are |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5460 | // infrequent, use of lookahead is not costly here. |
| 5461 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 5462 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5463 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5464 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5465 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5466 | StaticLoc = SourceLocation(); // Drop the static. |
| 5467 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5468 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5469 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5470 | // Note, in C89, this production uses the constant-expr production instead |
| 5471 | // of assignment-expr. The only difference is that assignment-expr allows |
| 5472 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 5473 | // 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] | 5474 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5475 | // Parse the constant-expression or assignment-expression now (depending |
| 5476 | // on dialect). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5477 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5478 | NumElements = ParseConstantExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5479 | } else { |
| 5480 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 5481 | Sema::ConstantEvaluated); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5482 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5483 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5484 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5485 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5486 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 5487 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 5488 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5489 | // If the expression was invalid, skip it. |
| 5490 | SkipUntil(tok::r_square); |
| 5491 | return; |
| 5492 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5493 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5494 | T.consumeClose(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5495 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5496 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5497 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5498 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5499 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5500 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5501 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5502 | NumElements.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5503 | T.getOpenLocation(), |
| 5504 | T.getCloseLocation()), |
| 5505 | attrs, T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5506 | } |
| 5507 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5508 | /// [GNU] typeof-specifier: |
| 5509 | /// typeof ( expressions ) |
| 5510 | /// typeof ( type-name ) |
| 5511 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5512 | /// |
| 5513 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5514 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5515 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5516 | SourceLocation StartLoc = ConsumeToken(); |
| 5517 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5518 | const bool hasParens = Tok.is(tok::l_paren); |
| 5519 | |
Eli Friedman | 80bfa3d | 2012-09-26 04:34:21 +0000 | [diff] [blame] | 5520 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated, |
| 5521 | Sema::ReuseLambdaContextDecl); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5522 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5523 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5524 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5525 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5526 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 5527 | CastTy, CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5528 | if (hasParens) |
| 5529 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5530 | |
| 5531 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5532 | // FIXME: Not accurate, the range gets one token more than it should. |
| 5533 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5534 | else |
| 5535 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5536 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5537 | if (isCastExpr) { |
| 5538 | if (!CastTy) { |
| 5539 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5540 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 5541 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5542 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5543 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5544 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5545 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5546 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5547 | DiagID, CastTy)) |
| 5548 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5549 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5550 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5551 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5552 | // If we get here, the operand to the typeof was an expresion. |
| 5553 | if (Operand.isInvalid()) { |
| 5554 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 5555 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5556 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5557 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5558 | // We might need to transform the operand if it is potentially evaluated. |
| 5559 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 5560 | if (Operand.isInvalid()) { |
| 5561 | DS.SetTypeSpecError(); |
| 5562 | return; |
| 5563 | } |
| 5564 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5565 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5566 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5567 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5568 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5569 | DiagID, Operand.get())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5570 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5571 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5572 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 5573 | /// [C11] atomic-specifier: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5574 | /// _Atomic ( type-name ) |
| 5575 | /// |
| 5576 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5577 | assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) && |
| 5578 | "Not an atomic specifier"); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5579 | |
| 5580 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5581 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5582 | if (T.consumeOpen()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5583 | return; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5584 | |
| 5585 | TypeResult Result = ParseTypeName(); |
| 5586 | if (Result.isInvalid()) { |
| 5587 | SkipUntil(tok::r_paren); |
| 5588 | return; |
| 5589 | } |
| 5590 | |
| 5591 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5592 | T.consumeClose(); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5593 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5594 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5595 | return; |
| 5596 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5597 | DS.setTypeofParensRange(T.getRange()); |
| 5598 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5599 | |
| 5600 | const char *PrevSpec = 0; |
| 5601 | unsigned DiagID; |
| 5602 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
| 5603 | DiagID, Result.release())) |
| 5604 | Diag(StartLoc, DiagID) << PrevSpec; |
| 5605 | } |
| 5606 | |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5607 | |
| 5608 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 5609 | /// from TryAltiVecVectorToken. |
| 5610 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 5611 | Token Next = NextToken(); |
| 5612 | switch (Next.getKind()) { |
| 5613 | default: return false; |
| 5614 | case tok::kw_short: |
| 5615 | case tok::kw_long: |
| 5616 | case tok::kw_signed: |
| 5617 | case tok::kw_unsigned: |
| 5618 | case tok::kw_void: |
| 5619 | case tok::kw_char: |
| 5620 | case tok::kw_int: |
| 5621 | case tok::kw_float: |
| 5622 | case tok::kw_double: |
| 5623 | case tok::kw_bool: |
| 5624 | case tok::kw___pixel: |
| 5625 | Tok.setKind(tok::kw___vector); |
| 5626 | return true; |
| 5627 | case tok::identifier: |
| 5628 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5629 | Tok.setKind(tok::kw___vector); |
| 5630 | return true; |
| 5631 | } |
Bill Schmidt | 3e3d20b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5632 | if (Next.getIdentifierInfo() == Ident_bool) { |
| 5633 | Tok.setKind(tok::kw___vector); |
| 5634 | return true; |
| 5635 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5636 | return false; |
| 5637 | } |
| 5638 | } |
| 5639 | |
| 5640 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 5641 | const char *&PrevSpec, unsigned &DiagID, |
| 5642 | bool &isInvalid) { |
| 5643 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 5644 | Token Next = NextToken(); |
| 5645 | switch (Next.getKind()) { |
| 5646 | case tok::kw_short: |
| 5647 | case tok::kw_long: |
| 5648 | case tok::kw_signed: |
| 5649 | case tok::kw_unsigned: |
| 5650 | case tok::kw_void: |
| 5651 | case tok::kw_char: |
| 5652 | case tok::kw_int: |
| 5653 | case tok::kw_float: |
| 5654 | case tok::kw_double: |
| 5655 | case tok::kw_bool: |
| 5656 | case tok::kw___pixel: |
| 5657 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5658 | return true; |
| 5659 | case tok::identifier: |
| 5660 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5661 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5662 | return true; |
| 5663 | } |
Bill Schmidt | 3e3d20b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5664 | if (Next.getIdentifierInfo() == Ident_bool) { |
| 5665 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5666 | return true; |
| 5667 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5668 | break; |
| 5669 | default: |
| 5670 | break; |
| 5671 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 5672 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5673 | DS.isTypeAltiVecVector()) { |
| 5674 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 5675 | return true; |
Bill Schmidt | 3e3d20b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5676 | } else if ((Tok.getIdentifierInfo() == Ident_bool) && |
| 5677 | DS.isTypeAltiVecVector()) { |
| 5678 | isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID); |
| 5679 | return true; |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5680 | } |
| 5681 | return false; |
| 5682 | } |