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 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 103 | /// FIXME: The GCC grammar/code for this construct implies we need two |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 105 | /// which is followed by a comma or close parenthesis, then the arguments |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | /// start with that identifier; otherwise they are an expression list." |
| 107 | /// |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 108 | /// GCC does not require the ',' between attribs in an attribute-list. |
| 109 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 111 | /// any attributes that don't work (based on my limited testing). Most |
| 112 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 113 | /// a pressing need to implement the 2 token lookahead. |
| 114 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 115 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 116 | SourceLocation *endLoc, |
| 117 | LateParsedAttrList *LateAttrs) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 118 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 120 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 121 | ConsumeToken(); |
| 122 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 123 | "attribute")) { |
| 124 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 125 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 126 | } |
| 127 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 128 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 129 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 130 | } |
| 131 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 132 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 133 | Tok.is(tok::comma)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 135 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 136 | ConsumeToken(); |
| 137 | continue; |
| 138 | } |
| 139 | // we have an identifier or declaration specifier (const, int, etc.) |
| 140 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 141 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 142 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 143 | if (Tok.is(tok::l_paren)) { |
| 144 | // handle "parameterized" attributes |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 145 | if (LateAttrs && isAttributeLateParsed(*AttrName)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 146 | LateParsedAttribute *LA = |
| 147 | new LateParsedAttribute(this, *AttrName, AttrNameLoc); |
| 148 | LateAttrs->push_back(LA); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 149 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 150 | // 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] | 151 | // with other late-parsed declarations. |
DeLesley Hutchins | 161db02 | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 152 | if (!ClassStack.empty() && !LateAttrs->parseSoon()) |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 153 | getCurrentClass().LateParsedDeclarations.push_back(LA); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 155 | // consume everything up to and including the matching right parens |
| 156 | ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false); |
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 | Token Eof; |
| 159 | Eof.startToken(); |
| 160 | Eof.setLocation(Tok.getLocation()); |
| 161 | LA->Toks.push_back(Eof); |
| 162 | } else { |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 163 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 164 | 0, SourceLocation(), AttributeList::AS_GNU); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 165 | } |
| 166 | } else { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 167 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 168 | AttributeList::AS_GNU); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 172 | SkipUntil(tok::r_paren, false); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 173 | SourceLocation Loc = Tok.getLocation(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 174 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 175 | SkipUntil(tok::r_paren, false); |
| 176 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 177 | if (endLoc) |
| 178 | *endLoc = Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 179 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 180 | } |
| 181 | |
Douglas Gregor | 92eb7d8 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 182 | /// \brief Determine whether the given attribute has all expression arguments. |
| 183 | static bool attributeHasExprArgs(const IdentifierInfo &II) { |
| 184 | return llvm::StringSwitch<bool>(II.getName()) |
| 185 | #include "clang/Parse/AttrExprArgs.inc" |
| 186 | .Default(false); |
| 187 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 188 | |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 189 | /// Parse the arguments to a parameterized GNU attribute or |
| 190 | /// a C++11 attribute in "gnu" namespace. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 191 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 192 | SourceLocation AttrNameLoc, |
| 193 | ParsedAttributes &Attrs, |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 194 | SourceLocation *EndLoc, |
| 195 | IdentifierInfo *ScopeName, |
| 196 | SourceLocation ScopeLoc, |
| 197 | AttributeList::Syntax Syntax) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 198 | |
| 199 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 200 | |
| 201 | // Availability attributes have their own grammar. |
| 202 | if (AttrName->isStr("availability")) { |
| 203 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 204 | return; |
| 205 | } |
| 206 | // Thread safety attributes fit into the FIXME case above, so we |
| 207 | // just parse the arguments as a list of expressions |
| 208 | if (IsThreadSafetyAttribute(AttrName->getName())) { |
| 209 | ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 210 | return; |
| 211 | } |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 212 | // Type safety attributes have their own grammar. |
| 213 | if (AttrName->isStr("type_tag_for_datatype")) { |
| 214 | ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 215 | return; |
| 216 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 217 | |
| 218 | ConsumeParen(); // ignore the left paren loc for now |
| 219 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 220 | bool BuiltinType = false; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 221 | ArgsVector ArgExprs; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 222 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 223 | TypeResult T; |
| 224 | SourceRange TypeRange; |
| 225 | bool TypeParsed = false; |
| 226 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 227 | switch (Tok.getKind()) { |
| 228 | case tok::kw_char: |
| 229 | case tok::kw_wchar_t: |
| 230 | case tok::kw_char16_t: |
| 231 | case tok::kw_char32_t: |
| 232 | case tok::kw_bool: |
| 233 | case tok::kw_short: |
| 234 | case tok::kw_int: |
| 235 | case tok::kw_long: |
| 236 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 237 | case tok::kw___int128: |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 238 | case tok::kw_signed: |
| 239 | case tok::kw_unsigned: |
| 240 | case tok::kw_float: |
| 241 | case tok::kw_double: |
| 242 | case tok::kw_void: |
| 243 | case tok::kw_typeof: |
| 244 | // __attribute__(( vec_type_hint(char) )) |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 245 | BuiltinType = true; |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 246 | T = ParseTypeName(&TypeRange); |
| 247 | TypeParsed = true; |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 248 | break; |
| 249 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 250 | case tok::identifier: { |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 251 | if (AttrName->isStr("vec_type_hint")) { |
| 252 | T = ParseTypeName(&TypeRange); |
| 253 | TypeParsed = true; |
| 254 | break; |
| 255 | } |
Douglas Gregor | 92eb7d8 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 256 | // If the attribute has all expression arguments, and not a "parameter", |
| 257 | // break out to handle it below. |
| 258 | if (attributeHasExprArgs(*AttrName)) |
| 259 | break; |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 260 | |
| 261 | IdentifierLoc *Param = ::new (Actions.Context) IdentifierLoc; |
| 262 | Param->Ident = Tok.getIdentifierInfo(); |
| 263 | Param->Loc = ConsumeToken(); |
| 264 | ArgExprs.push_back(Param); |
| 265 | } break; |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 266 | |
| 267 | default: |
| 268 | break; |
| 269 | } |
| 270 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 271 | bool isInvalid = false; |
| 272 | bool isParmType = false; |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 273 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 274 | if (!BuiltinType && !AttrName->isStr("vec_type_hint") && |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 275 | (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) { |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 276 | // Eat the comma. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 277 | if (!ArgExprs.empty()) |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 278 | ConsumeToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 279 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 280 | // Parse the non-empty comma-separated list of expressions. |
| 281 | while (1) { |
| 282 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 283 | if (ArgExpr.isInvalid()) { |
| 284 | SkipUntil(tok::r_paren); |
| 285 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 286 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 287 | ArgExprs.push_back(ArgExpr.release()); |
| 288 | if (Tok.isNot(tok::comma)) |
| 289 | break; |
| 290 | ConsumeToken(); // Eat the comma, move to the next argument |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 291 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 292 | } |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 293 | else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) { |
| 294 | if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<", |
| 295 | tok::greater)) { |
Fariborz Jahanian | b224343 | 2011-10-18 23:13:50 +0000 | [diff] [blame] | 296 | while (Tok.is(tok::identifier)) { |
| 297 | ConsumeToken(); |
| 298 | if (Tok.is(tok::greater)) |
| 299 | break; |
| 300 | if (Tok.is(tok::comma)) { |
| 301 | ConsumeToken(); |
| 302 | continue; |
| 303 | } |
| 304 | } |
| 305 | if (Tok.isNot(tok::greater)) |
| 306 | Diag(Tok, diag::err_iboutletcollection_with_protocol); |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 307 | SkipUntil(tok::r_paren, false, true); // skip until ')' |
| 308 | } |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 309 | } else if (AttrName->isStr("vec_type_hint")) { |
| 310 | if (T.get() && !T.isInvalid()) |
| 311 | isParmType = true; |
| 312 | else { |
| 313 | if (Tok.is(tok::identifier)) |
| 314 | ConsumeToken(); |
| 315 | if (TypeParsed) |
| 316 | isInvalid = true; |
| 317 | } |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 318 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 319 | |
| 320 | SourceLocation RParen = Tok.getLocation(); |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 321 | if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen) && |
| 322 | !isInvalid) { |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 323 | SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc; |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 324 | if (isParmType) { |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 325 | Attrs.addNewTypeAttr(AttrName, SourceRange(AttrLoc, RParen), ScopeName, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 326 | ScopeLoc, T.get(), Syntax); |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 327 | } else { |
| 328 | AttributeList *attr = Attrs.addNew( |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 329 | AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc, |
| 330 | ArgExprs.data(), ArgExprs.size(), Syntax); |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 331 | if (BuiltinType && |
| 332 | attr->getKind() == AttributeList::AT_IBOutletCollection) |
| 333 | Diag(Tok, diag::err_iboutletcollection_builtintype); |
| 334 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 338 | /// \brief Parses a single argument for a declspec, including the |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 339 | /// surrounding parens. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 340 | void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 341 | SourceLocation AttrNameLoc, |
| 342 | ParsedAttributes &Attrs) |
| 343 | { |
| 344 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 345 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 346 | AttrName->getNameStart(), tok::r_paren)) |
| 347 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 348 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 349 | ExprResult ArgExpr(ParseConstantExpression()); |
| 350 | if (ArgExpr.isInvalid()) { |
| 351 | T.skipToEnd(); |
| 352 | return; |
| 353 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 354 | ArgsUnion ExprList = ArgExpr.take(); |
| 355 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, &ExprList, 1, |
| 356 | AttributeList::AS_Declspec); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 357 | |
| 358 | T.consumeClose(); |
| 359 | } |
| 360 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 361 | /// \brief Determines whether a declspec is a "simple" one requiring no |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 362 | /// arguments. |
| 363 | bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) { |
| 364 | return llvm::StringSwitch<bool>(Ident->getName()) |
| 365 | .Case("dllimport", true) |
| 366 | .Case("dllexport", true) |
| 367 | .Case("noreturn", true) |
| 368 | .Case("nothrow", true) |
| 369 | .Case("noinline", true) |
| 370 | .Case("naked", true) |
| 371 | .Case("appdomain", true) |
| 372 | .Case("process", true) |
| 373 | .Case("jitintrinsic", true) |
| 374 | .Case("noalias", true) |
| 375 | .Case("restrict", true) |
| 376 | .Case("novtable", true) |
| 377 | .Case("selectany", true) |
| 378 | .Case("thread", true) |
Aaron Ballman | 3ce0de6 | 2013-05-04 16:58:37 +0000 | [diff] [blame] | 379 | .Case("safebuffers", true ) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 380 | .Default(false); |
| 381 | } |
| 382 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 383 | /// \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] | 384 | /// parameters). Will return false if we properly handled the declspec, or |
| 385 | /// true if it is an unknown declspec. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 386 | void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 387 | SourceLocation Loc, |
| 388 | ParsedAttributes &Attrs) { |
| 389 | // Try to handle the easy case first -- these declspecs all take a single |
| 390 | // parameter as their argument. |
| 391 | if (llvm::StringSwitch<bool>(Ident->getName()) |
| 392 | .Case("uuid", true) |
| 393 | .Case("align", true) |
| 394 | .Case("allocate", true) |
| 395 | .Default(false)) { |
| 396 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 397 | } else if (Ident->getName() == "deprecated") { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 398 | // The deprecated declspec has an optional single argument, so we will |
| 399 | // 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] | 400 | // not. |
| 401 | if (Tok.getKind() == tok::l_paren) |
| 402 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 403 | else |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 404 | Attrs.addNew(Ident, Loc, 0, Loc, 0, 0, AttributeList::AS_Declspec); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 405 | } else if (Ident->getName() == "property") { |
| 406 | // 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] | 407 | // assignment expressions as a parameter, but the lhs of the assignment |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 408 | // must be named get or put. |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 409 | if (Tok.isNot(tok::l_paren)) { |
| 410 | Diag(Tok.getLocation(), diag::err_expected_lparen_after) |
| 411 | << Ident->getNameStart(); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 412 | return; |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 413 | } |
| 414 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 415 | T.expectAndConsume(diag::err_expected_lparen_after, |
| 416 | Ident->getNameStart(), tok::r_paren); |
| 417 | |
| 418 | enum AccessorKind { |
| 419 | AK_Invalid = -1, |
| 420 | AK_Put = 0, AK_Get = 1 // indices into AccessorNames |
| 421 | }; |
| 422 | IdentifierInfo *AccessorNames[] = { 0, 0 }; |
| 423 | bool HasInvalidAccessor = false; |
| 424 | |
| 425 | // Parse the accessor specifications. |
| 426 | while (true) { |
| 427 | // Stop if this doesn't look like an accessor spec. |
| 428 | if (!Tok.is(tok::identifier)) { |
| 429 | // If the user wrote a completely empty list, use a special diagnostic. |
| 430 | if (Tok.is(tok::r_paren) && !HasInvalidAccessor && |
| 431 | AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) { |
| 432 | Diag(Loc, diag::err_ms_property_no_getter_or_putter); |
| 433 | break; |
| 434 | } |
| 435 | |
| 436 | Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor); |
| 437 | break; |
| 438 | } |
| 439 | |
| 440 | AccessorKind Kind; |
| 441 | SourceLocation KindLoc = Tok.getLocation(); |
| 442 | StringRef KindStr = Tok.getIdentifierInfo()->getName(); |
| 443 | if (KindStr == "get") { |
| 444 | Kind = AK_Get; |
| 445 | } else if (KindStr == "put") { |
| 446 | Kind = AK_Put; |
| 447 | |
| 448 | // Recover from the common mistake of using 'set' instead of 'put'. |
| 449 | } else if (KindStr == "set") { |
| 450 | Diag(KindLoc, diag::err_ms_property_has_set_accessor) |
| 451 | << FixItHint::CreateReplacement(KindLoc, "put"); |
| 452 | Kind = AK_Put; |
| 453 | |
| 454 | // Handle the mistake of forgetting the accessor kind by skipping |
| 455 | // this accessor. |
| 456 | } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) { |
| 457 | Diag(KindLoc, diag::err_ms_property_missing_accessor_kind); |
| 458 | ConsumeToken(); |
| 459 | HasInvalidAccessor = true; |
| 460 | goto next_property_accessor; |
| 461 | |
| 462 | // Otherwise, complain about the unknown accessor kind. |
| 463 | } else { |
| 464 | Diag(KindLoc, diag::err_ms_property_unknown_accessor); |
| 465 | HasInvalidAccessor = true; |
| 466 | Kind = AK_Invalid; |
| 467 | |
| 468 | // Try to keep parsing unless it doesn't look like an accessor spec. |
| 469 | if (!NextToken().is(tok::equal)) break; |
| 470 | } |
| 471 | |
| 472 | // Consume the identifier. |
| 473 | ConsumeToken(); |
| 474 | |
| 475 | // Consume the '='. |
| 476 | if (Tok.is(tok::equal)) { |
| 477 | ConsumeToken(); |
| 478 | } else { |
| 479 | Diag(Tok.getLocation(), diag::err_ms_property_expected_equal) |
| 480 | << KindStr; |
| 481 | break; |
| 482 | } |
| 483 | |
| 484 | // Expect the method name. |
| 485 | if (!Tok.is(tok::identifier)) { |
| 486 | Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name); |
| 487 | break; |
| 488 | } |
| 489 | |
| 490 | if (Kind == AK_Invalid) { |
| 491 | // Just drop invalid accessors. |
| 492 | } else if (AccessorNames[Kind] != NULL) { |
| 493 | // Complain about the repeated accessor, ignore it, and keep parsing. |
| 494 | Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr; |
| 495 | } else { |
| 496 | AccessorNames[Kind] = Tok.getIdentifierInfo(); |
| 497 | } |
| 498 | ConsumeToken(); |
| 499 | |
| 500 | next_property_accessor: |
| 501 | // Keep processing accessors until we run out. |
| 502 | if (Tok.is(tok::comma)) { |
| 503 | ConsumeAnyToken(); |
| 504 | continue; |
| 505 | |
| 506 | // If we run into the ')', stop without consuming it. |
| 507 | } else if (Tok.is(tok::r_paren)) { |
| 508 | break; |
| 509 | } else { |
| 510 | Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen); |
| 511 | break; |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | // Only add the property attribute if it was well-formed. |
| 516 | if (!HasInvalidAccessor) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 517 | Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(), |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 518 | AccessorNames[AK_Get], AccessorNames[AK_Put], |
| 519 | AttributeList::AS_Declspec); |
| 520 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 521 | T.skipToEnd(); |
| 522 | } else { |
| 523 | // We don't recognize this as a valid declspec, but instead of creating the |
| 524 | // attribute and allowing sema to warn about it, we will warn here instead. |
| 525 | // This is because some attributes have multiple spellings, but we need to |
| 526 | // 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] | 527 | // attribute, we'd have to split the valid declspec spelling logic into |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 528 | // both locations. |
| 529 | Diag(Loc, diag::warn_ms_declspec_unknown) << Ident; |
| 530 | |
| 531 | // If there's an open paren, we should eat the open and close parens under |
| 532 | // the assumption that this unknown declspec has parameters. |
| 533 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 534 | if (!T.consumeOpen()) |
| 535 | T.skipToEnd(); |
| 536 | } |
| 537 | } |
| 538 | |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 539 | /// [MS] decl-specifier: |
| 540 | /// __declspec ( extended-decl-modifier-seq ) |
| 541 | /// |
| 542 | /// [MS] extended-decl-modifier-seq: |
| 543 | /// extended-decl-modifier[opt] |
| 544 | /// extended-decl-modifier extended-decl-modifier-seq |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 545 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) { |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 546 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 547 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 548 | ConsumeToken(); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 549 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 550 | if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec", |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 551 | tok::r_paren)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 552 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 553 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 554 | // An empty declspec is perfectly legal and should not warn. Additionally, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 555 | // you can specify multiple attributes per declspec. |
| 556 | while (Tok.getKind() != tok::r_paren) { |
| 557 | // We expect either a well-known identifier or a generic string. Anything |
| 558 | // else is a malformed declspec. |
| 559 | bool IsString = Tok.getKind() == tok::string_literal ? true : false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 560 | if (!IsString && Tok.getKind() != tok::identifier && |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 561 | Tok.getKind() != tok::kw_restrict) { |
| 562 | Diag(Tok, diag::err_ms_declspec_type); |
| 563 | T.skipToEnd(); |
| 564 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 565 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 566 | |
| 567 | IdentifierInfo *AttrName; |
| 568 | SourceLocation AttrNameLoc; |
| 569 | if (IsString) { |
| 570 | SmallString<8> StrBuffer; |
| 571 | bool Invalid = false; |
| 572 | StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid); |
| 573 | if (Invalid) { |
| 574 | T.skipToEnd(); |
| 575 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 576 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 577 | AttrName = PP.getIdentifierInfo(Str); |
| 578 | AttrNameLoc = ConsumeStringToken(); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 579 | } else { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 580 | AttrName = Tok.getIdentifierInfo(); |
| 581 | AttrNameLoc = ConsumeToken(); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 582 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 583 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 584 | if (IsString || IsSimpleMicrosoftDeclSpec(AttrName)) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 585 | // If we have a generic string, we will allow it because there is no |
| 586 | // documented list of allowable string declspecs, but we know they exist |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 587 | // (for instance, SAL declspecs in older versions of MSVC). |
| 588 | // |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 589 | // Alternatively, if the identifier is a simple one, then it requires no |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 590 | // arguments and can be turned into an attribute directly. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 591 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 592 | AttributeList::AS_Declspec); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 593 | else |
| 594 | ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 595 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 596 | T.consumeClose(); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 597 | } |
| 598 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 599 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 600 | // Treat these like attributes |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 601 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 602 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 603 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 604 | Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned) || |
| 605 | Tok.is(tok::kw___sptr) || Tok.is(tok::kw___uptr)) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 606 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 607 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 608 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 609 | AttributeList::AS_Keyword); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 610 | } |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 611 | } |
| 612 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 613 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 614 | // Treat these like attributes |
| 615 | while (Tok.is(tok::kw___pascal)) { |
| 616 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 617 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 618 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 619 | AttributeList::AS_Keyword); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 620 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 621 | } |
| 622 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 623 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 624 | // Treat these like attributes |
| 625 | while (Tok.is(tok::kw___kernel)) { |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 626 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 627 | SourceLocation AttrNameLoc = ConsumeToken(); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 628 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 629 | AttributeList::AS_Keyword); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 630 | } |
| 631 | } |
| 632 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 633 | void Parser::ParseOpenCLQualifiers(DeclSpec &DS) { |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 634 | // FIXME: The mapping from attribute spelling to semantics should be |
| 635 | // performed in Sema, not here. |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 636 | SourceLocation Loc = Tok.getLocation(); |
| 637 | switch(Tok.getKind()) { |
| 638 | // OpenCL qualifiers: |
| 639 | case tok::kw___private: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 640 | case tok::kw_private: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 641 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 642 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 643 | PP.getIdentifierInfo("address_space"), Loc, 0); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 644 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 645 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 646 | case tok::kw___global: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 647 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 648 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 649 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 650 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 651 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 652 | case tok::kw___local: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 653 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 654 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 655 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 656 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 657 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 658 | case tok::kw___constant: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 659 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 660 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 661 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 662 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 663 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 664 | case tok::kw___read_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 665 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 666 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 667 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 668 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 669 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 670 | case tok::kw___write_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 671 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 672 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 673 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 674 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 675 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 676 | case tok::kw___read_write: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 677 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 678 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 679 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 680 | break; |
| 681 | default: break; |
| 682 | } |
| 683 | } |
| 684 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 685 | /// \brief Parse a version number. |
| 686 | /// |
| 687 | /// version: |
| 688 | /// simple-integer |
| 689 | /// simple-integer ',' simple-integer |
| 690 | /// simple-integer ',' simple-integer ',' simple-integer |
| 691 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 692 | Range = Tok.getLocation(); |
| 693 | |
| 694 | if (!Tok.is(tok::numeric_constant)) { |
| 695 | Diag(Tok, diag::err_expected_version); |
| 696 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 697 | return VersionTuple(); |
| 698 | } |
| 699 | |
| 700 | // Parse the major (and possibly minor and subminor) versions, which |
| 701 | // are stored in the numeric constant. We utilize a quirk of the |
| 702 | // lexer, which is that it handles something like 1.2.3 as a single |
| 703 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 704 | SmallString<512> Buffer; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 705 | Buffer.resize(Tok.getLength()+1); |
| 706 | const char *ThisTokBegin = &Buffer[0]; |
| 707 | |
| 708 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 709 | bool Invalid = false; |
| 710 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 711 | if (Invalid) |
| 712 | return VersionTuple(); |
| 713 | |
| 714 | // Parse the major version. |
| 715 | unsigned AfterMajor = 0; |
| 716 | unsigned Major = 0; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 717 | while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 718 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 719 | ++AfterMajor; |
| 720 | } |
| 721 | |
| 722 | if (AfterMajor == 0) { |
| 723 | Diag(Tok, diag::err_expected_version); |
| 724 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 725 | return VersionTuple(); |
| 726 | } |
| 727 | |
| 728 | if (AfterMajor == ActualLength) { |
| 729 | ConsumeToken(); |
| 730 | |
| 731 | // We only had a single version component. |
| 732 | if (Major == 0) { |
| 733 | Diag(Tok, diag::err_zero_version); |
| 734 | return VersionTuple(); |
| 735 | } |
| 736 | |
| 737 | return VersionTuple(Major); |
| 738 | } |
| 739 | |
| 740 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 741 | Diag(Tok, diag::err_expected_version); |
| 742 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 743 | return VersionTuple(); |
| 744 | } |
| 745 | |
| 746 | // Parse the minor version. |
| 747 | unsigned AfterMinor = AfterMajor + 1; |
| 748 | unsigned Minor = 0; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 749 | while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 750 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 751 | ++AfterMinor; |
| 752 | } |
| 753 | |
| 754 | if (AfterMinor == ActualLength) { |
| 755 | ConsumeToken(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 756 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 757 | // We had major.minor. |
| 758 | if (Major == 0 && Minor == 0) { |
| 759 | Diag(Tok, diag::err_zero_version); |
| 760 | return VersionTuple(); |
| 761 | } |
| 762 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 763 | return VersionTuple(Major, Minor); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 764 | } |
| 765 | |
| 766 | // If what follows is not a '.', we have a problem. |
| 767 | if (ThisTokBegin[AfterMinor] != '.') { |
| 768 | Diag(Tok, diag::err_expected_version); |
| 769 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 770 | return VersionTuple(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | // Parse the subminor version. |
| 774 | unsigned AfterSubminor = AfterMinor + 1; |
| 775 | unsigned Subminor = 0; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 776 | while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 777 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 778 | ++AfterSubminor; |
| 779 | } |
| 780 | |
| 781 | if (AfterSubminor != ActualLength) { |
| 782 | Diag(Tok, diag::err_expected_version); |
| 783 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 784 | return VersionTuple(); |
| 785 | } |
| 786 | ConsumeToken(); |
| 787 | return VersionTuple(Major, Minor, Subminor); |
| 788 | } |
| 789 | |
| 790 | /// \brief Parse the contents of the "availability" attribute. |
| 791 | /// |
| 792 | /// availability-attribute: |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 793 | /// 'availability' '(' platform ',' version-arg-list, opt-message')' |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 794 | /// |
| 795 | /// platform: |
| 796 | /// identifier |
| 797 | /// |
| 798 | /// version-arg-list: |
| 799 | /// version-arg |
| 800 | /// version-arg ',' version-arg-list |
| 801 | /// |
| 802 | /// version-arg: |
| 803 | /// 'introduced' '=' version |
| 804 | /// 'deprecated' '=' version |
Douglas Gregor | 93a7067 | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 805 | /// 'obsoleted' = version |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 806 | /// 'unavailable' |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 807 | /// opt-message: |
| 808 | /// 'message' '=' <string> |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 809 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 810 | SourceLocation AvailabilityLoc, |
| 811 | ParsedAttributes &attrs, |
| 812 | SourceLocation *endLoc) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 813 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 814 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 815 | ExprResult MessageExpr; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 816 | |
| 817 | // Opening '('. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 818 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 819 | if (T.consumeOpen()) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 820 | Diag(Tok, diag::err_expected_lparen); |
| 821 | return; |
| 822 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 823 | |
| 824 | // Parse the platform name, |
| 825 | if (Tok.isNot(tok::identifier)) { |
| 826 | Diag(Tok, diag::err_availability_expected_platform); |
| 827 | SkipUntil(tok::r_paren); |
| 828 | return; |
| 829 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 830 | |
| 831 | IdentifierLoc *Platform = new (Actions.Context) IdentifierLoc; |
| 832 | Platform->Ident = Tok.getIdentifierInfo(); |
| 833 | Platform->Loc = ConsumeToken(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 834 | |
| 835 | // Parse the ',' following the platform name. |
| 836 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren)) |
| 837 | return; |
| 838 | |
| 839 | // If we haven't grabbed the pointers for the identifiers |
| 840 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 841 | if (!Ident_introduced) { |
| 842 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 843 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 844 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 845 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 846 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 850 | SourceLocation UnavailableLoc; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 851 | do { |
| 852 | if (Tok.isNot(tok::identifier)) { |
| 853 | Diag(Tok, diag::err_availability_expected_change); |
| 854 | SkipUntil(tok::r_paren); |
| 855 | return; |
| 856 | } |
| 857 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 858 | SourceLocation KeywordLoc = ConsumeToken(); |
| 859 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 860 | if (Keyword == Ident_unavailable) { |
| 861 | if (UnavailableLoc.isValid()) { |
| 862 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 863 | << Keyword << SourceRange(UnavailableLoc); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 864 | } |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 865 | UnavailableLoc = KeywordLoc; |
| 866 | |
| 867 | if (Tok.isNot(tok::comma)) |
| 868 | break; |
| 869 | |
| 870 | ConsumeToken(); |
| 871 | continue; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 874 | if (Tok.isNot(tok::equal)) { |
| 875 | Diag(Tok, diag::err_expected_equal_after) |
| 876 | << Keyword; |
| 877 | SkipUntil(tok::r_paren); |
| 878 | return; |
| 879 | } |
| 880 | ConsumeToken(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 881 | if (Keyword == Ident_message) { |
| 882 | if (!isTokenStringLiteral()) { |
Andy Gibbs | 97f8461 | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 883 | Diag(Tok, diag::err_expected_string_literal) |
| 884 | << /*Source='availability attribute'*/2; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 885 | SkipUntil(tok::r_paren); |
| 886 | return; |
| 887 | } |
| 888 | MessageExpr = ParseStringLiteralExpression(); |
| 889 | break; |
| 890 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 891 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 892 | SourceRange VersionRange; |
| 893 | VersionTuple Version = ParseVersionTuple(VersionRange); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 894 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 895 | if (Version.empty()) { |
| 896 | SkipUntil(tok::r_paren); |
| 897 | return; |
| 898 | } |
| 899 | |
| 900 | unsigned Index; |
| 901 | if (Keyword == Ident_introduced) |
| 902 | Index = Introduced; |
| 903 | else if (Keyword == Ident_deprecated) |
| 904 | Index = Deprecated; |
| 905 | else if (Keyword == Ident_obsoleted) |
| 906 | Index = Obsoleted; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 907 | else |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 908 | Index = Unknown; |
| 909 | |
| 910 | if (Index < Unknown) { |
| 911 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 912 | Diag(KeywordLoc, diag::err_availability_redundant) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 913 | << Keyword |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 914 | << SourceRange(Changes[Index].KeywordLoc, |
| 915 | Changes[Index].VersionRange.getEnd()); |
| 916 | } |
| 917 | |
| 918 | Changes[Index].KeywordLoc = KeywordLoc; |
| 919 | Changes[Index].Version = Version; |
| 920 | Changes[Index].VersionRange = VersionRange; |
| 921 | } else { |
| 922 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 923 | << Keyword << VersionRange; |
| 924 | } |
| 925 | |
| 926 | if (Tok.isNot(tok::comma)) |
| 927 | break; |
| 928 | |
| 929 | ConsumeToken(); |
| 930 | } while (true); |
| 931 | |
| 932 | // Closing ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 933 | if (T.consumeClose()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 934 | return; |
| 935 | |
| 936 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 937 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 938 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 939 | // The 'unavailable' availability cannot be combined with any other |
| 940 | // availability changes. Make sure that hasn't happened. |
| 941 | if (UnavailableLoc.isValid()) { |
| 942 | bool Complained = false; |
| 943 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 944 | if (Changes[Index].KeywordLoc.isValid()) { |
| 945 | if (!Complained) { |
| 946 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 947 | << SourceRange(Changes[Index].KeywordLoc, |
| 948 | Changes[Index].VersionRange.getEnd()); |
| 949 | Complained = true; |
| 950 | } |
| 951 | |
| 952 | // Clear out the availability. |
| 953 | Changes[Index] = AvailabilityChange(); |
| 954 | } |
| 955 | } |
| 956 | } |
| 957 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 958 | // Record this attribute |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 959 | attrs.addNew(&Availability, |
| 960 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Fariborz Jahanian | f96708d | 2012-01-23 23:38:32 +0000 | [diff] [blame] | 961 | 0, AvailabilityLoc, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 962 | Platform, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 963 | Changes[Introduced], |
| 964 | Changes[Deprecated], |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 965 | Changes[Obsoleted], |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 966 | UnavailableLoc, MessageExpr.take(), |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 967 | AttributeList::AS_GNU); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 968 | } |
| 969 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 970 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 971 | // Late Parsed Attributes: |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 972 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 973 | |
| 974 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 975 | |
| 976 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 977 | Self->ParseLexedAttributes(*Class); |
| 978 | } |
| 979 | |
| 980 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 981 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 985 | /// scope appropriately. |
| 986 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 987 | // Deal with templates |
| 988 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 989 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 990 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 991 | HasTemplateScope); |
| 992 | if (HasTemplateScope) |
| 993 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 994 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 995 | // Set or update the scope flags. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 996 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 997 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 998 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 999 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 1000 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1001 | // Enter the scope of nested classes |
| 1002 | if (!AlreadyHasClassScope) |
| 1003 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 1004 | Class.TagOrTemplate); |
Benjamin Kramer | 268efba | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 1005 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1006 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 1007 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 1008 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1009 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1010 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1011 | if (!AlreadyHasClassScope) |
| 1012 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 1013 | Class.TagOrTemplate); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1016 | |
| 1017 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 1018 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 1019 | bool EnterScope, bool OnDefinition) { |
DeLesley Hutchins | 161db02 | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1020 | assert(LAs.parseSoon() && |
| 1021 | "Attribute list should be marked for immediate parsing."); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1022 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 95526a4 | 2012-08-15 22:41:04 +0000 | [diff] [blame] | 1023 | if (D) |
| 1024 | LAs[i]->addDecl(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1025 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | d306cf7 | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 1026 | delete LAs[i]; |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1027 | } |
| 1028 | LAs.clear(); |
| 1029 | } |
| 1030 | |
| 1031 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1032 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 1033 | /// This will be called at the end of parsing a class declaration |
| 1034 | /// for each LateParsedAttribute. We consume the saved tokens and |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1035 | /// create an attribute with the arguments filled in. We add this |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1036 | /// to the Attribute list for the decl. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1037 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 1038 | bool EnterScope, bool OnDefinition) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1039 | // Save the current token position. |
| 1040 | SourceLocation OrigLoc = Tok.getLocation(); |
| 1041 | |
| 1042 | // Append the current token at the end of the new token stream so that it |
| 1043 | // doesn't get lost. |
| 1044 | LA.Toks.push_back(Tok); |
| 1045 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 1046 | // Consume the previously pushed token. |
Argyrios Kyrtzidis | ab2d09b | 2013-03-27 23:58:17 +0000 | [diff] [blame] | 1047 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1048 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1049 | if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) { |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1050 | // FIXME: Do not warn on C++11 attributes, once we start supporting |
| 1051 | // them here. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1052 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 1053 | << LA.AttrName.getName(); |
| 1054 | } |
| 1055 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1056 | ParsedAttributes Attrs(AttrFactory); |
| 1057 | SourceLocation endLoc; |
| 1058 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1059 | if (LA.Decls.size() > 0) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1060 | Decl *D = LA.Decls[0]; |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1061 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1062 | RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1063 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1064 | // Allow 'this' within late-parsed attributes. |
Richard Smith | cafeb94 | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 1065 | Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0, |
| 1066 | ND && ND->isCXXInstanceMember()); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1067 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1068 | if (LA.Decls.size() == 1) { |
| 1069 | // If the Decl is templatized, add template parameters to scope. |
| 1070 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 1071 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 1072 | if (HasTemplateScope) |
| 1073 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1074 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1075 | // If the Decl is on a function, add function parameters to the scope. |
| 1076 | bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
| 1077 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope); |
| 1078 | if (HasFunScope) |
| 1079 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1080 | |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1081 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 1082 | 0, SourceLocation(), AttributeList::AS_GNU); |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1083 | |
| 1084 | if (HasFunScope) { |
| 1085 | Actions.ActOnExitFunctionContext(); |
| 1086 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 1087 | } |
| 1088 | if (HasTemplateScope) { |
| 1089 | TempScope.Exit(); |
| 1090 | } |
| 1091 | } else { |
| 1092 | // If there are multiple decls, then the decl cannot be within the |
| 1093 | // function scope. |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1094 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 1095 | 0, SourceLocation(), AttributeList::AS_GNU); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1096 | } |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 1097 | } else { |
| 1098 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1101 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) { |
| 1102 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
| 1103 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1104 | |
| 1105 | if (Tok.getLocation() != OrigLoc) { |
| 1106 | // Due to a parsing error, we either went over the cached tokens or |
| 1107 | // there are still cached tokens left, so we skip the leftover tokens. |
| 1108 | // Since this is an uncommon situation that should be avoided, use the |
| 1109 | // expensive isBeforeInTranslationUnit call. |
| 1110 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 1111 | OrigLoc)) |
| 1112 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
Douglas Gregor | d78ef5b | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 1113 | ConsumeAnyToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1114 | } |
| 1115 | } |
| 1116 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1117 | /// \brief Wrapper around a case statement checking if AttrName is |
| 1118 | /// one of the thread safety attributes |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1119 | bool Parser::IsThreadSafetyAttribute(StringRef AttrName) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1120 | return llvm::StringSwitch<bool>(AttrName) |
| 1121 | .Case("guarded_by", true) |
| 1122 | .Case("guarded_var", true) |
| 1123 | .Case("pt_guarded_by", true) |
| 1124 | .Case("pt_guarded_var", true) |
| 1125 | .Case("lockable", true) |
| 1126 | .Case("scoped_lockable", true) |
| 1127 | .Case("no_thread_safety_analysis", true) |
| 1128 | .Case("acquired_after", true) |
| 1129 | .Case("acquired_before", true) |
| 1130 | .Case("exclusive_lock_function", true) |
| 1131 | .Case("shared_lock_function", true) |
| 1132 | .Case("exclusive_trylock_function", true) |
| 1133 | .Case("shared_trylock_function", true) |
| 1134 | .Case("unlock_function", true) |
| 1135 | .Case("lock_returned", true) |
| 1136 | .Case("locks_excluded", true) |
| 1137 | .Case("exclusive_locks_required", true) |
| 1138 | .Case("shared_locks_required", true) |
| 1139 | .Default(false); |
| 1140 | } |
| 1141 | |
| 1142 | /// \brief Parse the contents of thread safety attributes. These |
| 1143 | /// should always be parsed as an expression list. |
| 1144 | /// |
| 1145 | /// We need to special case the parsing due to the fact that if the first token |
| 1146 | /// of the first argument is an identifier, the main parse loop will store |
| 1147 | /// that token as a "parameter" and the rest of |
| 1148 | /// the arguments will be added to a list of "arguments". However, |
| 1149 | /// subsequent tokens in the first argument are lost. We instead parse each |
| 1150 | /// argument as an expression and add all arguments to the list of "arguments". |
| 1151 | /// In future, we will take advantage of this special case to also |
| 1152 | /// deal with some argument scoping issues here (for example, referring to a |
| 1153 | /// function parameter in the attribute on that function). |
| 1154 | void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName, |
| 1155 | SourceLocation AttrNameLoc, |
| 1156 | ParsedAttributes &Attrs, |
| 1157 | SourceLocation *EndLoc) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1158 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1159 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1160 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1161 | T.consumeOpen(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1162 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 1163 | ArgsVector ArgExprs; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1164 | bool ArgExprsOk = true; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1165 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1166 | // now parse the list of expressions |
DeLesley Hutchins | 4805f15 | 2011-12-14 19:36:06 +0000 | [diff] [blame] | 1167 | while (Tok.isNot(tok::r_paren)) { |
DeLesley Hutchins | ed4330b | 2013-02-07 19:01:07 +0000 | [diff] [blame] | 1168 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1169 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 1170 | if (ArgExpr.isInvalid()) { |
| 1171 | ArgExprsOk = false; |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1172 | T.consumeClose(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1173 | break; |
| 1174 | } else { |
| 1175 | ArgExprs.push_back(ArgExpr.release()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1176 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1177 | if (Tok.isNot(tok::comma)) |
| 1178 | break; |
| 1179 | ConsumeToken(); // Eat the comma, move to the next argument |
| 1180 | } |
| 1181 | // Match the ')'. |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 1182 | if (ArgExprsOk && !T.consumeClose()) { |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 1183 | Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, ArgExprs.data(), |
| 1184 | ArgExprs.size(), AttributeList::AS_GNU); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1185 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1186 | if (EndLoc) |
| 1187 | *EndLoc = T.getCloseLocation(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1190 | void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName, |
| 1191 | SourceLocation AttrNameLoc, |
| 1192 | ParsedAttributes &Attrs, |
| 1193 | SourceLocation *EndLoc) { |
| 1194 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 1195 | |
| 1196 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1197 | T.consumeOpen(); |
| 1198 | |
| 1199 | if (Tok.isNot(tok::identifier)) { |
| 1200 | Diag(Tok, diag::err_expected_ident); |
| 1201 | T.skipToEnd(); |
| 1202 | return; |
| 1203 | } |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 1204 | IdentifierLoc *ArgumentKind = new (Actions.Context) IdentifierLoc; |
| 1205 | ArgumentKind->Ident = Tok.getIdentifierInfo(); |
| 1206 | ArgumentKind->Loc = ConsumeToken(); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1207 | |
| 1208 | if (Tok.isNot(tok::comma)) { |
| 1209 | Diag(Tok, diag::err_expected_comma); |
| 1210 | T.skipToEnd(); |
| 1211 | return; |
| 1212 | } |
| 1213 | ConsumeToken(); |
| 1214 | |
| 1215 | SourceRange MatchingCTypeRange; |
| 1216 | TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange); |
| 1217 | if (MatchingCType.isInvalid()) { |
| 1218 | T.skipToEnd(); |
| 1219 | return; |
| 1220 | } |
| 1221 | |
| 1222 | bool LayoutCompatible = false; |
| 1223 | bool MustBeNull = false; |
| 1224 | while (Tok.is(tok::comma)) { |
| 1225 | ConsumeToken(); |
| 1226 | if (Tok.isNot(tok::identifier)) { |
| 1227 | Diag(Tok, diag::err_expected_ident); |
| 1228 | T.skipToEnd(); |
| 1229 | return; |
| 1230 | } |
| 1231 | IdentifierInfo *Flag = Tok.getIdentifierInfo(); |
| 1232 | if (Flag->isStr("layout_compatible")) |
| 1233 | LayoutCompatible = true; |
| 1234 | else if (Flag->isStr("must_be_null")) |
| 1235 | MustBeNull = true; |
| 1236 | else { |
| 1237 | Diag(Tok, diag::err_type_safety_unknown_flag) << Flag; |
| 1238 | T.skipToEnd(); |
| 1239 | return; |
| 1240 | } |
| 1241 | ConsumeToken(); // consume flag |
| 1242 | } |
| 1243 | |
| 1244 | if (!T.consumeClose()) { |
| 1245 | Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc, |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 1246 | ArgumentKind, MatchingCType.release(), |
| 1247 | LayoutCompatible, MustBeNull, |
| 1248 | AttributeList::AS_GNU); |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | if (EndLoc) |
| 1252 | *EndLoc = T.getCloseLocation(); |
| 1253 | } |
| 1254 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1255 | /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets |
| 1256 | /// of a C++11 attribute-specifier in a location where an attribute is not |
| 1257 | /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this |
| 1258 | /// situation. |
| 1259 | /// |
| 1260 | /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if |
| 1261 | /// this doesn't appear to actually be an attribute-specifier, and the caller |
| 1262 | /// should try to parse it. |
| 1263 | bool Parser::DiagnoseProhibitedCXX11Attribute() { |
| 1264 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)); |
| 1265 | |
| 1266 | switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) { |
| 1267 | case CAK_NotAttributeSpecifier: |
| 1268 | // No diagnostic: we're in Obj-C++11 and this is not actually an attribute. |
| 1269 | return false; |
| 1270 | |
| 1271 | case CAK_InvalidAttributeSpecifier: |
| 1272 | Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute); |
| 1273 | return false; |
| 1274 | |
| 1275 | case CAK_AttributeSpecifier: |
| 1276 | // Parse and discard the attributes. |
| 1277 | SourceLocation BeginLoc = ConsumeBracket(); |
| 1278 | ConsumeBracket(); |
| 1279 | SkipUntil(tok::r_square, /*StopAtSemi*/ false); |
| 1280 | assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied"); |
| 1281 | SourceLocation EndLoc = ConsumeBracket(); |
| 1282 | Diag(BeginLoc, diag::err_attributes_not_allowed) |
| 1283 | << SourceRange(BeginLoc, EndLoc); |
| 1284 | return true; |
| 1285 | } |
Chandler Carruth | 2c6dbd7 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 1286 | llvm_unreachable("All cases handled above."); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
Richard Smith | 975d52c | 2013-02-20 01:17:14 +0000 | [diff] [blame] | 1289 | /// \brief We have found the opening square brackets of a C++11 |
| 1290 | /// attribute-specifier in a location where an attribute is not permitted, but |
| 1291 | /// we know where the attributes ought to be written. Parse them anyway, and |
| 1292 | /// provide a fixit moving them to the right place. |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1293 | void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs, |
| 1294 | SourceLocation CorrectLocation) { |
| 1295 | assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) || |
| 1296 | Tok.is(tok::kw_alignas)); |
| 1297 | |
| 1298 | // Consume the attributes. |
| 1299 | SourceLocation Loc = Tok.getLocation(); |
| 1300 | ParseCXX11Attributes(Attrs); |
| 1301 | CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true); |
| 1302 | |
| 1303 | Diag(Loc, diag::err_attributes_not_allowed) |
| 1304 | << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange) |
| 1305 | << FixItHint::CreateRemoval(AttrRange); |
| 1306 | } |
| 1307 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1308 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 1309 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 1310 | << attrs.Range; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1313 | void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) { |
| 1314 | AttributeList *AttrList = attrs.getList(); |
| 1315 | while (AttrList) { |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1316 | if (AttrList->isCXX11Attribute()) { |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 1317 | Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr) |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1318 | << AttrList->getName(); |
| 1319 | AttrList->setInvalid(); |
| 1320 | } |
| 1321 | AttrList = AttrList->getNext(); |
| 1322 | } |
| 1323 | } |
| 1324 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1325 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 1326 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1327 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 1328 | /// location of the semicolon in DeclEnd. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1329 | /// |
| 1330 | /// declaration: [C99 6.7] |
| 1331 | /// block-declaration -> |
| 1332 | /// simple-declaration |
| 1333 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1334 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1335 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1336 | /// [C++] using-directive |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1337 | /// [C++] using-declaration |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1338 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1339 | /// others... [FIXME] |
| 1340 | /// |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1341 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 1342 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1343 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1344 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 1345 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | e8cff36 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 1346 | // Must temporarily exit the objective-c container scope for |
| 1347 | // parsing c none objective-c decls. |
| 1348 | ObjCDeclContextSwitch ObjCDC(*this); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1349 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1350 | Decl *SingleDecl = 0; |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1351 | Decl *OwnedType = 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1352 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1353 | case tok::kw_template: |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1354 | case tok::kw_export: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1355 | ProhibitAttributes(attrs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1356 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1357 | break; |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1358 | case tok::kw_inline: |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 1359 | // 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] | 1360 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1361 | ProhibitAttributes(attrs); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1362 | SourceLocation InlineLoc = ConsumeToken(); |
| 1363 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 1364 | break; |
| 1365 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1366 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1367 | true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1368 | case tok::kw_namespace: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1369 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1370 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1371 | break; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1372 | case tok::kw_using: |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1373 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1374 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1375 | break; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1376 | case tok::kw_static_assert: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1377 | case tok::kw__Static_assert: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1378 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1379 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1380 | break; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1381 | default: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1382 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1383 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1384 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1385 | // 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] | 1386 | // single decl, convert it now. Alias declarations can also declare a type; |
| 1387 | // include that too if it is present. |
| 1388 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1392 | /// declaration-specifiers init-declarator-list[opt] ';' |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1393 | /// [C++11] attribute-specifier-seq decl-specifier-seq[opt] |
| 1394 | /// init-declarator-list ';' |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1395 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 1396 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1397 | /// |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1398 | /// for-range-declaration: [C++11 6.5p1: stmt.ranged] |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1399 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1400 | /// |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1401 | /// 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] | 1402 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1403 | /// |
| 1404 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 1405 | /// of a simple-declaration. If we find that we are, we also parse the |
| 1406 | /// for-range-initializer, and place it here. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1407 | Parser::DeclGroupPtrTy |
| 1408 | Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context, |
| 1409 | SourceLocation &DeclEnd, |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1410 | ParsedAttributesWithRange &Attrs, |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1411 | bool RequireSemi, ForRangeInit *FRI) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1412 | // Parse the common declaration-specifiers piece. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1413 | ParsingDeclSpec DS(*this); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1414 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1415 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1416 | getDeclSpecContextFromDeclaratorContext(Context)); |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1417 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1418 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1419 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1420 | if (Tok.is(tok::semi)) { |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1421 | ProhibitAttributes(Attrs); |
Argyrios Kyrtzidis | 5641b0d | 2012-05-16 23:49:15 +0000 | [diff] [blame] | 1422 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1423 | if (RequireSemi) ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1424 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1425 | DS); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1426 | DS.complete(TheDecl); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1427 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1428 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1429 | |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1430 | DS.takeAttributesFrom(Attrs); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1431 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1432 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1433 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1434 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1435 | /// for a declarator. |
| 1436 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1437 | switch (Tok.getKind()) { |
| 1438 | case tok::annot_cxxscope: |
| 1439 | case tok::annot_template_id: |
| 1440 | case tok::caret: |
| 1441 | case tok::code_completion: |
| 1442 | case tok::coloncolon: |
| 1443 | case tok::ellipsis: |
| 1444 | case tok::kw___attribute: |
| 1445 | case tok::kw_operator: |
| 1446 | case tok::l_paren: |
| 1447 | case tok::star: |
| 1448 | return true; |
| 1449 | |
| 1450 | case tok::amp: |
| 1451 | case tok::ampamp: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1452 | return getLangOpts().CPlusPlus; |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1453 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1454 | 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] | 1455 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 && |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1456 | NextToken().is(tok::l_square); |
| 1457 | |
| 1458 | 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] | 1459 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1460 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1461 | case tok::identifier: |
| 1462 | switch (NextToken().getKind()) { |
| 1463 | case tok::code_completion: |
| 1464 | case tok::coloncolon: |
| 1465 | case tok::comma: |
| 1466 | case tok::equal: |
| 1467 | case tok::equalequal: // Might be a typo for '='. |
| 1468 | case tok::kw_alignas: |
| 1469 | case tok::kw_asm: |
| 1470 | case tok::kw___attribute: |
| 1471 | case tok::l_brace: |
| 1472 | case tok::l_paren: |
| 1473 | case tok::l_square: |
| 1474 | case tok::less: |
| 1475 | case tok::r_brace: |
| 1476 | case tok::r_paren: |
| 1477 | case tok::r_square: |
| 1478 | case tok::semi: |
| 1479 | return true; |
| 1480 | |
| 1481 | case tok::colon: |
| 1482 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1483 | // and in block scope it's probably a label. Inside a class definition, |
| 1484 | // this is a bit-field. |
| 1485 | return Context == Declarator::MemberContext || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1486 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1487 | |
| 1488 | case tok::identifier: // Possible virt-specifier. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1489 | return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken()); |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1490 | |
| 1491 | default: |
| 1492 | return false; |
| 1493 | } |
| 1494 | |
| 1495 | default: |
| 1496 | return false; |
| 1497 | } |
| 1498 | } |
| 1499 | |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1500 | /// Skip until we reach something which seems like a sensible place to pick |
| 1501 | /// up parsing after a malformed declaration. This will sometimes stop sooner |
| 1502 | /// than SkipUntil(tok::r_brace) would, but will never stop later. |
| 1503 | void Parser::SkipMalformedDecl() { |
| 1504 | while (true) { |
| 1505 | switch (Tok.getKind()) { |
| 1506 | case tok::l_brace: |
| 1507 | // Skip until matching }, then stop. We've probably skipped over |
| 1508 | // a malformed class or function definition or similar. |
| 1509 | ConsumeBrace(); |
| 1510 | SkipUntil(tok::r_brace, /*StopAtSemi*/false); |
| 1511 | if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) { |
| 1512 | // This declaration isn't over yet. Keep skipping. |
| 1513 | continue; |
| 1514 | } |
| 1515 | if (Tok.is(tok::semi)) |
| 1516 | ConsumeToken(); |
| 1517 | return; |
| 1518 | |
| 1519 | case tok::l_square: |
| 1520 | ConsumeBracket(); |
| 1521 | SkipUntil(tok::r_square, /*StopAtSemi*/false); |
| 1522 | continue; |
| 1523 | |
| 1524 | case tok::l_paren: |
| 1525 | ConsumeParen(); |
| 1526 | SkipUntil(tok::r_paren, /*StopAtSemi*/false); |
| 1527 | continue; |
| 1528 | |
| 1529 | case tok::r_brace: |
| 1530 | return; |
| 1531 | |
| 1532 | case tok::semi: |
| 1533 | ConsumeToken(); |
| 1534 | return; |
| 1535 | |
| 1536 | case tok::kw_inline: |
| 1537 | // 'inline namespace' at the start of a line is almost certainly |
Jordan Rose | 94f29f4 | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1538 | // a good place to pick back up parsing, except in an Objective-C |
| 1539 | // @interface context. |
| 1540 | if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) && |
| 1541 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1542 | return; |
| 1543 | break; |
| 1544 | |
| 1545 | case tok::kw_namespace: |
| 1546 | // 'namespace' at the start of a line is almost certainly a good |
Jordan Rose | 94f29f4 | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1547 | // place to pick back up parsing, except in an Objective-C |
| 1548 | // @interface context. |
| 1549 | if (Tok.isAtStartOfLine() && |
| 1550 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
| 1551 | return; |
| 1552 | break; |
| 1553 | |
| 1554 | case tok::at: |
| 1555 | // @end is very much like } in Objective-C contexts. |
| 1556 | if (NextToken().isObjCAtKeyword(tok::objc_end) && |
| 1557 | ParsingInObjCContainer) |
| 1558 | return; |
| 1559 | break; |
| 1560 | |
| 1561 | case tok::minus: |
| 1562 | case tok::plus: |
| 1563 | // - and + probably start new method declarations in Objective-C contexts. |
| 1564 | if (Tok.isAtStartOfLine() && ParsingInObjCContainer) |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1565 | return; |
| 1566 | break; |
| 1567 | |
| 1568 | case tok::eof: |
| 1569 | return; |
| 1570 | |
| 1571 | default: |
| 1572 | break; |
| 1573 | } |
| 1574 | |
| 1575 | ConsumeAnyToken(); |
| 1576 | } |
| 1577 | } |
| 1578 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1579 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1580 | /// definition or a group of object declarations, actually parse the |
| 1581 | /// result. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1582 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1583 | unsigned Context, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1584 | bool AllowFunctionDefinitions, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1585 | SourceLocation *DeclEnd, |
| 1586 | ForRangeInit *FRI) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1587 | // Parse the first declarator. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1588 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1589 | ParseDeclarator(D); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1590 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1591 | // Bail out if the first declarator didn't seem well-formed. |
| 1592 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1593 | SkipMalformedDecl(); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1594 | return DeclGroupPtrTy(); |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1595 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1596 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1597 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1598 | // appropriate function scope after the function Decl has been constructed. |
DeLesley Hutchins | 161db02 | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1599 | // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList. |
| 1600 | LateParsedAttrList LateParsedAttrs(true); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1601 | if (D.isFunctionDeclarator()) |
| 1602 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1603 | |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1604 | // 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] | 1605 | if (D.isFunctionDeclarator() && |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1606 | // Look at the next token to make sure that this isn't a function |
| 1607 | // declaration. We have to check this because __attribute__ might be the |
| 1608 | // start of a function definition in GCC-extended K&R C. |
Fariborz Jahanian | be1d4ec | 2012-08-10 15:54:40 +0000 | [diff] [blame] | 1609 | !isDeclarationAfterDeclarator()) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1610 | |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1611 | if (AllowFunctionDefinitions) { |
| 1612 | if (isStartOfFunctionDefinition(D)) { |
| 1613 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1614 | Diag(Tok, diag::err_function_declared_typedef); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1615 | |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1616 | // Recover by treating the 'typedef' as spurious. |
| 1617 | DS.ClearStorageClassSpecs(); |
| 1618 | } |
| 1619 | |
| 1620 | Decl *TheDecl = |
| 1621 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
| 1622 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1625 | if (isDeclarationSpecifier()) { |
| 1626 | // If there is an invalid declaration specifier right after the function |
| 1627 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1628 | // actually a body. Just fall through into the code that handles it as a |
| 1629 | // prototype, and let the top-level code handle the erroneous declspec |
| 1630 | // where it would otherwise expect a comma or semicolon. |
| 1631 | } else { |
| 1632 | Diag(Tok, diag::err_expected_fn_body); |
| 1633 | SkipUntil(tok::semi); |
| 1634 | return DeclGroupPtrTy(); |
| 1635 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1636 | } else { |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame] | 1637 | if (Tok.is(tok::l_brace)) { |
| 1638 | Diag(Tok, diag::err_function_definition_not_allowed); |
| 1639 | SkipUntil(tok::r_brace, true, true); |
| 1640 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1641 | } |
| 1642 | } |
| 1643 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1644 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1645 | return DeclGroupPtrTy(); |
| 1646 | |
| 1647 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1648 | // must parse and analyze the for-range-initializer before the declaration is |
| 1649 | // analyzed. |
Douglas Gregor | 12849d0 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1650 | // |
| 1651 | // Handle the Objective-C for-in loop variable similarly, although we |
| 1652 | // don't need to parse the container in advance. |
| 1653 | if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) { |
| 1654 | bool IsForRangeLoop = false; |
| 1655 | if (Tok.is(tok::colon)) { |
| 1656 | IsForRangeLoop = true; |
| 1657 | FRI->ColonLoc = ConsumeToken(); |
| 1658 | if (Tok.is(tok::l_brace)) |
| 1659 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1660 | else |
| 1661 | FRI->RangeExpr = ParseExpression(); |
| 1662 | } |
| 1663 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1664 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | 12849d0 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1665 | if (IsForRangeLoop) |
| 1666 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1667 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | 6895a64 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 1668 | D.complete(ThisDecl); |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1669 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1670 | } |
| 1671 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1672 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1673 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1674 | if (LateParsedAttrs.size() > 0) |
| 1675 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1676 | D.complete(FirstDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1677 | if (FirstDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1678 | DeclsInGroup.push_back(FirstDecl); |
| 1679 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1680 | bool ExpectSemi = Context != Declarator::ForContext; |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 1681 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1682 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1683 | // error, bail out. |
| 1684 | while (Tok.is(tok::comma)) { |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1685 | SourceLocation CommaLoc = ConsumeToken(); |
| 1686 | |
| 1687 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1688 | // This comma was followed by a line-break and something which can't be |
| 1689 | // the start of a declarator. The comma was probably a typo for a |
| 1690 | // semicolon. |
| 1691 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1692 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1693 | ExpectSemi = false; |
| 1694 | break; |
| 1695 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1696 | |
| 1697 | // Parse the next declarator. |
| 1698 | D.clear(); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1699 | D.setCommaLoc(CommaLoc); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1700 | |
| 1701 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1702 | // declaration, these would be part of the declspec. In subsequent |
| 1703 | // declarators, they become part of the declarator itself, so that they |
| 1704 | // don't apply to declarators after *this* one. Examples: |
| 1705 | // short __attribute__((common)) var; -> declspec |
| 1706 | // short var __attribute__((common)); -> declarator |
| 1707 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1708 | MaybeParseGNUAttributes(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1709 | |
| 1710 | ParseDeclarator(D); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1711 | if (!D.isInvalidType()) { |
| 1712 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1713 | D.complete(ThisDecl); |
| 1714 | if (ThisDecl) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1715 | DeclsInGroup.push_back(ThisDecl); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1716 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1717 | } |
| 1718 | |
| 1719 | if (DeclEnd) |
| 1720 | *DeclEnd = Tok.getLocation(); |
| 1721 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1722 | if (ExpectSemi && |
Chris Lattner | 8bb21d3 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 1723 | ExpectAndConsumeSemi(Context == Declarator::FileContext |
| 1724 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1725 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1726 | // Okay, there was no semicolon and one was expected. If we see a |
| 1727 | // declaration specifier, just assume it was missing and continue parsing. |
| 1728 | // Otherwise things are very confused and we skip to recover. |
| 1729 | if (!isDeclarationSpecifier()) { |
| 1730 | SkipUntil(tok::r_brace, true, true); |
| 1731 | if (Tok.is(tok::semi)) |
| 1732 | ConsumeToken(); |
| 1733 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1734 | } |
| 1735 | |
Rafael Espindola | 4549d7f | 2013-07-09 12:05:01 +0000 | [diff] [blame] | 1736 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1737 | } |
| 1738 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1739 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1740 | /// declarator. Returns true on an error. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1741 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1742 | // If a simple-asm-expr is present, parse it. |
| 1743 | if (Tok.is(tok::kw_asm)) { |
| 1744 | SourceLocation Loc; |
| 1745 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1746 | if (AsmLabel.isInvalid()) { |
| 1747 | SkipUntil(tok::semi, true, true); |
| 1748 | return true; |
| 1749 | } |
| 1750 | |
| 1751 | D.setAsmLabel(AsmLabel.release()); |
| 1752 | D.SetRangeEnd(Loc); |
| 1753 | } |
| 1754 | |
| 1755 | MaybeParseGNUAttributes(D); |
| 1756 | return false; |
| 1757 | } |
| 1758 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1759 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1760 | /// declarator'. This method parses the remainder of the declaration |
| 1761 | /// (including any attributes or initializer, among other things) and |
| 1762 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1763 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1764 | /// init-declarator: [C99 6.7] |
| 1765 | /// declarator |
| 1766 | /// declarator '=' initializer |
| 1767 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1768 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1769 | /// [C++] declarator initializer[opt] |
| 1770 | /// |
| 1771 | /// [C++] initializer: |
| 1772 | /// [C++] '=' initializer-clause |
| 1773 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1774 | /// [C++0x] '=' 'default' [TODO] |
| 1775 | /// [C++0x] '=' 'delete' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1776 | /// [C++0x] braced-init-list |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1777 | /// |
| 1778 | /// According to the standard grammar, =default and =delete are function |
| 1779 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1780 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1781 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1782 | const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1783 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1784 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1785 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1786 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1787 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1788 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1789 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1790 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1791 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1792 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1793 | switch (TemplateInfo.Kind) { |
| 1794 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1795 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1796 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1797 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1798 | case ParsedTemplateInfo::Template: |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1799 | case ParsedTemplateInfo::ExplicitSpecialization: { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1800 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1801 | *TemplateInfo.TemplateParams, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1802 | D); |
Larisse Voufo | 06935f3 | 2013-08-06 03:43:07 +0000 | [diff] [blame] | 1803 | if (Tok.is(tok::semi) && |
| 1804 | Actions.HandleVariableRedeclaration(ThisDecl, D.getCXXScopeSpec())) { |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1805 | SkipUntil(tok::semi, true, true); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1806 | return 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1807 | } |
Larisse Voufo | 2521813 | 2013-08-06 07:33:00 +0000 | [diff] [blame] | 1808 | if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl)) |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1809 | // Re-direct this decl to refer to the templated decl so that we can |
| 1810 | // initialize it. |
| 1811 | ThisDecl = VT->getTemplatedDecl(); |
| 1812 | break; |
| 1813 | } |
| 1814 | case ParsedTemplateInfo::ExplicitInstantiation: { |
| 1815 | if (Tok.is(tok::semi)) { |
| 1816 | DeclResult ThisRes = Actions.ActOnExplicitInstantiation( |
| 1817 | getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D); |
| 1818 | if (ThisRes.isInvalid()) { |
| 1819 | SkipUntil(tok::semi, true, true); |
| 1820 | return 0; |
| 1821 | } |
| 1822 | ThisDecl = ThisRes.get(); |
| 1823 | } else { |
| 1824 | // FIXME: This check should be for a variable template instantiation only. |
| 1825 | |
| 1826 | // Check that this is a valid instantiation |
| 1827 | if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) { |
| 1828 | // If the declarator-id is not a template-id, issue a diagnostic and |
| 1829 | // recover by ignoring the 'template' keyword. |
| 1830 | Diag(Tok, diag::err_template_defn_explicit_instantiation) |
| 1831 | << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc); |
| 1832 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1833 | } else { |
| 1834 | SourceLocation LAngleLoc = |
| 1835 | PP.getLocForEndOfToken(TemplateInfo.TemplateLoc); |
| 1836 | Diag(D.getIdentifierLoc(), |
| 1837 | diag::err_explicit_instantiation_with_definition) |
| 1838 | << SourceRange(TemplateInfo.TemplateLoc) |
| 1839 | << FixItHint::CreateInsertion(LAngleLoc, "<>"); |
| 1840 | |
| 1841 | // Recover as if it were an explicit specialization. |
| 1842 | TemplateParameterLists FakedParamLists; |
| 1843 | FakedParamLists.push_back(Actions.ActOnTemplateParameterList( |
| 1844 | 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, 0, 0, |
| 1845 | LAngleLoc)); |
| 1846 | |
| 1847 | ThisDecl = |
| 1848 | Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D); |
| 1849 | } |
| 1850 | } |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1851 | break; |
| 1852 | } |
| 1853 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1854 | |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1855 | bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType(); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1856 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1857 | // Parse declarator '=' initializer. |
Richard Trieu | d6c7c67 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1858 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | fcaf27e | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1859 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1860 | ConsumeToken(); |
Larisse Voufo | ef4579c | 2013-08-06 01:03:05 +0000 | [diff] [blame] | 1861 | |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1862 | if (Tok.is(tok::kw_delete)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1863 | if (D.isFunctionDeclarator()) |
| 1864 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1865 | << 1 /* delete */; |
| 1866 | else |
| 1867 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Sean Hunt | fe2695e | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1868 | } else if (Tok.is(tok::kw_default)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1869 | if (D.isFunctionDeclarator()) |
Sebastian Redl | ecfcd56 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1870 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1871 | << 0 /* default */; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1872 | else |
| 1873 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1874 | } else { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1875 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1876 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1877 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1878 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1879 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1880 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1881 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Peter Collingbourne | ec98f2f | 2012-07-27 12:56:09 +0000 | [diff] [blame] | 1882 | Actions.FinalizeDeclaration(ThisDecl); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1883 | cutOffParsing(); |
| 1884 | return 0; |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1885 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1886 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1887 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1888 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1889 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1890 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1891 | ExitScope(); |
| 1892 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1893 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1894 | if (Init.isInvalid()) { |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1895 | SkipUntil(tok::comma, true, true); |
| 1896 | Actions.ActOnInitializerError(ThisDecl); |
| 1897 | } else |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1898 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1899 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1900 | } |
| 1901 | } else if (Tok.is(tok::l_paren)) { |
| 1902 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1903 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1904 | T.consumeOpen(); |
| 1905 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1906 | ExprVector Exprs; |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1907 | CommaLocsTy CommaLocs; |
| 1908 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1909 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1910 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1911 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1912 | } |
| 1913 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1914 | if (ParseExpressionList(Exprs, CommaLocs)) { |
David Blaikie | 3ea19c8 | 2012-10-10 23:15:05 +0000 | [diff] [blame] | 1915 | Actions.ActOnInitializerError(ThisDecl); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1916 | SkipUntil(tok::r_paren); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1917 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1918 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1919 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1920 | ExitScope(); |
| 1921 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1922 | } else { |
| 1923 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1924 | T.consumeClose(); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1925 | |
| 1926 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1927 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1928 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1929 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1930 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1931 | ExitScope(); |
| 1932 | } |
| 1933 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1934 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 1935 | T.getCloseLocation(), |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1936 | Exprs); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1937 | Actions.AddInitializerToDecl(ThisDecl, Initializer.take(), |
| 1938 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1939 | } |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1940 | } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) && |
Fariborz Jahanian | b0ed95c | 2012-07-03 23:22:13 +0000 | [diff] [blame] | 1941 | (!CurParsedObjCImpl || !D.isFunctionDeclarator())) { |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1942 | // Parse C++0x braced-init-list. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1943 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1944 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1945 | if (D.getCXXScopeSpec().isSet()) { |
| 1946 | EnterScope(0); |
| 1947 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1948 | } |
| 1949 | |
| 1950 | ExprResult Init(ParseBraceInitializer()); |
| 1951 | |
| 1952 | if (D.getCXXScopeSpec().isSet()) { |
| 1953 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1954 | ExitScope(); |
| 1955 | } |
| 1956 | |
| 1957 | if (Init.isInvalid()) { |
| 1958 | Actions.ActOnInitializerError(ThisDecl); |
| 1959 | } else |
| 1960 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1961 | /*DirectInit=*/true, TypeContainsAuto); |
| 1962 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1963 | } else { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1964 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1967 | Actions.FinalizeDeclaration(ThisDecl); |
| 1968 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1969 | return ThisDecl; |
| 1970 | } |
| 1971 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1972 | /// ParseSpecifierQualifierList |
| 1973 | /// specifier-qualifier-list: |
| 1974 | /// type-specifier specifier-qualifier-list[opt] |
| 1975 | /// type-qualifier specifier-qualifier-list[opt] |
| 1976 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 1977 | /// |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1978 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 1979 | DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1980 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1981 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1982 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1983 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1984 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1985 | // Validate declspec for type-name. |
| 1986 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 1987 | if ((DSC == DSC_type_specifier || DSC == DSC_trailing) && |
| 1988 | !DS.hasTypeSpecifier()) { |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1989 | Diag(Tok, diag::err_expected_type); |
| 1990 | DS.SetTypeSpecError(); |
| 1991 | } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 1992 | !DS.hasAttributes()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1993 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1994 | if (!DS.hasTypeSpecifier()) |
| 1995 | DS.SetTypeSpecError(); |
| 1996 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1997 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1998 | // Issue diagnostic and remove storage class if present. |
| 1999 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 2000 | if (DS.getStorageClassSpecLoc().isValid()) |
| 2001 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 2002 | else |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2003 | Diag(DS.getThreadStorageClassSpecLoc(), |
| 2004 | diag::err_typename_invalid_storageclass); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2005 | DS.ClearStorageClassSpecs(); |
| 2006 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2007 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2008 | // Issue diagnostic and remove function specfier if present. |
| 2009 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2010 | if (DS.isInlineSpecified()) |
| 2011 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 2012 | if (DS.isVirtualSpecified()) |
| 2013 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 2014 | if (DS.isExplicitSpecified()) |
| 2015 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2016 | DS.ClearFunctionSpecs(); |
| 2017 | } |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2018 | |
| 2019 | // Issue diagnostic and remove constexpr specfier if present. |
| 2020 | if (DS.isConstexprSpecified()) { |
| 2021 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 2022 | DS.ClearConstexprSpec(); |
| 2023 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2024 | } |
| 2025 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2026 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 2027 | /// specified token is valid after the identifier in a declarator which |
| 2028 | /// immediately follows the declspec. For example, these things are valid: |
| 2029 | /// |
| 2030 | /// int x [ 4]; // direct-declarator |
| 2031 | /// int x ( int y); // direct-declarator |
| 2032 | /// int(int x ) // direct-declarator |
| 2033 | /// int x ; // simple-declaration |
| 2034 | /// int x = 17; // init-declarator-list |
| 2035 | /// int x , y; // init-declarator-list |
| 2036 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2037 | /// int x : 4; // struct-declarator |
Chris Lattner | c83c27a | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 2038 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2039 | /// |
| 2040 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 2041 | /// ')' happens to be valid anyway). |
| 2042 | /// int (x) |
| 2043 | /// |
| 2044 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 2045 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 2046 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2047 | 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] | 2048 | } |
| 2049 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2050 | |
| 2051 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 2052 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 2053 | /// the declspec has no type specifier. In this case, the declspec is either |
| 2054 | /// malformed or is "implicit int" (in K&R and C89). |
| 2055 | /// |
| 2056 | /// This method handles diagnosing this prettily and returns false if the |
| 2057 | /// declspec is done being processed. If it recovers and thinks there may be |
| 2058 | /// other pieces of declspec after it, it returns true. |
| 2059 | /// |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2060 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2061 | const ParsedTemplateInfo &TemplateInfo, |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2062 | AccessSpecifier AS, DeclSpecContext DSC, |
| 2063 | ParsedAttributesWithRange &Attrs) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2064 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2065 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2066 | SourceLocation Loc = Tok.getLocation(); |
| 2067 | // If we see an identifier that is not a type name, we normally would |
| 2068 | // parse it as the identifer being declared. However, when a typename |
| 2069 | // is typo'd or the definition is not included, this will incorrectly |
| 2070 | // parse the typename as the identifier name and fall over misparsing |
| 2071 | // later parts of the diagnostic. |
| 2072 | // |
| 2073 | // As such, we try to do some look-ahead in cases where this would |
| 2074 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 2075 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 2076 | // an identifier with implicit int, we'd get a parse error because the |
| 2077 | // next token is obviously invalid for a type. Parse these as a case |
| 2078 | // with an invalid type specifier. |
| 2079 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2080 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2081 | // 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] | 2082 | // error, do lookahead to try to do better recovery. This never applies |
| 2083 | // within a type specifier. Outside of C++, we allow this even if the |
| 2084 | // language doesn't "officially" support implicit int -- we support |
Richard Smith | 58eb370 | 2013-04-30 22:43:51 +0000 | [diff] [blame] | 2085 | // implicit int as an extension in C99 and C11. |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 2086 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
Richard Smith | 58eb370 | 2013-04-30 22:43:51 +0000 | [diff] [blame] | 2087 | !getLangOpts().CPlusPlus && |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2088 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2089 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 2090 | // we just avoid eating the identifier, so it will be parsed as the |
| 2091 | // identifier in the declarator. |
| 2092 | return false; |
| 2093 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2094 | |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2095 | if (getLangOpts().CPlusPlus && |
| 2096 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 2097 | // Don't require a type specifier if we have the 'auto' storage class |
| 2098 | // specifier in C++98 -- we'll promote it to a type specifier. |
| 2099 | return false; |
| 2100 | } |
| 2101 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2102 | // Otherwise, if we don't consume this token, we are going to emit an |
| 2103 | // error anyway. Try to recover from various common problems. Check |
| 2104 | // to see if this was a reference to a tag name without a tag specified. |
| 2105 | // 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] | 2106 | // |
| 2107 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 2108 | if (SS == 0) { |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2109 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2110 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2111 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2112 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2113 | default: break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2114 | case DeclSpec::TST_enum: |
| 2115 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 2116 | case DeclSpec::TST_union: |
| 2117 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 2118 | case DeclSpec::TST_struct: |
| 2119 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2120 | case DeclSpec::TST_interface: |
| 2121 | TagName="__interface"; FixitTagName = "__interface "; |
| 2122 | TagKind=tok::kw___interface;break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2123 | case DeclSpec::TST_class: |
| 2124 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2125 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2126 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2127 | if (TagName) { |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2128 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 2129 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 2130 | Sema::LookupOrdinaryName); |
| 2131 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2132 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2133 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 2134 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 2135 | |
| 2136 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 2137 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 2138 | I != IEnd; ++I) |
Kaelyn Uhrain | 392b3f5 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 2139 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2140 | << TokenName << TagName; |
| 2141 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2142 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2143 | // Parse this as a tag as if the missing tag were present. |
| 2144 | if (TagKind == tok::kw_enum) |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2145 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2146 | else |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2147 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2148 | /*EnteringContext*/ false, DSC_normal, Attrs); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2149 | return true; |
| 2150 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2151 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2152 | |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2153 | // Determine whether this identifier could plausibly be the name of something |
Richard Smith | 7514db2 | 2012-05-15 21:42:17 +0000 | [diff] [blame] | 2154 | // being declared (with a missing type). |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2155 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
| 2156 | (!SS || DSC == DSC_top_level || DSC == DSC_class)) { |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2157 | // Look ahead to the next token to try to figure out what this declaration |
| 2158 | // was supposed to be. |
| 2159 | switch (NextToken().getKind()) { |
| 2160 | case tok::comma: |
| 2161 | case tok::equal: |
| 2162 | case tok::kw_asm: |
| 2163 | case tok::l_brace: |
| 2164 | case tok::l_square: |
| 2165 | case tok::semi: |
| 2166 | // This looks like a variable declaration. The type is probably missing. |
| 2167 | // We're done parsing decl-specifiers. |
| 2168 | return false; |
| 2169 | |
| 2170 | case tok::l_paren: { |
| 2171 | // static x(4); // 'x' is not a type |
| 2172 | // x(int n); // 'x' is not a type |
| 2173 | // x (*p)[]; // 'x' is a type |
| 2174 | // |
| 2175 | // Since we're in an error case (or the rare 'implicit int in C++' MS |
| 2176 | // extension), we can afford to perform a tentative parse to determine |
| 2177 | // which case we're in. |
| 2178 | TentativeParsingAction PA(*this); |
| 2179 | ConsumeToken(); |
| 2180 | TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false); |
| 2181 | PA.Revert(); |
| 2182 | if (TPR == TPResult::False()) |
| 2183 | return false; |
| 2184 | // The identifier is followed by a parenthesized declarator. |
| 2185 | // It's supposed to be a type. |
| 2186 | break; |
| 2187 | } |
| 2188 | |
| 2189 | default: |
| 2190 | // This is probably supposed to be a type. This includes cases like: |
| 2191 | // int f(itn); |
| 2192 | // struct S { unsinged : 4; }; |
| 2193 | break; |
| 2194 | } |
| 2195 | } |
| 2196 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2197 | // 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] | 2198 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2199 | ParsedType T; |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2200 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2201 | if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2202 | // The action emitted a diagnostic, so we don't have to. |
| 2203 | if (T) { |
| 2204 | // The action has suggested that the type T could be used. Set that as |
| 2205 | // the type in the declaration specifiers, consume the would-be type |
| 2206 | // name token, and we're done. |
| 2207 | const char *PrevSpec; |
| 2208 | unsigned DiagID; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2209 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2210 | DS.SetRangeEnd(Tok.getLocation()); |
| 2211 | ConsumeToken(); |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2212 | // There may be other declaration specifiers after this. |
| 2213 | return true; |
| 2214 | } else if (II != Tok.getIdentifierInfo()) { |
| 2215 | // If no type was suggested, the correction is to a keyword |
| 2216 | Tok.setKind(II->getTokenID()); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2217 | // There may be other declaration specifiers after this. |
| 2218 | return true; |
| 2219 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2220 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2221 | // Fall through; the action had no suggestion for us. |
| 2222 | } else { |
| 2223 | // The action did not emit a diagnostic, so emit one now. |
| 2224 | SourceRange R; |
| 2225 | if (SS) R = SS->getRange(); |
| 2226 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 2227 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2228 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2229 | // Mark this as an error. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2230 | DS.SetTypeSpecError(); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2231 | DS.SetRangeEnd(Tok.getLocation()); |
| 2232 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2233 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2234 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 2235 | // avoid rippling error messages on subsequent uses of the same type, |
| 2236 | // could be useful if #include was forgotten. |
| 2237 | return false; |
| 2238 | } |
| 2239 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2240 | /// \brief Determine the declaration specifier context from the declarator |
| 2241 | /// context. |
| 2242 | /// |
| 2243 | /// \param Context the declarator context, which is one of the |
| 2244 | /// Declarator::TheContext enumerator values. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2245 | Parser::DeclSpecContext |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2246 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 2247 | if (Context == Declarator::MemberContext) |
| 2248 | return DSC_class; |
| 2249 | if (Context == Declarator::FileContext) |
| 2250 | return DSC_top_level; |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 2251 | if (Context == Declarator::TrailingReturnContext) |
| 2252 | return DSC_trailing; |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2253 | return DSC_normal; |
| 2254 | } |
| 2255 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2256 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 2257 | /// |
| 2258 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 2259 | /// type. Ideally, the type should be propagated directly into Sema. |
| 2260 | /// |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2261 | /// [C11] type-id |
| 2262 | /// [C11] constant-expression |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2263 | /// [C++0x] type-id ...[opt] |
| 2264 | /// [C++0x] assignment-expression ...[opt] |
| 2265 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 2266 | SourceLocation &EllipsisLoc) { |
| 2267 | ExprResult ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2268 | if (isTypeIdInParens()) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2269 | SourceLocation TypeLoc = Tok.getLocation(); |
| 2270 | ParsedType Ty = ParseTypeName().get(); |
| 2271 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2272 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 2273 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2274 | } else |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2275 | ER = ParseConstantExpression(); |
| 2276 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2277 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis)) |
Peter Collingbourne | fe9b2a8 | 2011-10-24 17:56:00 +0000 | [diff] [blame] | 2278 | EllipsisLoc = ConsumeToken(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2279 | |
| 2280 | return ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 2284 | /// attribute to Attrs. |
| 2285 | /// |
| 2286 | /// alignment-specifier: |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2287 | /// [C11] '_Alignas' '(' type-id ')' |
| 2288 | /// [C11] '_Alignas' '(' constant-expression ')' |
Richard Smith | 33f04a2 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2289 | /// [C++11] 'alignas' '(' type-id ...[opt] ')' |
| 2290 | /// [C++11] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2291 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2292 | SourceLocation *EndLoc) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2293 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 2294 | "Not an alignment-specifier!"); |
| 2295 | |
Richard Smith | 33f04a2 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2296 | IdentifierInfo *KWName = Tok.getIdentifierInfo(); |
| 2297 | SourceLocation KWLoc = ConsumeToken(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2298 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2299 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2300 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2301 | return; |
| 2302 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2303 | SourceLocation EllipsisLoc; |
| 2304 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2305 | if (ArgExpr.isInvalid()) { |
| 2306 | SkipUntil(tok::r_paren); |
| 2307 | return; |
| 2308 | } |
| 2309 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2310 | T.consumeClose(); |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2311 | if (EndLoc) |
| 2312 | *EndLoc = T.getCloseLocation(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2313 | |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 2314 | ArgsVector ArgExprs; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2315 | ArgExprs.push_back(ArgExpr.release()); |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 2316 | Attrs.addNew(KWName, KWLoc, 0, KWLoc, ArgExprs.data(), 1, |
| 2317 | AttributeList::AS_Keyword, EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2320 | /// ParseDeclarationSpecifiers |
| 2321 | /// declaration-specifiers: [C99 6.7] |
| 2322 | /// storage-class-specifier declaration-specifiers[opt] |
| 2323 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2324 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2325 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2326 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2327 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2328 | /// |
| 2329 | /// storage-class-specifier: [C99 6.7.1] |
| 2330 | /// 'typedef' |
| 2331 | /// 'extern' |
| 2332 | /// 'static' |
| 2333 | /// 'auto' |
| 2334 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2335 | /// [C++] 'mutable' |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2336 | /// [C++11] 'thread_local' |
| 2337 | /// [C11] '_Thread_local' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2338 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2339 | /// function-specifier: [C99 6.7.4] |
| 2340 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2341 | /// [C++] 'virtual' |
| 2342 | /// [C++] 'explicit' |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2343 | /// [OpenCL] '__kernel' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2344 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2345 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2346 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2347 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 2348 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2349 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2350 | AccessSpecifier AS, |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2351 | DeclSpecContext DSContext, |
| 2352 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2353 | if (DS.getSourceRange().isInvalid()) { |
| 2354 | DS.SetRangeStart(Tok.getLocation()); |
| 2355 | DS.SetRangeEnd(Tok.getLocation()); |
| 2356 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2357 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2358 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2359 | bool AttrsLastTime = false; |
| 2360 | ParsedAttributesWithRange attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2361 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2362 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2363 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2364 | unsigned DiagID = 0; |
| 2365 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2366 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2367 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2368 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2369 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2370 | DoneWithDeclSpec: |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2371 | if (!AttrsLastTime) |
| 2372 | ProhibitAttributes(attrs); |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2373 | else { |
| 2374 | // Reject C++11 attributes that appertain to decl specifiers as |
| 2375 | // we don't support any C++11 attributes that appertain to decl |
| 2376 | // specifiers. This also conforms to what g++ 4.8 is doing. |
| 2377 | ProhibitCXX11Attributes(attrs); |
| 2378 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2379 | DS.takeAttributesFrom(attrs); |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2380 | } |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 2381 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2382 | // If this is not a declaration specifier token, we're done reading decl |
| 2383 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2384 | DS.Finish(Diags, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2385 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2386 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2387 | case tok::l_square: |
| 2388 | case tok::kw_alignas: |
Richard Smith | 672edb0 | 2013-02-22 09:15:49 +0000 | [diff] [blame] | 2389 | if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier()) |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2390 | goto DoneWithDeclSpec; |
| 2391 | |
| 2392 | ProhibitAttributes(attrs); |
| 2393 | // FIXME: It would be good to recover by accepting the attributes, |
| 2394 | // but attempting to do that now would cause serious |
| 2395 | // madness in terms of diagnostics. |
| 2396 | attrs.clear(); |
| 2397 | attrs.Range = SourceRange(); |
| 2398 | |
| 2399 | ParseCXX11Attributes(attrs); |
| 2400 | AttrsLastTime = true; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2401 | continue; |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2402 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2403 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2404 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2405 | if (DS.hasTypeSpecifier()) { |
| 2406 | bool AllowNonIdentifiers |
| 2407 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 2408 | Scope::BlockScope | |
| 2409 | Scope::TemplateParamScope | |
| 2410 | Scope::FunctionPrototypeScope | |
| 2411 | Scope::AtCatchScope)) == 0; |
| 2412 | bool AllowNestedNameSpecifiers |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2413 | = DSContext == DSC_top_level || |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2414 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 2415 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2416 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2417 | AllowNonIdentifiers, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2418 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2419 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2420 | } |
| 2421 | |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2422 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 2423 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 2424 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2425 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2426 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2427 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2428 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 2429 | else if (CurParsedObjCImpl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2430 | CCC = Sema::PCC_ObjCImplementation; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2431 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2432 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2433 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2434 | } |
| 2435 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2436 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2437 | // C++ scope specifier. Annotate and loop, or bail out on error. |
Eli Friedman | 5a42820 | 2013-08-15 23:59:20 +0000 | [diff] [blame] | 2438 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2439 | if (!DS.hasTypeSpecifier()) |
| 2440 | DS.SetTypeSpecError(); |
| 2441 | goto DoneWithDeclSpec; |
| 2442 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 2443 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 2444 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2445 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2446 | |
| 2447 | case tok::annot_cxxscope: { |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2448 | if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector()) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2449 | goto DoneWithDeclSpec; |
| 2450 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2451 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 2452 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 2453 | Tok.getAnnotationRange(), |
| 2454 | SS); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2455 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2456 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2457 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2458 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2459 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2460 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2461 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2462 | |
| 2463 | // C++ [class.qual]p2: |
| 2464 | // In a lookup in which the constructor is an acceptable lookup |
| 2465 | // result and the nested-name-specifier nominates a class C: |
| 2466 | // |
| 2467 | // - if the name specified after the |
| 2468 | // nested-name-specifier, when looked up in C, is the |
| 2469 | // injected-class-name of C (Clause 9), or |
| 2470 | // |
| 2471 | // - if the name specified after the nested-name-specifier |
| 2472 | // is the same as the identifier or the |
| 2473 | // simple-template-id's template-name in the last |
| 2474 | // component of the nested-name-specifier, |
| 2475 | // |
| 2476 | // the name is instead considered to name the constructor of |
| 2477 | // class C. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2478 | // |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2479 | // Thus, if the template-name is actually the constructor |
| 2480 | // name, then the code is ill-formed; this interpretation is |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2481 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2482 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2483 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2484 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2485 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2486 | if (isConstructorDeclarator()) { |
| 2487 | // The user meant this to be an out-of-line constructor |
| 2488 | // definition, but template arguments are not allowed |
| 2489 | // there. Just allow this as a constructor; we'll |
| 2490 | // complain about it later. |
| 2491 | goto DoneWithDeclSpec; |
| 2492 | } |
| 2493 | |
| 2494 | // The user meant this to name a type, but it actually names |
| 2495 | // a constructor with some extraneous template |
| 2496 | // arguments. Complain, then parse it as a type as the user |
| 2497 | // intended. |
| 2498 | Diag(TemplateId->TemplateNameLoc, |
| 2499 | diag::err_out_of_line_template_id_names_constructor) |
| 2500 | << TemplateId->Name; |
| 2501 | } |
| 2502 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2503 | DS.getTypeSpecScope() = SS; |
| 2504 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2505 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2506 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2507 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2508 | continue; |
| 2509 | } |
| 2510 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2511 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2512 | DS.getTypeSpecScope() = SS; |
| 2513 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2514 | if (Tok.getAnnotationValue()) { |
| 2515 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2516 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2517 | Tok.getAnnotationEndLoc(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2518 | PrevSpec, DiagID, T); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2519 | if (isInvalid) |
| 2520 | break; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2521 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2522 | else |
| 2523 | DS.SetTypeSpecError(); |
| 2524 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2525 | ConsumeToken(); // The typename |
| 2526 | } |
| 2527 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2528 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2529 | goto DoneWithDeclSpec; |
| 2530 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2531 | // If we're in a context where the identifier could be a class name, |
| 2532 | // check whether this is a constructor declaration. |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2533 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2534 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2535 | &SS)) { |
| 2536 | if (isConstructorDeclarator()) |
| 2537 | goto DoneWithDeclSpec; |
| 2538 | |
| 2539 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 2540 | // of the class is qualified in a context where it could name |
| 2541 | // a constructor, its a constructor name. However, we've |
| 2542 | // looked at the declarator, and the user probably meant this |
| 2543 | // to be a type. Complain that it isn't supposed to be treated |
| 2544 | // as a type, then proceed to parse it as a type. |
| 2545 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 2546 | << Next.getIdentifierInfo(); |
| 2547 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2548 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2549 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 2550 | Next.getLocation(), |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2551 | getCurScope(), &SS, |
| 2552 | false, false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2553 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2554 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2555 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2556 | // If the referenced identifier is not a type, then this declspec is |
| 2557 | // erroneous: We already checked about that it has no type specifier, and |
| 2558 | // 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] | 2559 | // typename. |
David Blaikie | 7247c88 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 2560 | if (!TypeRep) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2561 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2562 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2563 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) { |
| 2564 | if (!Attrs.empty()) { |
| 2565 | AttrsLastTime = true; |
| 2566 | attrs.takeAllFrom(Attrs); |
| 2567 | } |
| 2568 | continue; |
| 2569 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2570 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2571 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2572 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2573 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2574 | ConsumeToken(); // The C++ scope. |
| 2575 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2576 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2577 | DiagID, TypeRep); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2578 | if (isInvalid) |
| 2579 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2580 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2581 | DS.SetRangeEnd(Tok.getLocation()); |
| 2582 | ConsumeToken(); // The typename. |
| 2583 | |
| 2584 | continue; |
| 2585 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2586 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2587 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2588 | if (Tok.getAnnotationValue()) { |
| 2589 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | c43271e | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 2590 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2591 | DiagID, T); |
| 2592 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2593 | DS.SetTypeSpecError(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2594 | |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 2595 | if (isInvalid) |
| 2596 | break; |
| 2597 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2598 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2599 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2600 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2601 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2602 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2603 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2604 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2605 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2606 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2607 | continue; |
| 2608 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2609 | |
Douglas Gregor | bfad915 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2610 | case tok::kw___is_signed: |
| 2611 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 2612 | // typically treats it as a trait. If we see __is_signed as it appears |
| 2613 | // in libstdc++, e.g., |
| 2614 | // |
| 2615 | // static const bool __is_signed; |
| 2616 | // |
| 2617 | // then treat __is_signed as an identifier rather than as a keyword. |
| 2618 | if (DS.getTypeSpecType() == TST_bool && |
| 2619 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
| 2620 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 2621 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
| 2622 | Tok.setKind(tok::identifier); |
| 2623 | } |
| 2624 | |
| 2625 | // We're done with the declaration-specifiers. |
| 2626 | goto DoneWithDeclSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2627 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2628 | // typedef-name |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2629 | case tok::kw_decltype: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2630 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2631 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 2632 | // so handle it as such. This is important for ctor parsing. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2633 | if (getLangOpts().CPlusPlus) { |
Eli Friedman | 5a42820 | 2013-08-15 23:59:20 +0000 | [diff] [blame] | 2634 | if (TryAnnotateCXXScopeToken(EnteringContext)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2635 | if (!DS.hasTypeSpecifier()) |
| 2636 | DS.SetTypeSpecError(); |
| 2637 | goto DoneWithDeclSpec; |
| 2638 | } |
| 2639 | if (!Tok.is(tok::identifier)) |
| 2640 | continue; |
| 2641 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2642 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2643 | // This identifier can only be a typedef name if we haven't already seen |
| 2644 | // a type-specifier. Without this check we misparse: |
| 2645 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 2646 | if (DS.hasTypeSpecifier()) |
| 2647 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2648 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2649 | // Check for need to substitute AltiVec keyword tokens. |
| 2650 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2651 | break; |
| 2652 | |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2653 | // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not |
| 2654 | // allow the use of a typedef name as a type specifier. |
| 2655 | if (DS.isTypeAltiVecVector()) |
| 2656 | goto DoneWithDeclSpec; |
| 2657 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2658 | ParsedType TypeRep = |
| 2659 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 2660 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2661 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2662 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 2663 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2664 | if (!TypeRep) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2665 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2666 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) { |
| 2667 | if (!Attrs.empty()) { |
| 2668 | AttrsLastTime = true; |
| 2669 | attrs.takeAllFrom(Attrs); |
| 2670 | } |
| 2671 | continue; |
| 2672 | } |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2673 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2674 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2675 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2676 | // If we're in a context where the identifier could be a class name, |
| 2677 | // check whether this is a constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2678 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2679 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2680 | isConstructorDeclarator()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2681 | goto DoneWithDeclSpec; |
| 2682 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2683 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2684 | DiagID, TypeRep); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2685 | if (isInvalid) |
| 2686 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2687 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2688 | DS.SetRangeEnd(Tok.getLocation()); |
| 2689 | ConsumeToken(); // The identifier |
| 2690 | |
| 2691 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2692 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2693 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2694 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2695 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2696 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2697 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2698 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2699 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2700 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2701 | |
| 2702 | // type-name |
| 2703 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2704 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2705 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2706 | // This template-id does not refer to a type name, so we're |
| 2707 | // done with the type-specifiers. |
| 2708 | goto DoneWithDeclSpec; |
| 2709 | } |
| 2710 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2711 | // If we're in a context where the template-id could be a |
| 2712 | // constructor name or specialization, check whether this is a |
| 2713 | // constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2714 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2715 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2716 | isConstructorDeclarator()) |
| 2717 | goto DoneWithDeclSpec; |
| 2718 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2719 | // Turn the template-id annotation token into a type annotation |
| 2720 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2721 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2722 | continue; |
| 2723 | } |
| 2724 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2725 | // GNU attributes support. |
| 2726 | case tok::kw___attribute: |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2727 | ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2728 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2729 | |
| 2730 | // Microsoft declspec support. |
| 2731 | case tok::kw___declspec: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2732 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2733 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2734 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2735 | // Microsoft single token adornments. |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2736 | case tok::kw___forceinline: { |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2737 | isInvalid = DS.setFunctionSpecInline(Loc); |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2738 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2739 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 2740 | // FIXME: This does not work correctly if it is set to be a declspec |
| 2741 | // attribute, and a GNU attribute is simply incorrect. |
Aaron Ballman | 624421f | 2013-08-31 01:11:41 +0000 | [diff] [blame^] | 2742 | DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, 0, |
| 2743 | AttributeList::AS_GNU); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2744 | break; |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2745 | } |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2746 | |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 2747 | case tok::kw___sptr: |
| 2748 | case tok::kw___uptr: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2749 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2750 | case tok::kw___ptr32: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2751 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2752 | case tok::kw___cdecl: |
| 2753 | case tok::kw___stdcall: |
| 2754 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2755 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2756 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2757 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2758 | continue; |
| 2759 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2760 | // Borland single token adornments. |
| 2761 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2762 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2763 | continue; |
| 2764 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2765 | // OpenCL single token adornments. |
| 2766 | case tok::kw___kernel: |
| 2767 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2768 | continue; |
| 2769 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2770 | // storage-class-specifier |
| 2771 | case tok::kw_typedef: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2772 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
| 2773 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2774 | break; |
| 2775 | case tok::kw_extern: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2776 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2777 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2778 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
| 2779 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2780 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2781 | case tok::kw___private_extern__: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2782 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
| 2783 | Loc, PrevSpec, DiagID); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2784 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2785 | case tok::kw_static: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2786 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2787 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2788 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
| 2789 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2790 | break; |
| 2791 | case tok::kw_auto: |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2792 | if (getLangOpts().CPlusPlus11) { |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2793 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2794 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2795 | PrevSpec, DiagID); |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2796 | if (!isInvalid) |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2797 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2798 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2799 | } else |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2800 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 2801 | DiagID); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2802 | } else |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2803 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2804 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2805 | break; |
| 2806 | case tok::kw_register: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2807 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
| 2808 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2809 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2810 | case tok::kw_mutable: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2811 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
| 2812 | PrevSpec, DiagID); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2813 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2814 | case tok::kw___thread: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2815 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc, |
| 2816 | PrevSpec, DiagID); |
| 2817 | break; |
| 2818 | case tok::kw_thread_local: |
| 2819 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc, |
| 2820 | PrevSpec, DiagID); |
| 2821 | break; |
| 2822 | case tok::kw__Thread_local: |
| 2823 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local, |
| 2824 | Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2825 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2826 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2827 | // function-specifier |
| 2828 | case tok::kw_inline: |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2829 | isInvalid = DS.setFunctionSpecInline(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2830 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2831 | case tok::kw_virtual: |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2832 | isInvalid = DS.setFunctionSpecVirtual(Loc); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2833 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2834 | case tok::kw_explicit: |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2835 | isInvalid = DS.setFunctionSpecExplicit(Loc); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2836 | break; |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 2837 | case tok::kw__Noreturn: |
| 2838 | if (!getLangOpts().C11) |
| 2839 | Diag(Loc, diag::ext_c11_noreturn); |
| 2840 | isInvalid = DS.setFunctionSpecNoreturn(Loc); |
| 2841 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2842 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2843 | // alignment-specifier |
| 2844 | case tok::kw__Alignas: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2845 | if (!getLangOpts().C11) |
Jordan Rose | f70a886 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2846 | Diag(Tok, diag::ext_c11_alignment) << Tok.getName(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2847 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2848 | continue; |
| 2849 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2850 | // friend |
| 2851 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2852 | if (DSContext == DSC_class) |
| 2853 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 2854 | else { |
| 2855 | PrevSpec = ""; // not actually used by the diagnostic |
| 2856 | DiagID = diag::err_friend_invalid_in_context; |
| 2857 | isInvalid = true; |
| 2858 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2859 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2860 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2861 | // Modules |
| 2862 | case tok::kw___module_private__: |
| 2863 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 2864 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2865 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2866 | // constexpr |
| 2867 | case tok::kw_constexpr: |
| 2868 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 2869 | break; |
| 2870 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2871 | // type-specifier |
| 2872 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2873 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 2874 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2875 | break; |
| 2876 | case tok::kw_long: |
| 2877 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2878 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2879 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2880 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2881 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2882 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2883 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2884 | case tok::kw___int64: |
| 2885 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2886 | DiagID); |
| 2887 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2888 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2889 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 2890 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2891 | break; |
| 2892 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2893 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2894 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2895 | break; |
| 2896 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2897 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2898 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2899 | break; |
| 2900 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2901 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2902 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2903 | break; |
| 2904 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2905 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 2906 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2907 | break; |
| 2908 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2909 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 2910 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2911 | break; |
| 2912 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2913 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 2914 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2915 | break; |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 2916 | case tok::kw___int128: |
| 2917 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
| 2918 | DiagID); |
| 2919 | break; |
| 2920 | case tok::kw_half: |
| 2921 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
| 2922 | DiagID); |
| 2923 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2924 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2925 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 2926 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2927 | break; |
| 2928 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2929 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 2930 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2931 | break; |
| 2932 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2933 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 2934 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2935 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2936 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2937 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 2938 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2939 | break; |
| 2940 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2941 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 2942 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2943 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2944 | case tok::kw_bool: |
| 2945 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2946 | if (Tok.is(tok::kw_bool) && |
| 2947 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 2948 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 2949 | PrevSpec = ""; // Not used by the diagnostic. |
| 2950 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2951 | // For better error recovery. |
| 2952 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2953 | isInvalid = true; |
| 2954 | } else { |
| 2955 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 2956 | DiagID); |
| 2957 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2958 | break; |
| 2959 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2960 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2961 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2962 | break; |
| 2963 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2964 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2965 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2966 | break; |
| 2967 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2968 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2969 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2970 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2971 | case tok::kw___vector: |
| 2972 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2973 | break; |
| 2974 | case tok::kw___pixel: |
| 2975 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2976 | break; |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 2977 | case tok::kw_image1d_t: |
| 2978 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc, |
| 2979 | PrevSpec, DiagID); |
| 2980 | break; |
| 2981 | case tok::kw_image1d_array_t: |
| 2982 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc, |
| 2983 | PrevSpec, DiagID); |
| 2984 | break; |
| 2985 | case tok::kw_image1d_buffer_t: |
| 2986 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc, |
| 2987 | PrevSpec, DiagID); |
| 2988 | break; |
| 2989 | case tok::kw_image2d_t: |
| 2990 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc, |
| 2991 | PrevSpec, DiagID); |
| 2992 | break; |
| 2993 | case tok::kw_image2d_array_t: |
| 2994 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc, |
| 2995 | PrevSpec, DiagID); |
| 2996 | break; |
| 2997 | case tok::kw_image3d_t: |
| 2998 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc, |
| 2999 | PrevSpec, DiagID); |
| 3000 | break; |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3001 | case tok::kw_sampler_t: |
| 3002 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc, |
| 3003 | PrevSpec, DiagID); |
| 3004 | break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3005 | case tok::kw_event_t: |
| 3006 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc, |
| 3007 | PrevSpec, DiagID); |
| 3008 | break; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3009 | case tok::kw___unknown_anytype: |
| 3010 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 3011 | PrevSpec, DiagID); |
| 3012 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3013 | |
| 3014 | // class-specifier: |
| 3015 | case tok::kw_class: |
| 3016 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3017 | case tok::kw___interface: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3018 | case tok::kw_union: { |
| 3019 | tok::TokenKind Kind = Tok.getKind(); |
| 3020 | ConsumeToken(); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3021 | |
| 3022 | // These are attributes following class specifiers. |
| 3023 | // To produce better diagnostic, we parse them when |
| 3024 | // parsing class specifier. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3025 | ParsedAttributesWithRange Attributes(AttrFactory); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3026 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3027 | EnteringContext, DSContext, Attributes); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3028 | |
| 3029 | // If there are attributes following class specifier, |
| 3030 | // take them over and handle them here. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3031 | if (!Attributes.empty()) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3032 | AttrsLastTime = true; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3033 | attrs.takeAllFrom(Attributes); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3034 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3035 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3036 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3037 | |
| 3038 | // enum-specifier: |
| 3039 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3040 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3041 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3042 | continue; |
| 3043 | |
| 3044 | // cv-qualifier: |
| 3045 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3046 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3047 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3048 | break; |
| 3049 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3050 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, 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_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3054 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, 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 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3058 | // C++ typename-specifier: |
| 3059 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3060 | if (TryAnnotateTypeOrScopeToken()) { |
| 3061 | DS.SetTypeSpecError(); |
| 3062 | goto DoneWithDeclSpec; |
| 3063 | } |
| 3064 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3065 | continue; |
| 3066 | break; |
| 3067 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3068 | // GNU typeof support. |
| 3069 | case tok::kw_typeof: |
| 3070 | ParseTypeofSpecifier(DS); |
| 3071 | continue; |
| 3072 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3073 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 3074 | ParseDecltypeSpecifier(DS); |
| 3075 | continue; |
| 3076 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3077 | case tok::kw___underlying_type: |
| 3078 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3079 | continue; |
| 3080 | |
| 3081 | case tok::kw__Atomic: |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3082 | // C11 6.7.2.4/4: |
| 3083 | // If the _Atomic keyword is immediately followed by a left parenthesis, |
| 3084 | // it is interpreted as a type specifier (with a type name), not as a |
| 3085 | // type qualifier. |
| 3086 | if (NextToken().is(tok::l_paren)) { |
| 3087 | ParseAtomicSpecifier(DS); |
| 3088 | continue; |
| 3089 | } |
| 3090 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 3091 | getLangOpts()); |
| 3092 | break; |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3093 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3094 | // OpenCL qualifiers: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3095 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3096 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3097 | goto DoneWithDeclSpec; |
| 3098 | case tok::kw___private: |
| 3099 | case tok::kw___global: |
| 3100 | case tok::kw___local: |
| 3101 | case tok::kw___constant: |
| 3102 | case tok::kw___read_only: |
| 3103 | case tok::kw___write_only: |
| 3104 | case tok::kw___read_write: |
| 3105 | ParseOpenCLQualifiers(DS); |
| 3106 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3107 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 3108 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3109 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3110 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 3111 | // but we support it. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3112 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3113 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3114 | |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 3115 | if (!ParseObjCProtocolQualifiers(DS)) |
| 3116 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 3117 | << FixItHint::CreateInsertion(Loc, "id") |
| 3118 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3119 | |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 3120 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 3121 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 3122 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3123 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3124 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3125 | if (isInvalid) { |
| 3126 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3127 | assert(DiagID); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3128 | |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 3129 | if (DiagID == diag::ext_duplicate_declspec) |
| 3130 | Diag(Tok, DiagID) |
| 3131 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 3132 | else |
| 3133 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3134 | } |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3135 | |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 3136 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3137 | if (DiagID != diag::err_bool_redeclaration) |
| 3138 | ConsumeToken(); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3139 | |
| 3140 | AttrsLastTime = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3141 | } |
| 3142 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 3143 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3144 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 3145 | /// semicolon. |
| 3146 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3147 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3148 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3149 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3150 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3151 | /// struct-declarator-list: |
| 3152 | /// struct-declarator |
| 3153 | /// struct-declarator-list ',' struct-declarator |
| 3154 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 3155 | /// struct-declarator: |
| 3156 | /// declarator |
| 3157 | /// [GNU] declarator attributes[opt] |
| 3158 | /// declarator[opt] ':' constant-expression |
| 3159 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 3160 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3161 | void Parser:: |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3162 | ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3163 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3164 | if (Tok.is(tok::kw___extension__)) { |
| 3165 | // __extension__ silences extension warnings in the subexpression. |
| 3166 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3167 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3168 | return ParseStructDeclaration(DS, Fields); |
| 3169 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3170 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3171 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3172 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3173 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3174 | // If there are no declarators, this is a free-standing declaration |
| 3175 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3176 | if (Tok.is(tok::semi)) { |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3177 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
| 3178 | DS); |
| 3179 | DS.complete(TheDecl); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3180 | return; |
| 3181 | } |
| 3182 | |
| 3183 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3184 | bool FirstDeclarator = true; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3185 | SourceLocation CommaLoc; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3186 | while (1) { |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3187 | ParsingFieldDeclarator DeclaratorInfo(*this, DS); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3188 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3189 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3190 | // Attributes are only allowed here on successive declarators. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3191 | if (!FirstDeclarator) |
| 3192 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3193 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3194 | /// struct-declarator: declarator |
| 3195 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3196 | if (Tok.isNot(tok::colon)) { |
| 3197 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 3198 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3199 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3200 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3201 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3202 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3203 | ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3204 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3205 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3206 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 3207 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 3208 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3209 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3210 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3211 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3212 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3213 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3214 | // We're done with this declarator; invoke the callback. |
Eli Friedman | 817a886 | 2012-08-08 23:35:12 +0000 | [diff] [blame] | 3215 | Fields.invoke(DeclaratorInfo); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3216 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3217 | // If we don't have a comma, it is either the end of the list (a ';') |
| 3218 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3219 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3220 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3221 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3222 | // Consume the comma. |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3223 | CommaLoc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3224 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3225 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3226 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3227 | } |
| 3228 | |
| 3229 | /// ParseStructUnionBody |
| 3230 | /// struct-contents: |
| 3231 | /// struct-declaration-list |
| 3232 | /// [EXT] empty |
| 3233 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 3234 | /// struct-declaration-list: |
| 3235 | /// struct-declaration |
| 3236 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3237 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3238 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3239 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3240 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3241 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 3242 | "parsing struct/union body"); |
Andy Gibbs | f50f3f7 | 2013-04-03 09:31:19 +0000 | [diff] [blame] | 3243 | assert(!getLangOpts().CPlusPlus && "C++ declarations not supported"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3244 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3245 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3246 | if (T.consumeOpen()) |
| 3247 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3248 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3249 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3250 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3251 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3252 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3253 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3254 | // 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] | 3255 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3256 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3257 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3258 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3259 | if (Tok.is(tok::semi)) { |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3260 | ConsumeExtraSemi(InsideStruct, TagType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3261 | continue; |
| 3262 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3263 | |
Andy Gibbs | 74b9fa1 | 2013-04-03 09:46:04 +0000 | [diff] [blame] | 3264 | // Parse _Static_assert declaration. |
| 3265 | if (Tok.is(tok::kw__Static_assert)) { |
| 3266 | SourceLocation DeclEnd; |
| 3267 | ParseStaticAssertDeclaration(DeclEnd); |
| 3268 | continue; |
| 3269 | } |
| 3270 | |
Argyrios Kyrtzidis | bd95745 | 2013-04-18 01:42:35 +0000 | [diff] [blame] | 3271 | if (Tok.is(tok::annot_pragma_pack)) { |
| 3272 | HandlePragmaPack(); |
| 3273 | continue; |
| 3274 | } |
| 3275 | |
| 3276 | if (Tok.is(tok::annot_pragma_align)) { |
| 3277 | HandlePragmaAlign(); |
| 3278 | continue; |
| 3279 | } |
| 3280 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3281 | if (!Tok.is(tok::at)) { |
| 3282 | struct CFieldCallback : FieldCallback { |
| 3283 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3284 | Decl *TagDecl; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3285 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3286 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3287 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3288 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3289 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 3290 | |
Eli Friedman | dcdff46 | 2012-08-08 23:53:27 +0000 | [diff] [blame] | 3291 | void invoke(ParsingFieldDeclarator &FD) { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3292 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3293 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 3294 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 3295 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3296 | FieldDecls.push_back(Field); |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3297 | FD.complete(Field); |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3298 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3299 | } Callback(*this, TagDecl, FieldDecls); |
| 3300 | |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3301 | // Parse all the comma separated declarators. |
| 3302 | ParsingDeclSpec DS(*this); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3303 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3304 | } else { // Handle @defs |
| 3305 | ConsumeToken(); |
| 3306 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 3307 | Diag(Tok, diag::err_unexpected_at); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3308 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3309 | continue; |
| 3310 | } |
| 3311 | ConsumeToken(); |
| 3312 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 3313 | if (!Tok.is(tok::identifier)) { |
| 3314 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3315 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3316 | continue; |
| 3317 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3318 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3319 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3320 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3321 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 3322 | ConsumeToken(); |
| 3323 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3324 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3325 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3326 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3327 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3328 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3329 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3330 | break; |
| 3331 | } else { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3332 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 3333 | // 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] | 3334 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3335 | // If we stopped at a ';', eat it. |
| 3336 | if (Tok.is(tok::semi)) ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3337 | } |
| 3338 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3339 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3340 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3341 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3342 | ParsedAttributes attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3343 | // If attributes exist after struct contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3344 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 3345 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3346 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 3347 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3348 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3349 | attrs.getList()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3350 | StructScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3351 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 3352 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3355 | /// ParseEnumSpecifier |
| 3356 | /// enum-specifier: [C99 6.7.2.2] |
| 3357 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3358 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3359 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3360 | /// '}' attributes[opt] |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 3361 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3362 | /// '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3363 | /// 'enum' identifier |
| 3364 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3365 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3366 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 3367 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3368 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3369 | /// enum-head: [C++11] |
| 3370 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 3371 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 3372 | /// identifier enum-base[opt] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3373 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3374 | /// enum-key: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3375 | /// 'enum' |
| 3376 | /// 'enum' 'class' |
| 3377 | /// 'enum' 'struct' |
| 3378 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3379 | /// enum-base: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3380 | /// ':' type-specifier-seq |
| 3381 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3382 | /// [C++] elaborated-type-specifier: |
| 3383 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 3384 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3385 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 3386 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3387 | AccessSpecifier AS, DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3388 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3389 | if (Tok.is(tok::code_completion)) { |
| 3390 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3391 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3392 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3393 | } |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3394 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3395 | // If attributes exist after tag, parse them. |
| 3396 | ParsedAttributesWithRange attrs(AttrFactory); |
| 3397 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3398 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3399 | |
| 3400 | // If declspecs exist after tag, parse them. |
| 3401 | while (Tok.is(tok::kw___declspec)) |
| 3402 | ParseMicrosoftDeclSpec(attrs); |
| 3403 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3404 | SourceLocation ScopedEnumKWLoc; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3405 | bool IsScopedUsingClassTag = false; |
| 3406 | |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3407 | // In C++11, recognize 'enum class' and 'enum struct'. |
Richard Trieu | ed5a292 | 2013-04-23 02:47:36 +0000 | [diff] [blame] | 3408 | if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) { |
| 3409 | Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum |
| 3410 | : diag::ext_scoped_enum); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3411 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3412 | ScopedEnumKWLoc = ConsumeToken(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3413 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3414 | // Attributes are not allowed between these keywords. Diagnose, |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3415 | // but then just treat them like they appeared in the right place. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3416 | ProhibitAttributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3417 | |
| 3418 | // They are allowed afterwards, though. |
| 3419 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3420 | MaybeParseCXX11Attributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3421 | while (Tok.is(tok::kw___declspec)) |
| 3422 | ParseMicrosoftDeclSpec(attrs); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3423 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3424 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3425 | // C++11 [temp.explicit]p12: |
| 3426 | // The usual access controls do not apply to names used to specify |
| 3427 | // explicit instantiations. |
| 3428 | // We extend this to also cover explicit specializations. Note that |
| 3429 | // we don't suppress if this turns out to be an elaborated type |
| 3430 | // specifier. |
| 3431 | bool shouldDelayDiagsInTag = |
| 3432 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 3433 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 3434 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3435 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3436 | // Enum definitions should not be parsed in a trailing-return-type. |
| 3437 | bool AllowDeclaration = DSC != DSC_trailing; |
| 3438 | |
| 3439 | bool AllowFixedUnderlyingType = AllowDeclaration && |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3440 | (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt || |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3441 | getLangOpts().ObjC2); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3442 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3443 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3444 | if (getLangOpts().CPlusPlus) { |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3445 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 3446 | // if a fixed underlying type is allowed. |
| 3447 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3448 | |
| 3449 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Richard Smith | 725fe0e | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 3450 | /*EnteringContext=*/true)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3451 | return; |
| 3452 | |
| 3453 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3454 | Diag(Tok, diag::err_expected_ident); |
| 3455 | if (Tok.isNot(tok::l_brace)) { |
| 3456 | // Has no name and is not a definition. |
| 3457 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3458 | SkipUntil(tok::comma, true); |
| 3459 | return; |
| 3460 | } |
| 3461 | } |
| 3462 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3463 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3464 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3465 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3466 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3467 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3468 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3469 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3470 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3471 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3472 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3473 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3474 | // If an identifier is present, consume and remember it. |
| 3475 | IdentifierInfo *Name = 0; |
| 3476 | SourceLocation NameLoc; |
| 3477 | if (Tok.is(tok::identifier)) { |
| 3478 | Name = Tok.getIdentifierInfo(); |
| 3479 | NameLoc = ConsumeToken(); |
| 3480 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3481 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3482 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3483 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 3484 | // declaration of a scoped enumeration. |
| 3485 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3486 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3487 | IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3490 | // Okay, end the suppression area. We'll decide whether to emit the |
| 3491 | // diagnostics in a second. |
| 3492 | if (shouldDelayDiagsInTag) |
| 3493 | diagsFromTag.done(); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3494 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3495 | TypeResult BaseType; |
| 3496 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3497 | // Parse the fixed underlying type. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3498 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3499 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3500 | bool PossibleBitfield = false; |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3501 | if (CanBeBitfield) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3502 | // If we're in class scope, this can either be an enum declaration with |
| 3503 | // an underlying type, or a declaration of a bitfield member. We try to |
| 3504 | // use a simple disambiguation scheme first to catch the common cases |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3505 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 3506 | // anything that's a simple-type-specifier followed by '(' as an |
| 3507 | // expression. This suffices because function types are not valid |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3508 | // underlying types anyway. |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 3509 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 3510 | Sema::ConstantEvaluated); |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3511 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3512 | // 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] | 3513 | // bit-field. This is the common case. |
| 3514 | if (TPR == TPResult::True()) |
| 3515 | PossibleBitfield = true; |
| 3516 | // If the next token starts a type-specifier-seq, it may be either a |
| 3517 | // 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] | 3518 | // 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] | 3519 | // fixed underlying type. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3520 | else if (TPR == TPResult::False() && |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3521 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 3522 | // Consume the ':'. |
| 3523 | ConsumeToken(); |
| 3524 | } else { |
| 3525 | // We have the start of a type-specifier-seq, so we have to perform |
| 3526 | // tentative parsing to determine whether we have an expression or a |
| 3527 | // type. |
| 3528 | TentativeParsingAction TPA(*this); |
| 3529 | |
| 3530 | // Consume the ':'. |
| 3531 | ConsumeToken(); |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3532 | |
| 3533 | // If we see a type specifier followed by an open-brace, we have an |
| 3534 | // ambiguity between an underlying type and a C++11 braced |
| 3535 | // function-style cast. Resolve this by always treating it as an |
| 3536 | // underlying type. |
| 3537 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 3538 | // this case. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3539 | if ((getLangOpts().CPlusPlus && |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3540 | isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3541 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3542 | // We'll parse this as a bitfield later. |
| 3543 | PossibleBitfield = true; |
| 3544 | TPA.Revert(); |
| 3545 | } else { |
| 3546 | // We have a type-specifier-seq. |
| 3547 | TPA.Commit(); |
| 3548 | } |
| 3549 | } |
| 3550 | } else { |
| 3551 | // Consume the ':'. |
| 3552 | ConsumeToken(); |
| 3553 | } |
| 3554 | |
| 3555 | if (!PossibleBitfield) { |
| 3556 | SourceRange Range; |
| 3557 | BaseType = ParseTypeName(&Range); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3558 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3559 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3560 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Eli Friedman | cef3a7b | 2012-11-02 01:34:28 +0000 | [diff] [blame] | 3561 | } else if (!getLangOpts().ObjC2) { |
| 3562 | if (getLangOpts().CPlusPlus) |
| 3563 | Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range; |
| 3564 | else |
| 3565 | Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range; |
| 3566 | } |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3567 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3568 | } |
| 3569 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3570 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 3571 | // friend declaration, and cannot have an accompanying definition. If we have |
| 3572 | // 'enum foo;', then this is a forward declaration. If we have |
| 3573 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 3574 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3575 | // |
| 3576 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 3577 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 3578 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 3579 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3580 | Sema::TagUseKind TUK; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3581 | if (!AllowDeclaration) { |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3582 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3583 | } else if (Tok.is(tok::l_brace)) { |
| 3584 | if (DS.isFriendSpecified()) { |
| 3585 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
| 3586 | << SourceRange(DS.getFriendSpecLoc()); |
| 3587 | ConsumeBrace(); |
| 3588 | SkipUntil(tok::r_brace); |
| 3589 | TUK = Sema::TUK_Friend; |
| 3590 | } else { |
| 3591 | TUK = Sema::TUK_Definition; |
| 3592 | } |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3593 | } else if (DSC != DSC_type_specifier && |
| 3594 | (Tok.is(tok::semi) || |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3595 | (Tok.isAtStartOfLine() && |
| 3596 | !isValidAfterTypeSpecifier(CanBeBitfield)))) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3597 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
| 3598 | if (Tok.isNot(tok::semi)) { |
| 3599 | // A semicolon was missing after this declaration. Diagnose and recover. |
| 3600 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, |
| 3601 | "enum"); |
| 3602 | PP.EnterToken(Tok); |
| 3603 | Tok.setKind(tok::semi); |
| 3604 | } |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3605 | } else { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3606 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3607 | } |
| 3608 | |
| 3609 | // If this is an elaborated type specifier, and we delayed |
| 3610 | // diagnostics before, just merge them into the current pool. |
| 3611 | if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) { |
| 3612 | diagsFromTag.redelay(); |
| 3613 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3614 | |
| 3615 | MultiTemplateParamsArg TParams; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3616 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3617 | TUK != Sema::TUK_Reference) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3618 | if (!getLangOpts().CPlusPlus11 || !SS.isSet()) { |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3619 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3620 | Diag(Tok, diag::err_enum_template); |
| 3621 | SkipUntil(tok::comma, true); |
| 3622 | return; |
| 3623 | } |
| 3624 | |
| 3625 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 3626 | // Enumerations can't be explicitly instantiated. |
| 3627 | DS.SetTypeSpecError(); |
| 3628 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 3629 | return; |
| 3630 | } |
| 3631 | |
| 3632 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 3633 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 3634 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3635 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3636 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3637 | if (TUK == Sema::TUK_Reference) |
| 3638 | ProhibitAttributes(attrs); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3639 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3640 | if (!Name && TUK != Sema::TUK_Definition) { |
| 3641 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3642 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3643 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3644 | SkipUntil(tok::comma, true); |
| 3645 | return; |
| 3646 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3647 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3648 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3649 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3650 | const char *PrevSpec = 0; |
| 3651 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3652 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3653 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3654 | AS, DS.getModulePrivateSpecLoc(), TParams, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3655 | Owned, IsDependent, ScopedEnumKWLoc, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3656 | IsScopedUsingClassTag, BaseType); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3657 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3658 | if (IsDependent) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3659 | // This enum has a dependent nested-name-specifier. Handle it as a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3660 | // dependent tag. |
| 3661 | if (!Name) { |
| 3662 | DS.SetTypeSpecError(); |
| 3663 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 3664 | return; |
| 3665 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3666 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3667 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3668 | TUK, SS, Name, StartLoc, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3669 | NameLoc); |
| 3670 | if (Type.isInvalid()) { |
| 3671 | DS.SetTypeSpecError(); |
| 3672 | return; |
| 3673 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3674 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3675 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 3676 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3677 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3678 | Diag(StartLoc, DiagID) << PrevSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3679 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3680 | return; |
| 3681 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3682 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3683 | if (!TagDecl) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3684 | // The action failed to produce an enumeration tag. If this is a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3685 | // definition, consume the entire definition. |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3686 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3687 | ConsumeBrace(); |
| 3688 | SkipUntil(tok::r_brace); |
| 3689 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3690 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3691 | DS.SetTypeSpecError(); |
| 3692 | return; |
| 3693 | } |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3694 | |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3695 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3696 | ParseEnumBody(StartLoc, TagDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3697 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3698 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3699 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3700 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3701 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3702 | } |
| 3703 | |
| 3704 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3705 | /// enumerator-list: |
| 3706 | /// enumerator |
| 3707 | /// enumerator-list ',' enumerator |
| 3708 | /// enumerator: |
| 3709 | /// enumeration-constant |
| 3710 | /// enumeration-constant '=' constant-expression |
| 3711 | /// enumeration-constant: |
| 3712 | /// identifier |
| 3713 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3714 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3715 | // Enter the scope of the enum body and start the definition. |
| 3716 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3717 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3718 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3719 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3720 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3721 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3722 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3723 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3724 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3725 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3726 | SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3727 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3728 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3729 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3730 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3731 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3732 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3733 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3734 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3735 | // If attributes exist after the enumerator, parse them. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3736 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3737 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3738 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3739 | ProhibitAttributes(attrs); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3740 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3741 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3742 | ExprResult AssignedVal; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 3743 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3744 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3745 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3746 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3747 | AssignedVal = ParseConstantExpression(); |
| 3748 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3749 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3750 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3751 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3752 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3753 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3754 | LastEnumConstDecl, |
| 3755 | IdentLoc, Ident, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3756 | attrs.getList(), EqualLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3757 | AssignedVal.release()); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3758 | PD.complete(EnumConstDecl); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3759 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3760 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3761 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3762 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3763 | if (Tok.is(tok::identifier)) { |
| 3764 | // We're missing a comma between enumerators. |
| 3765 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3766 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3767 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3768 | continue; |
| 3769 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3770 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3771 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3772 | break; |
| 3773 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3774 | |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3775 | if (Tok.isNot(tok::identifier)) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3776 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11) |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3777 | Diag(CommaLoc, getLangOpts().CPlusPlus ? |
| 3778 | diag::ext_enumerator_list_comma_cxx : |
| 3779 | diag::ext_enumerator_list_comma_c) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3780 | << FixItHint::CreateRemoval(CommaLoc); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3781 | else if (getLangOpts().CPlusPlus11) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3782 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3783 | << FixItHint::CreateRemoval(CommaLoc); |
| 3784 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3785 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3786 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3787 | // Eat the }. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3788 | T.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3789 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3790 | // If attributes exist after the identifier list, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3791 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3792 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3793 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3794 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 3795 | EnumDecl, EnumConstantDecls, |
| 3796 | getCurScope(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3797 | attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3798 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3799 | EnumScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3800 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3801 | T.getCloseLocation()); |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3802 | |
| 3803 | // The next token must be valid after an enum definition. If not, a ';' |
| 3804 | // was probably forgotten. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3805 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
| 3806 | if (!isValidAfterTypeSpecifier(CanBeBitfield)) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3807 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum"); |
| 3808 | // Push this token back into the preprocessor and change our current token |
| 3809 | // to ';' so that the rest of the code recovers as though there were an |
| 3810 | // ';' after the definition. |
| 3811 | PP.EnterToken(Tok); |
| 3812 | Tok.setKind(tok::semi); |
| 3813 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3814 | } |
| 3815 | |
| 3816 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3817 | /// start of a type-qualifier-list. |
| 3818 | bool Parser::isTypeQualifier() const { |
| 3819 | switch (Tok.getKind()) { |
| 3820 | default: return false; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3821 | |
| 3822 | // type-qualifier only in OpenCL |
| 3823 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3824 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3825 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3826 | // type-qualifier |
| 3827 | case tok::kw_const: |
| 3828 | case tok::kw_volatile: |
| 3829 | case tok::kw_restrict: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3830 | case tok::kw___private: |
| 3831 | case tok::kw___local: |
| 3832 | case tok::kw___global: |
| 3833 | case tok::kw___constant: |
| 3834 | case tok::kw___read_only: |
| 3835 | case tok::kw___read_write: |
| 3836 | case tok::kw___write_only: |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3837 | return true; |
| 3838 | } |
| 3839 | } |
| 3840 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3841 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3842 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3843 | /// specifier or if we're not sure. |
| 3844 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3845 | switch (Tok.getKind()) { |
| 3846 | default: return false; |
| 3847 | // type-specifiers |
| 3848 | case tok::kw_short: |
| 3849 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3850 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3851 | case tok::kw___int128: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3852 | case tok::kw_signed: |
| 3853 | case tok::kw_unsigned: |
| 3854 | case tok::kw__Complex: |
| 3855 | case tok::kw__Imaginary: |
| 3856 | case tok::kw_void: |
| 3857 | case tok::kw_char: |
| 3858 | case tok::kw_wchar_t: |
| 3859 | case tok::kw_char16_t: |
| 3860 | case tok::kw_char32_t: |
| 3861 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3862 | case tok::kw_half: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3863 | case tok::kw_float: |
| 3864 | case tok::kw_double: |
| 3865 | case tok::kw_bool: |
| 3866 | case tok::kw__Bool: |
| 3867 | case tok::kw__Decimal32: |
| 3868 | case tok::kw__Decimal64: |
| 3869 | case tok::kw__Decimal128: |
| 3870 | case tok::kw___vector: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3871 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3872 | // OpenCL specific types: |
| 3873 | case tok::kw_image1d_t: |
| 3874 | case tok::kw_image1d_array_t: |
| 3875 | case tok::kw_image1d_buffer_t: |
| 3876 | case tok::kw_image2d_t: |
| 3877 | case tok::kw_image2d_array_t: |
| 3878 | case tok::kw_image3d_t: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3879 | case tok::kw_sampler_t: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3880 | case tok::kw_event_t: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3881 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3882 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3883 | case tok::kw_class: |
| 3884 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3885 | case tok::kw___interface: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3886 | case tok::kw_union: |
| 3887 | // enum-specifier |
| 3888 | case tok::kw_enum: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3889 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3890 | // typedef-name |
| 3891 | case tok::annot_typename: |
| 3892 | return true; |
| 3893 | } |
| 3894 | } |
| 3895 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3896 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3897 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3898 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3899 | switch (Tok.getKind()) { |
| 3900 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3901 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3902 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3903 | if (TryAltiVecVectorToken()) |
| 3904 | return true; |
| 3905 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3906 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3907 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3908 | // recurse to handle whatever we get. |
| 3909 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3910 | return true; |
| 3911 | if (Tok.is(tok::identifier)) |
| 3912 | return false; |
| 3913 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3914 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3915 | case tok::coloncolon: // ::foo::bar |
| 3916 | if (NextToken().is(tok::kw_new) || // ::new |
| 3917 | NextToken().is(tok::kw_delete)) // ::delete |
| 3918 | return false; |
| 3919 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3920 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3921 | return true; |
| 3922 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3923 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3924 | // GNU attributes support. |
| 3925 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3926 | // GNU typeof support. |
| 3927 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3928 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3929 | // type-specifiers |
| 3930 | case tok::kw_short: |
| 3931 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3932 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3933 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3934 | case tok::kw_signed: |
| 3935 | case tok::kw_unsigned: |
| 3936 | case tok::kw__Complex: |
| 3937 | case tok::kw__Imaginary: |
| 3938 | case tok::kw_void: |
| 3939 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3940 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3941 | case tok::kw_char16_t: |
| 3942 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3943 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3944 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3945 | case tok::kw_float: |
| 3946 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3947 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3948 | case tok::kw__Bool: |
| 3949 | case tok::kw__Decimal32: |
| 3950 | case tok::kw__Decimal64: |
| 3951 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3952 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3953 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3954 | // OpenCL specific types: |
| 3955 | case tok::kw_image1d_t: |
| 3956 | case tok::kw_image1d_array_t: |
| 3957 | case tok::kw_image1d_buffer_t: |
| 3958 | case tok::kw_image2d_t: |
| 3959 | case tok::kw_image2d_array_t: |
| 3960 | case tok::kw_image3d_t: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3961 | case tok::kw_sampler_t: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3962 | case tok::kw_event_t: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3963 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3964 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3965 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3966 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3967 | case tok::kw___interface: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3968 | case tok::kw_union: |
| 3969 | // enum-specifier |
| 3970 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3971 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3972 | // type-qualifier |
| 3973 | case tok::kw_const: |
| 3974 | case tok::kw_volatile: |
| 3975 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3976 | |
John McCall | b8a8de3 | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 3977 | // Debugger support. |
| 3978 | case tok::kw___unknown_anytype: |
| 3979 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3980 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 3981 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3982 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3983 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 3984 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3985 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3986 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3987 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3988 | case tok::kw___cdecl: |
| 3989 | case tok::kw___stdcall: |
| 3990 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3991 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3992 | case tok::kw___w64: |
| 3993 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3994 | case tok::kw___ptr32: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3995 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3996 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3997 | |
| 3998 | case tok::kw___private: |
| 3999 | case tok::kw___local: |
| 4000 | case tok::kw___global: |
| 4001 | case tok::kw___constant: |
| 4002 | case tok::kw___read_only: |
| 4003 | case tok::kw___read_write: |
| 4004 | case tok::kw___write_only: |
| 4005 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4006 | return true; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4007 | |
| 4008 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4009 | return getLangOpts().OpenCL; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4010 | |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4011 | // C11 _Atomic |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4012 | case tok::kw__Atomic: |
| 4013 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4014 | } |
| 4015 | } |
| 4016 | |
| 4017 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 4018 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4019 | /// |
| 4020 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 4021 | /// this check is to disambiguate between an expression and a declaration. |
| 4022 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4023 | switch (Tok.getKind()) { |
| 4024 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4025 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4026 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4027 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4028 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4029 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4030 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4031 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4032 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4033 | if (TryAltiVecVectorToken()) |
| 4034 | return true; |
| 4035 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4036 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4037 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4038 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4039 | // recurse to handle whatever we get. |
| 4040 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4041 | return true; |
| 4042 | if (Tok.is(tok::identifier)) |
| 4043 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4044 | |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4045 | // 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] | 4046 | // by an identifier and then either ':' or ']', in a place where an |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4047 | // expression is permitted, then this is probably a class message send |
| 4048 | // missing the initial '['. In this case, we won't consider this to be |
| 4049 | // the start of a declaration. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4050 | if (DisambiguatingWithExpression && |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4051 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 4052 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4053 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4054 | return isDeclarationSpecifier(); |
| 4055 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4056 | case tok::coloncolon: // ::foo::bar |
| 4057 | if (NextToken().is(tok::kw_new) || // ::new |
| 4058 | NextToken().is(tok::kw_delete)) // ::delete |
| 4059 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4060 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4061 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4062 | // recurse to handle whatever we get. |
| 4063 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4064 | return true; |
| 4065 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4066 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4067 | // storage-class-specifier |
| 4068 | case tok::kw_typedef: |
| 4069 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 4070 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4071 | case tok::kw_static: |
| 4072 | case tok::kw_auto: |
| 4073 | case tok::kw_register: |
| 4074 | case tok::kw___thread: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4075 | case tok::kw_thread_local: |
| 4076 | case tok::kw__Thread_local: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4077 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 4078 | // Modules |
| 4079 | case tok::kw___module_private__: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4080 | |
John McCall | b8a8de3 | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4081 | // Debugger support |
| 4082 | case tok::kw___unknown_anytype: |
| 4083 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4084 | // type-specifiers |
| 4085 | case tok::kw_short: |
| 4086 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4087 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4088 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4089 | case tok::kw_signed: |
| 4090 | case tok::kw_unsigned: |
| 4091 | case tok::kw__Complex: |
| 4092 | case tok::kw__Imaginary: |
| 4093 | case tok::kw_void: |
| 4094 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4095 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4096 | case tok::kw_char16_t: |
| 4097 | case tok::kw_char32_t: |
| 4098 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4099 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4100 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4101 | case tok::kw_float: |
| 4102 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4103 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4104 | case tok::kw__Bool: |
| 4105 | case tok::kw__Decimal32: |
| 4106 | case tok::kw__Decimal64: |
| 4107 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4108 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4109 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 4110 | // OpenCL specific types: |
| 4111 | case tok::kw_image1d_t: |
| 4112 | case tok::kw_image1d_array_t: |
| 4113 | case tok::kw_image1d_buffer_t: |
| 4114 | case tok::kw_image2d_t: |
| 4115 | case tok::kw_image2d_array_t: |
| 4116 | case tok::kw_image3d_t: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4117 | case tok::kw_sampler_t: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4118 | case tok::kw_event_t: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 4119 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4120 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4121 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4122 | case tok::kw_struct: |
| 4123 | case tok::kw_union: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4124 | case tok::kw___interface: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4125 | // enum-specifier |
| 4126 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4127 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4128 | // type-qualifier |
| 4129 | case tok::kw_const: |
| 4130 | case tok::kw_volatile: |
| 4131 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4132 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4133 | // function-specifier |
| 4134 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4135 | case tok::kw_virtual: |
| 4136 | case tok::kw_explicit: |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4137 | case tok::kw__Noreturn: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4138 | |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 4139 | // alignment-specifier |
| 4140 | case tok::kw__Alignas: |
| 4141 | |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4142 | // friend keyword. |
| 4143 | case tok::kw_friend: |
| 4144 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 4145 | // static_assert-declaration |
| 4146 | case tok::kw__Static_assert: |
| 4147 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4148 | // GNU typeof support. |
| 4149 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4150 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4151 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4152 | case tok::kw___attribute: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4153 | |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4154 | // C++11 decltype and constexpr. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4155 | case tok::annot_decltype: |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4156 | case tok::kw_constexpr: |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 4157 | |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4158 | // C11 _Atomic |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4159 | case tok::kw__Atomic: |
| 4160 | return true; |
| 4161 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 4162 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4163 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4164 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4165 | |
Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 4166 | // typedef-name |
| 4167 | case tok::annot_typename: |
| 4168 | return !DisambiguatingWithExpression || |
| 4169 | !isStartOfObjCClassMessageMissingOpenBracket(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4170 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 4171 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4172 | case tok::kw___cdecl: |
| 4173 | case tok::kw___stdcall: |
| 4174 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4175 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4176 | case tok::kw___w64: |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4177 | case tok::kw___sptr: |
| 4178 | case tok::kw___uptr: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4179 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4180 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4181 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4182 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4183 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4184 | |
| 4185 | case tok::kw___private: |
| 4186 | case tok::kw___local: |
| 4187 | case tok::kw___global: |
| 4188 | case tok::kw___constant: |
| 4189 | case tok::kw___read_only: |
| 4190 | case tok::kw___read_write: |
| 4191 | case tok::kw___write_only: |
| 4192 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4193 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4194 | } |
| 4195 | } |
| 4196 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4197 | bool Parser::isConstructorDeclarator() { |
| 4198 | TentativeParsingAction TPA(*this); |
| 4199 | |
| 4200 | // Parse the C++ scope specifier. |
| 4201 | CXXScopeSpec SS; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4202 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4203 | /*EnteringContext=*/true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4204 | TPA.Revert(); |
| 4205 | return false; |
| 4206 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4207 | |
| 4208 | // Parse the constructor name. |
| 4209 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 4210 | // We already know that we have a constructor name; just consume |
| 4211 | // the token. |
| 4212 | ConsumeToken(); |
| 4213 | } else { |
| 4214 | TPA.Revert(); |
| 4215 | return false; |
| 4216 | } |
| 4217 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4218 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4219 | if (Tok.isNot(tok::l_paren)) { |
| 4220 | TPA.Revert(); |
| 4221 | return false; |
| 4222 | } |
| 4223 | ConsumeParen(); |
| 4224 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4225 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 4226 | // that we have a constructor. |
| 4227 | if (Tok.is(tok::r_paren) || |
| 4228 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4229 | TPA.Revert(); |
| 4230 | return true; |
| 4231 | } |
| 4232 | |
| 4233 | // If we need to, enter the specified scope. |
| 4234 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4235 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4236 | DeclScopeObj.EnterDeclaratorScope(); |
| 4237 | |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4238 | // Optionally skip Microsoft attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4239 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4240 | MaybeParseMicrosoftAttributes(Attrs); |
| 4241 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4242 | // Check whether the next token(s) are part of a declaration |
| 4243 | // specifier, in which case we have the start of a parameter and, |
| 4244 | // therefore, we know that this is a constructor. |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4245 | bool IsConstructor = false; |
| 4246 | if (isDeclarationSpecifier()) |
| 4247 | IsConstructor = true; |
| 4248 | else if (Tok.is(tok::identifier) || |
| 4249 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 4250 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 4251 | // This might be a parenthesized member name, but is more likely to |
| 4252 | // be a constructor declaration with an invalid argument type. Keep |
| 4253 | // looking. |
| 4254 | if (Tok.is(tok::annot_cxxscope)) |
| 4255 | ConsumeToken(); |
| 4256 | ConsumeToken(); |
| 4257 | |
| 4258 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4259 | // which must have one of the following syntactic forms (see the |
| 4260 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4261 | switch (Tok.getKind()) { |
| 4262 | case tok::l_paren: |
| 4263 | // C(X ( int)); |
| 4264 | case tok::l_square: |
| 4265 | // C(X [ 5]); |
| 4266 | // C(X [ [attribute]]); |
| 4267 | case tok::coloncolon: |
| 4268 | // C(X :: Y); |
| 4269 | // C(X :: *p); |
| 4270 | case tok::r_paren: |
| 4271 | // C(X ) |
| 4272 | // Assume this isn't a constructor, rather than assuming it's a |
| 4273 | // constructor with an unnamed parameter of an ill-formed type. |
| 4274 | break; |
| 4275 | |
| 4276 | default: |
| 4277 | IsConstructor = true; |
| 4278 | break; |
| 4279 | } |
| 4280 | } |
| 4281 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4282 | TPA.Revert(); |
| 4283 | return IsConstructor; |
| 4284 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4285 | |
| 4286 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4287 | /// type-qualifier-list: [C99 6.7.5] |
| 4288 | /// type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4289 | /// [vendor] attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4290 | /// [ only if VendorAttributesAllowed=true ] |
| 4291 | /// type-qualifier-list type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4292 | /// [vendor] type-qualifier-list attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4293 | /// [ only if VendorAttributesAllowed=true ] |
| 4294 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4295 | /// [ only if CXX11AttributesAllowed=true ] |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4296 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4297 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4298 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 4299 | bool VendorAttributesAllowed, |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4300 | bool CXX11AttributesAllowed, |
| 4301 | bool AtomicAllowed) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4302 | if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed && |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4303 | isCXX11AttributeSpecifier()) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4304 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4305 | ParseCXX11Attributes(attrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4306 | DS.takeAttributesFrom(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4307 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4308 | |
| 4309 | SourceLocation EndLoc; |
| 4310 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4311 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4312 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4313 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4314 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4315 | SourceLocation Loc = Tok.getLocation(); |
| 4316 | |
| 4317 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4318 | case tok::code_completion: |
| 4319 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 4320 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4321 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4322 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4323 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4324 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4325 | break; |
| 4326 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4327 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, 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_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4331 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, 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; |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4334 | case tok::kw__Atomic: |
| 4335 | if (!AtomicAllowed) |
| 4336 | goto DoneWithTypeQuals; |
| 4337 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 4338 | getLangOpts()); |
| 4339 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4340 | |
| 4341 | // OpenCL qualifiers: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4342 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4343 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4344 | goto DoneWithTypeQuals; |
| 4345 | case tok::kw___private: |
| 4346 | case tok::kw___global: |
| 4347 | case tok::kw___local: |
| 4348 | case tok::kw___constant: |
| 4349 | case tok::kw___read_only: |
| 4350 | case tok::kw___write_only: |
| 4351 | case tok::kw___read_write: |
| 4352 | ParseOpenCLQualifiers(DS); |
| 4353 | break; |
| 4354 | |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4355 | case tok::kw___sptr: |
| 4356 | case tok::kw___uptr: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4357 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 4358 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4359 | case tok::kw___ptr32: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4360 | case tok::kw___cdecl: |
| 4361 | case tok::kw___stdcall: |
| 4362 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4363 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4364 | case tok::kw___unaligned: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4365 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4366 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4367 | continue; |
| 4368 | } |
| 4369 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4370 | case tok::kw___pascal: |
| 4371 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4372 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4373 | continue; |
| 4374 | } |
| 4375 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4376 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4377 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4378 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4379 | continue; // do *not* consume the next token! |
| 4380 | } |
| 4381 | // otherwise, FALL THROUGH! |
| 4382 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4383 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4384 | // If this is not a type-qualifier token, we're done reading type |
| 4385 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 4386 | DS.Finish(Diags, PP); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4387 | if (EndLoc.isValid()) |
| 4388 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4389 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4390 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4391 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4392 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 4393 | if (isInvalid) { |
| 4394 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4395 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4396 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4397 | EndLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4398 | } |
| 4399 | } |
| 4400 | |
| 4401 | |
| 4402 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 4403 | /// |
| 4404 | void Parser::ParseDeclarator(Declarator &D) { |
| 4405 | /// This implements the 'declarator' production in the C grammar, then checks |
| 4406 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4407 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4408 | } |
| 4409 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4410 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) { |
| 4411 | if (Kind == tok::star || Kind == tok::caret) |
| 4412 | return true; |
| 4413 | |
| 4414 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 4415 | if (!Lang.CPlusPlus) |
| 4416 | return false; |
| 4417 | |
| 4418 | return Kind == tok::amp || Kind == tok::ampamp; |
| 4419 | } |
| 4420 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4421 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 4422 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 4423 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4424 | /// ptr-operator production. |
| 4425 | /// |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4426 | /// 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] | 4427 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 4428 | /// isConstructorDeclarator. |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4429 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4430 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 4431 | /// [C] pointer[opt] direct-declarator |
| 4432 | /// [C++] direct-declarator |
| 4433 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4434 | /// |
| 4435 | /// pointer: [C99 6.7.5] |
| 4436 | /// '*' type-qualifier-list[opt] |
| 4437 | /// '*' type-qualifier-list[opt] pointer |
| 4438 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4439 | /// ptr-operator: |
| 4440 | /// '*' cv-qualifier-seq[opt] |
| 4441 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4442 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4443 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4444 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4445 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4446 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 4447 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 4448 | if (Diags.hasAllExtensionsSilenced()) |
| 4449 | D.setExtension(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4450 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4451 | // C++ member pointers start with a '::' or a nested-name. |
| 4452 | // Member pointers get special handling, since there's no place for the |
| 4453 | // scope spec in the generic path below. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4454 | if (getLangOpts().CPlusPlus && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 4455 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 4456 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4457 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4458 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4459 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4460 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4461 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 4462 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4463 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4464 | // The scope spec really belongs to the direct-declarator. |
Richard Smith | 6a502c4 | 2013-01-08 22:43:49 +0000 | [diff] [blame] | 4465 | if (D.mayHaveIdentifier()) |
| 4466 | D.getCXXScopeSpec() = SS; |
| 4467 | else |
| 4468 | AnnotateScopeToken(SS, true); |
| 4469 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4470 | if (DirectDeclParser) |
| 4471 | (this->*DirectDeclParser)(D); |
| 4472 | return; |
| 4473 | } |
| 4474 | |
| 4475 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4476 | D.SetRangeEnd(Loc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4477 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4478 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4479 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4480 | |
| 4481 | // Recurse to parse whatever is left. |
| 4482 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 4483 | |
| 4484 | // Sema will have to catch (syntactically invalid) pointers into global |
| 4485 | // scope. It has to catch pointers into namespace scope anyway. |
| 4486 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4487 | Loc), |
| 4488 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4489 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4490 | return; |
| 4491 | } |
| 4492 | } |
| 4493 | |
| 4494 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4495 | // Not a pointer, C++ reference, or block. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4496 | if (!isPtrOperatorToken(Kind, getLangOpts())) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4497 | if (DirectDeclParser) |
| 4498 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4499 | return; |
| 4500 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4501 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4502 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 4503 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4504 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4505 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4506 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 4507 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4508 | // Is a pointer. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4509 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4510 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4511 | // FIXME: GNU attributes are not allowed here in a new-type-id. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4512 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4513 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4514 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4515 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4516 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4517 | if (Kind == tok::star) |
| 4518 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 4519 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 4520 | DS.getConstSpecLoc(), |
| 4521 | DS.getVolatileSpecLoc(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4522 | DS.getRestrictSpecLoc()), |
| 4523 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4524 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4525 | else |
| 4526 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4527 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4528 | Loc), |
| 4529 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4530 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4531 | } else { |
| 4532 | // Is a reference |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4533 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4534 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4535 | // Complain about rvalue references in C++03, but then go on and build |
| 4536 | // the declarator. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4537 | if (Kind == tok::ampamp) |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4538 | Diag(Loc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4539 | diag::warn_cxx98_compat_rvalue_reference : |
| 4540 | diag::ext_rvalue_reference); |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4541 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4542 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 4543 | ParseTypeQualifierListOpt(DS); |
| 4544 | D.ExtendWithDeclSpec(DS); |
| 4545 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4546 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 4547 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 4548 | // template type argument, in which case the cv-qualifiers are ignored. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4549 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 4550 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 4551 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4552 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4553 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 4554 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4555 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4556 | // 'restrict' is permitted as an extension. |
| 4557 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 4558 | Diag(DS.getAtomicSpecLoc(), |
| 4559 | diag::err_invalid_reference_qualifier_application) << "_Atomic"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4560 | } |
| 4561 | |
| 4562 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4563 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4564 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4565 | if (D.getNumTypeObjects() > 0) { |
| 4566 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 4567 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 4568 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 4569 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 4570 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4571 | << II; |
| 4572 | else |
| 4573 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4574 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4575 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4576 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4577 | // can go ahead and build the (technically ill-formed) |
| 4578 | // declarator: reference collapsing will take care of it. |
| 4579 | } |
| 4580 | } |
| 4581 | |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4582 | // Remember that we parsed a reference type. |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4583 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4584 | Kind == tok::amp), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4585 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4586 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4587 | } |
| 4588 | } |
| 4589 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4590 | static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D, |
| 4591 | SourceLocation EllipsisLoc) { |
| 4592 | if (EllipsisLoc.isValid()) { |
| 4593 | FixItHint Insertion; |
| 4594 | if (!D.getEllipsisLoc().isValid()) { |
| 4595 | Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "..."); |
| 4596 | D.setEllipsisLoc(EllipsisLoc); |
| 4597 | } |
| 4598 | P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) |
| 4599 | << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName(); |
| 4600 | } |
| 4601 | } |
| 4602 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4603 | /// ParseDirectDeclarator |
| 4604 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4605 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4606 | /// '(' declarator ')' |
| 4607 | /// [GNU] '(' attributes declarator ')' |
| 4608 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4609 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4610 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4611 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4612 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4613 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 4614 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4615 | /// direct-declarator '(' parameter-type-list ')' |
| 4616 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4617 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4618 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 4619 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 4620 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4621 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 4622 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 4623 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4624 | /// [C++] declarator-id |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4625 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4626 | /// |
| 4627 | /// declarator-id: [C++ 8] |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4628 | /// '...'[opt] id-expression |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4629 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 4630 | /// |
| 4631 | /// id-expression: [C++ 5.1] |
| 4632 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4633 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4634 | /// |
| 4635 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4636 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4637 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4638 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4639 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 4640 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 4641 | /// |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4642 | /// Note, any additional constructs added here may need corresponding changes |
| 4643 | /// in isConstructorDeclarator. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4644 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4645 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4646 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4647 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4648 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4649 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4650 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4651 | D.getContext() == Declarator::MemberContext; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4652 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4653 | EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4654 | } |
| 4655 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4656 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4657 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 4658 | // Change the declaration context for name lookup, until this function |
| 4659 | // is exited (and the declarator has been parsed). |
| 4660 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4661 | } |
| 4662 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4663 | // C++0x [dcl.fct]p14: |
| 4664 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4665 | // of a parameter-declaration-clause without a preceding comma. In |
| 4666 | // this case, the ellipsis is parsed as part of the |
| 4667 | // abstract-declarator if the type of the parameter names a template |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4668 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 4669 | // as part of the parameter-declaration-clause. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4670 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4671 | !((D.getContext() == Declarator::PrototypeContext || |
| 4672 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4673 | NextToken().is(tok::r_paren) && |
Richard Smith | 30f2a74 | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4674 | !D.hasGroupingParens() && |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4675 | !Actions.containsUnexpandedParameterPacks(D))) { |
| 4676 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 4677 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) { |
| 4678 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 4679 | // the user what they should have done. |
| 4680 | ParseDeclarator(D); |
| 4681 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4682 | return; |
| 4683 | } else |
| 4684 | D.setEllipsisLoc(EllipsisLoc); |
| 4685 | |
| 4686 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 4687 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 4688 | // the l_paren token. |
| 4689 | } |
| 4690 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4691 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 4692 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 4693 | // We found something that indicates the start of an unqualified-id. |
| 4694 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4695 | bool AllowConstructorName; |
| 4696 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 4697 | AllowConstructorName = false; |
| 4698 | else if (D.getCXXScopeSpec().isSet()) |
| 4699 | AllowConstructorName = |
| 4700 | (D.getContext() == Declarator::FileContext || |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 4701 | D.getContext() == Declarator::MemberContext); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4702 | else |
| 4703 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 4704 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4705 | SourceLocation TemplateKWLoc; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4706 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 4707 | /*EnteringContext=*/true, |
| 4708 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4709 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4710 | ParsedType(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4711 | TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4712 | D.getName()) || |
| 4713 | // Once we're past the identifier, if the scope was bad, mark the |
| 4714 | // whole declarator bad. |
| 4715 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4716 | D.SetIdentifier(0, Tok.getLocation()); |
| 4717 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4718 | } else { |
| 4719 | // Parsed the unqualified-id; update range information and move along. |
| 4720 | if (D.getSourceRange().getBegin().isInvalid()) |
| 4721 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 4722 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4723 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4724 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4725 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4726 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4727 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4728 | "There's a C++-specific check for tok::identifier above"); |
| 4729 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 4730 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 4731 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4732 | goto PastIdentifier; |
Richard Smith | a38253c | 2013-07-11 05:10:21 +0000 | [diff] [blame] | 4733 | } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) { |
| 4734 | Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id) |
| 4735 | << FixItHint::CreateRemoval(Tok.getLocation()); |
| 4736 | D.SetIdentifier(0, Tok.getLocation()); |
| 4737 | ConsumeToken(); |
| 4738 | goto PastIdentifier; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4739 | } |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4740 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4741 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4742 | // direct-declarator: '(' declarator ')' |
| 4743 | // direct-declarator: '(' attributes declarator ')' |
| 4744 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 4745 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4746 | |
| 4747 | // If the declarator was parenthesized, we entered the declarator |
| 4748 | // scope when parsing the parenthesized declarator, then exited |
| 4749 | // the scope already. Re-enter the scope, if we need to. |
| 4750 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4751 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4752 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4753 | if (!D.isInvalidType() && |
| 4754 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4755 | // Change the declaration context for name lookup, until this function |
| 4756 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4757 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4758 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4759 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4760 | // This could be something simple like "int" (in which case the declarator |
| 4761 | // portion is empty), if an abstract-declarator is allowed. |
| 4762 | D.SetIdentifier(0, Tok.getLocation()); |
Richard Smith | 30f2a74 | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4763 | |
| 4764 | // The grammar for abstract-pack-declarator does not allow grouping parens. |
| 4765 | // FIXME: Revisit this once core issue 1488 is resolved. |
| 4766 | if (D.hasEllipsis() && D.hasGroupingParens()) |
| 4767 | Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()), |
| 4768 | diag::ext_abstract_pack_declarator_parens); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4769 | } else { |
David Blaikie | e75d9cf | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 4770 | if (Tok.getKind() == tok::annot_pragma_parser_crash) |
David Blaikie | 377da4c | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 4771 | LLVM_BUILTIN_TRAP; |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 4772 | if (D.getContext() == Declarator::MemberContext) |
| 4773 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 4774 | << D.getDeclSpec().getSourceRange(); |
Richard Trieu | db55c04c | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 4775 | else if (getLangOpts().CPlusPlus) { |
| 4776 | if (Tok.is(tok::period) || Tok.is(tok::arrow)) |
| 4777 | Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow); |
| 4778 | else |
| 4779 | Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus; |
| 4780 | } else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4781 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4782 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 4783 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4784 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4785 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4786 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4787 | assert(D.isPastIdentifier() && |
| 4788 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4789 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4790 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 4791 | if (D.hasName() && !D.getNumTypeObjects()) |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4792 | MaybeParseCXX11Attributes(D); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4793 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4794 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4795 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4796 | // Enter function-declaration scope, limiting any declarators to the |
| 4797 | // function prototype scope, including parameter declarators. |
| 4798 | ParseScope PrototypeScope(this, |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4799 | Scope::FunctionPrototypeScope|Scope::DeclScope| |
| 4800 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 4801 | ? Scope::FunctionDeclarationScope : 0)); |
| 4802 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4803 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 4804 | // In such a case, check if we actually have a function declarator; if it |
| 4805 | // is not, the declarator has been fully parsed. |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4806 | bool IsAmbiguous = false; |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 4807 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 4808 | // The name of the declarator, if any, is tentatively declared within |
| 4809 | // a possible direct initializer. |
| 4810 | TentativelyDeclaredIdentifiers.push_back(D.getIdentifier()); |
| 4811 | bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous); |
| 4812 | TentativelyDeclaredIdentifiers.pop_back(); |
| 4813 | if (!IsFunctionDecl) |
| 4814 | break; |
| 4815 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4816 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4817 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4818 | T.consumeOpen(); |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4819 | ParseFunctionDeclarator(D, attrs, T, IsAmbiguous); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4820 | PrototypeScope.Exit(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4821 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4822 | ParseBracketDeclarator(D); |
| 4823 | } else { |
| 4824 | break; |
| 4825 | } |
| 4826 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4827 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4828 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4829 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 4830 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4831 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4832 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 4833 | /// |
| 4834 | /// direct-declarator: |
| 4835 | /// '(' declarator ')' |
| 4836 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4837 | /// direct-declarator '(' parameter-type-list ')' |
| 4838 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4839 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4840 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4841 | /// |
| 4842 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4843 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4844 | T.consumeOpen(); |
| 4845 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4846 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4847 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4848 | // Eat any attributes before we look at whether this is a grouping or function |
| 4849 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 4850 | // the type being built up, for example: |
| 4851 | // int (__attribute__(()) *x)(long y) |
| 4852 | // If this ends up not being a grouping paren, the attribute applies to the |
| 4853 | // first argument, for example: |
| 4854 | // int (__attribute__(()) int x) |
| 4855 | // In either case, we need to eat any attributes to be able to determine what |
| 4856 | // sort of paren this is. |
| 4857 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4858 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4859 | bool RequiresArg = false; |
| 4860 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4861 | ParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4862 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4863 | // We require that the argument list (if this is a non-grouping paren) be |
| 4864 | // present even if the attribute list was empty. |
| 4865 | RequiresArg = true; |
| 4866 | } |
Chad Rosier | 9cab1c9 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 4867 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4868 | // Eat any Microsoft extensions. |
Chad Rosier | 9cab1c9 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 4869 | ParseMicrosoftTypeAttributes(attrs); |
| 4870 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4871 | // Eat any Borland extensions. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 4872 | if (Tok.is(tok::kw___pascal)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4873 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4874 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4875 | // If we haven't past the identifier yet (or where the identifier would be |
| 4876 | // stored, if this is an abstract declarator), then this is probably just |
| 4877 | // grouping parens. However, if this could be an abstract-declarator, then |
| 4878 | // this could also be the start of function arguments (consider 'void()'). |
| 4879 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4880 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4881 | if (!D.mayOmitIdentifier()) { |
| 4882 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 4883 | // paren, because we haven't seen the identifier yet. |
| 4884 | isGrouping = true; |
| 4885 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4886 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 4887 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4888 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 4889 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4890 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 4891 | // considered to be a type, not a K&R identifier-list. |
| 4892 | isGrouping = false; |
| 4893 | } else { |
| 4894 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 4895 | isGrouping = true; |
| 4896 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4897 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4898 | // If this is a grouping paren, handle: |
| 4899 | // direct-declarator: '(' declarator ')' |
| 4900 | // direct-declarator: '(' attributes declarator ')' |
| 4901 | if (isGrouping) { |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4902 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 4903 | D.setEllipsisLoc(SourceLocation()); |
| 4904 | |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4905 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4906 | D.setGroupingParens(true); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4907 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4908 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4909 | T.consumeClose(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4910 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4911 | T.getCloseLocation()), |
| 4912 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4913 | |
| 4914 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4915 | |
| 4916 | // An ellipsis cannot be placed outside parentheses. |
| 4917 | if (EllipsisLoc.isValid()) |
| 4918 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4919 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4920 | return; |
| 4921 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4922 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4923 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 4924 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4925 | // identifier (and remember where it would have been), then call into |
| 4926 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4927 | D.SetIdentifier(0, Tok.getLocation()); |
| 4928 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4929 | // Enter function-declaration scope, limiting any declarators to the |
| 4930 | // function prototype scope, including parameter declarators. |
| 4931 | ParseScope PrototypeScope(this, |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4932 | Scope::FunctionPrototypeScope | Scope::DeclScope | |
| 4933 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 4934 | ? Scope::FunctionDeclarationScope : 0)); |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4935 | ParseFunctionDeclarator(D, attrs, T, false, RequiresArg); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4936 | PrototypeScope.Exit(); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4937 | } |
| 4938 | |
| 4939 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 4940 | /// declarator D up to a paren, which indicates that we are parsing function |
| 4941 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4942 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4943 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 4944 | /// immediately after the open paren - they should be considered to be the |
| 4945 | /// first argument of a parameter. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4946 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4947 | /// If RequiresArg is true, then the first argument of the function is required |
| 4948 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4949 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4950 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 4951 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 4952 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 4953 | /// |
| 4954 | /// [C++11] exception-specification: |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4955 | /// dynamic-exception-specification |
| 4956 | /// noexcept-specification |
| 4957 | /// |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4958 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4959 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4960 | BalancedDelimiterTracker &Tracker, |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4961 | bool IsAmbiguous, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4962 | bool RequiresArg) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4963 | assert(getCurScope()->isFunctionPrototypeScope() && |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4964 | "Should call from a Function scope"); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4965 | // lparen is already consumed! |
| 4966 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 4967 | |
| 4968 | // This should be true when the function has typed arguments. |
| 4969 | // Otherwise, it is treated as a K&R-style function. |
| 4970 | bool HasProto = false; |
| 4971 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4972 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4973 | // Remember where we see an ellipsis, if any. |
| 4974 | SourceLocation EllipsisLoc; |
| 4975 | |
| 4976 | DeclSpec DS(AttrFactory); |
| 4977 | bool RefQualifierIsLValueRef = true; |
| 4978 | SourceLocation RefQualifierLoc; |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4979 | SourceLocation ConstQualifierLoc; |
| 4980 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4981 | ExceptionSpecificationType ESpecType = EST_None; |
| 4982 | SourceRange ESpecRange; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4983 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 4984 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4985 | ExprResult NoexceptExpr; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4986 | ParsedAttributes FnAttrs(AttrFactory); |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 4987 | TypeResult TrailingReturnType; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4988 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4989 | Actions.ActOnStartFunctionDeclarator(); |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 4990 | |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4991 | /* LocalEndLoc is the end location for the local FunctionTypeLoc. |
| 4992 | EndLoc is the end location for the function declarator. |
| 4993 | They differ for trailing return types. */ |
| 4994 | SourceLocation StartLoc, LocalEndLoc, EndLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4995 | SourceLocation LParenLoc, RParenLoc; |
| 4996 | LParenLoc = Tracker.getOpenLocation(); |
| 4997 | StartLoc = LParenLoc; |
| 4998 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4999 | if (isFunctionDeclaratorIdentifierList()) { |
| 5000 | if (RequiresArg) |
| 5001 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5002 | |
| 5003 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 5004 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5005 | Tracker.consumeClose(); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5006 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5007 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5008 | EndLoc = RParenLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5009 | } else { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5010 | if (Tok.isNot(tok::r_paren)) |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 5011 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5012 | else if (RequiresArg) |
| 5013 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 5014 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5015 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5016 | |
| 5017 | // If we have the closing ')', eat it. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5018 | Tracker.consumeClose(); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5019 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5020 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5021 | EndLoc = RParenLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5022 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5023 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5024 | // FIXME: Accept these components in any order, and produce fixits to |
| 5025 | // correct the order if the user gets it wrong. Ideally we should deal |
| 5026 | // with the virt-specifier-seq and pure-specifier in the same way. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5027 | |
| 5028 | // Parse cv-qualifier-seq[opt]. |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5029 | ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false, |
| 5030 | /*CXX11AttributesAllowed*/ false, |
| 5031 | /*AtomicAllowed*/ false); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5032 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 5033 | EndLoc = DS.getSourceRange().getEnd(); |
| 5034 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 5035 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 5036 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5037 | |
| 5038 | // Parse ref-qualifier[opt]. |
| 5039 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5040 | Diag(Tok, getLangOpts().CPlusPlus11 ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5041 | diag::warn_cxx98_compat_ref_qualifier : |
| 5042 | diag::ext_ref_qualifier); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5043 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5044 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 5045 | RefQualifierLoc = ConsumeToken(); |
| 5046 | EndLoc = RefQualifierLoc; |
| 5047 | } |
| 5048 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5049 | // C++11 [expr.prim.general]p3: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5050 | // If a declaration declares a member function or member function |
| 5051 | // 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] | 5052 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5053 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5054 | // declarator. |
Richard Smith | d922779 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5055 | // FIXME: currently, "static" case isn't handled correctly. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5056 | bool IsCXX11MemberFunction = |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5057 | getLangOpts().CPlusPlus11 && |
Richard Smith | d922779 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5058 | (D.getContext() == Declarator::MemberContext |
| 5059 | ? !D.getDeclSpec().isFriendSpecified() |
| 5060 | : D.getContext() == Declarator::FileContext && |
| 5061 | D.getCXXScopeSpec().isValid() && |
| 5062 | Actions.CurContext->isRecord()); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5063 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 5064 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
Richard Smith | 7b19cb1 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 5065 | DS.getTypeQualifiers() | |
Richard Smith | 8404626 | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 5066 | (D.getDeclSpec().isConstexprSpecified() && |
| 5067 | !getLangOpts().CPlusPlus1y |
Richard Smith | 7b19cb1 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 5068 | ? Qualifiers::Const : 0), |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5069 | IsCXX11MemberFunction); |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5070 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5071 | // Parse exception-specification[opt]. |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5072 | ESpecType = tryParseExceptionSpecification(ESpecRange, |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 5073 | DynamicExceptions, |
| 5074 | DynamicExceptionRanges, |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5075 | NoexceptExpr); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5076 | if (ESpecType != EST_None) |
| 5077 | EndLoc = ESpecRange.getEnd(); |
| 5078 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5079 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 5080 | // after the exception-specification. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5081 | MaybeParseCXX11Attributes(FnAttrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5082 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5083 | // Parse trailing-return-type[opt]. |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5084 | LocalEndLoc = EndLoc; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5085 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5086 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5087 | if (D.getDeclSpec().getTypeSpecType() == TST_auto) |
| 5088 | StartLoc = D.getDeclSpec().getTypeSpecTypeLoc(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5089 | LocalEndLoc = Tok.getLocation(); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 5090 | SourceRange Range; |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 5091 | TrailingReturnType = ParseTrailingReturnType(Range); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5092 | EndLoc = Range.getEnd(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5093 | } |
| 5094 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5095 | } |
| 5096 | |
| 5097 | // Remember that we parsed a function type, and remember the attributes. |
| 5098 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5099 | IsAmbiguous, |
| 5100 | LParenLoc, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5101 | ParamInfo.data(), ParamInfo.size(), |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5102 | EllipsisLoc, RParenLoc, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5103 | DS.getTypeQualifiers(), |
| 5104 | RefQualifierIsLValueRef, |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 5105 | RefQualifierLoc, ConstQualifierLoc, |
| 5106 | VolatileQualifierLoc, |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 5107 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5108 | ESpecType, ESpecRange.getBegin(), |
| 5109 | DynamicExceptions.data(), |
| 5110 | DynamicExceptionRanges.data(), |
| 5111 | DynamicExceptions.size(), |
| 5112 | NoexceptExpr.isUsable() ? |
| 5113 | NoexceptExpr.get() : 0, |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5114 | StartLoc, LocalEndLoc, D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5115 | TrailingReturnType), |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5116 | FnAttrs, EndLoc); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 5117 | |
| 5118 | Actions.ActOnEndFunctionDeclarator(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5119 | } |
| 5120 | |
| 5121 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 5122 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 5123 | /// |
| 5124 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 5125 | /// abstract-declarators. |
| 5126 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5127 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5128 | && Tok.is(tok::identifier) |
| 5129 | && !TryAltiVecVectorToken() |
| 5130 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 5131 | // 6.7.5.3p11. |
| 5132 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 5133 | // Identifier lists follow a really simple grammar: the identifiers can |
| 5134 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 5135 | // identifier lists are really rare in the brave new modern world, and |
| 5136 | // it is very common for someone to typo a type in a non-K&R style |
| 5137 | // list. If we are presented with something like: "void foo(intptr x, |
| 5138 | // float y)", we don't want to start parsing the function declarator as |
| 5139 | // though it is a K&R style declarator just because intptr is an |
| 5140 | // invalid type. |
| 5141 | // |
| 5142 | // To handle this, we check to see if the token after the first |
| 5143 | // identifier is a "," or ")". Only then do we parse it as an |
| 5144 | // identifier list. |
| 5145 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 5146 | } |
| 5147 | |
| 5148 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 5149 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 5150 | /// |
| 5151 | /// After returning, ParamInfo will hold the parsed parameters. |
| 5152 | /// |
| 5153 | /// identifier-list: [C99 6.7.5] |
| 5154 | /// identifier |
| 5155 | /// identifier-list ',' identifier |
| 5156 | /// |
| 5157 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 5158 | Declarator &D, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5159 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5160 | // If there was no identifier specified for the declarator, either we are in |
| 5161 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 5162 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 5163 | // diagnose this. |
| 5164 | if (!D.getIdentifier()) |
| 5165 | Diag(Tok, diag::ext_ident_list_in_param); |
| 5166 | |
| 5167 | // Maintain an efficient lookup of params we have seen so far. |
| 5168 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 5169 | |
| 5170 | while (1) { |
| 5171 | // If this isn't an identifier, report the error and skip until ')'. |
| 5172 | if (Tok.isNot(tok::identifier)) { |
| 5173 | Diag(Tok, diag::err_expected_ident); |
| 5174 | SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 5175 | // Forget we parsed anything. |
| 5176 | ParamInfo.clear(); |
| 5177 | return; |
| 5178 | } |
| 5179 | |
| 5180 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 5181 | |
| 5182 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 5183 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 5184 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 5185 | |
| 5186 | // Verify that the argument identifier has not already been mentioned. |
| 5187 | if (!ParamsSoFar.insert(ParmII)) { |
| 5188 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 5189 | } else { |
| 5190 | // Remember this identifier in ParamInfo. |
| 5191 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 5192 | Tok.getLocation(), |
| 5193 | 0)); |
| 5194 | } |
| 5195 | |
| 5196 | // Eat the identifier. |
| 5197 | ConsumeToken(); |
| 5198 | |
| 5199 | // The list continues if we see a comma. |
| 5200 | if (Tok.isNot(tok::comma)) |
| 5201 | break; |
| 5202 | ConsumeToken(); |
| 5203 | } |
| 5204 | } |
| 5205 | |
| 5206 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 5207 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 5208 | /// identifier list. |
| 5209 | /// |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5210 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 5211 | /// caller parsed those arguments immediately after the open paren - they should |
| 5212 | /// be considered to be part of the first parameter. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5213 | /// |
| 5214 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 5215 | /// be the location of the ellipsis, if any was parsed. |
| 5216 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5217 | /// parameter-type-list: [C99 6.7.5] |
| 5218 | /// parameter-list |
| 5219 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5220 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5221 | /// |
| 5222 | /// parameter-list: [C99 6.7.5] |
| 5223 | /// parameter-declaration |
| 5224 | /// parameter-list ',' parameter-declaration |
| 5225 | /// |
| 5226 | /// parameter-declaration: [C99 6.7.5] |
| 5227 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5228 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5229 | /// [C++11] initializer-clause |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5230 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 5231 | /// declaration-specifiers abstract-declarator[opt] |
| 5232 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 5233 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5234 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5235 | /// [C++11] attribute-specifier-seq parameter-declaration |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5236 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5237 | void Parser::ParseParameterDeclarationClause( |
| 5238 | Declarator &D, |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5239 | ParsedAttributes &FirstArgAttrs, |
Craig Topper | 6b9240e | 2013-07-05 19:34:19 +0000 | [diff] [blame] | 5240 | SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5241 | SourceLocation &EllipsisLoc) { |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 5242 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5243 | while (1) { |
| 5244 | if (Tok.is(tok::ellipsis)) { |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5245 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 5246 | // before deciding this was a parameter-declaration-clause. |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 5247 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5248 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5249 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5250 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5251 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5252 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5253 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5254 | |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5255 | // Parse any C++11 attributes. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5256 | MaybeParseCXX11Attributes(DS.getAttributes()); |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5257 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5258 | // Skip any Microsoft attributes before a param. |
Chad Rosier | 16f90bf | 2012-12-20 20:37:53 +0000 | [diff] [blame] | 5259 | MaybeParseMicrosoftAttributes(DS.getAttributes()); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5260 | |
| 5261 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5262 | |
| 5263 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5264 | // 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] | 5265 | // 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] | 5266 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 5267 | // too much hassle. |
| 5268 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5269 | |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 5270 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5271 | |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 5272 | // Parse the declarator. This is "PrototypeContext", because we must |
| 5273 | // accept either 'declarator' or 'abstract-declarator' here. |
| 5274 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 5275 | ParseDeclarator(ParmDecl); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5276 | |
| 5277 | // Parse GNU attributes, if present. |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 5278 | MaybeParseGNUAttributes(ParmDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5279 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5280 | // Remember this parsed parameter in ParamInfo. |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 5281 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5282 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5283 | // DefArgToks is used when the parsing of default arguments needs |
| 5284 | // to be delayed. |
| 5285 | CachedTokens *DefArgToks = 0; |
| 5286 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5287 | // If no parameter was specified, verify that *something* was specified, |
| 5288 | // otherwise we have a missing type and identifier. |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 5289 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 5290 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5291 | // Completely missing, emit error. |
| 5292 | Diag(DSStart, diag::err_missing_param); |
| 5293 | } else { |
| 5294 | // Otherwise, we have something. Add it and let semantic analysis try |
| 5295 | // 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] | 5296 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5297 | // Inform the actions module about the parameter declarator, so it gets |
| 5298 | // added to the current scope. |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 5299 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); |
| 5300 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5301 | // Parse the default argument, if any. We parse the default |
| 5302 | // arguments in all dialects; the semantic analysis in |
| 5303 | // ActOnParamDefaultArgument will reject the default argument in |
| 5304 | // C. |
| 5305 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5306 | SourceLocation EqualLoc = Tok.getLocation(); |
| 5307 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5308 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5309 | if (D.getContext() == Declarator::MemberContext) { |
| 5310 | // If we're inside a class definition, cache the tokens |
| 5311 | // corresponding to the default argument. We'll actually parse |
| 5312 | // them when we see the end of the class definition. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5313 | // FIXME: Can we use a smart pointer for Toks? |
| 5314 | DefArgToks = new CachedTokens; |
| 5315 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5316 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 5317 | /*StopAtSemi=*/true, |
| 5318 | /*ConsumeFinalToken=*/false)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5319 | delete DefArgToks; |
| 5320 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5321 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5322 | } else { |
| 5323 | // Mark the end of the default argument so that we know when to |
| 5324 | // stop when we parse it later on. |
| 5325 | Token DefArgEnd; |
| 5326 | DefArgEnd.startToken(); |
| 5327 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 5328 | DefArgEnd.setLocation(Tok.getLocation()); |
| 5329 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5330 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 5331 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5332 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5333 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5334 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5335 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5336 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5337 | // The argument isn't actually potentially evaluated unless it is |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5338 | // used. |
| 5339 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 5340 | Sema::PotentiallyEvaluatedIfUsed, |
| 5341 | Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5342 | |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5343 | ExprResult DefArgResult; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5344 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5345 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5346 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5347 | } else |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5348 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5349 | if (DefArgResult.isInvalid()) { |
| 5350 | Actions.ActOnParamDefaultArgumentError(Param); |
| 5351 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 5352 | } else { |
| 5353 | // Inform the actions module about the default argument |
| 5354 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5355 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5356 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5357 | } |
| 5358 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5359 | |
| 5360 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
Manuel Klimek | 152b4e4 | 2013-08-22 12:12:24 +0000 | [diff] [blame] | 5361 | ParmDecl.getIdentifierLoc(), Param, |
| 5362 | DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5363 | } |
| 5364 | |
| 5365 | // 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] | 5366 | if (Tok.isNot(tok::comma)) { |
| 5367 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5368 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5369 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5370 | if (!getLangOpts().CPlusPlus) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5371 | // We have ellipsis without a preceding ',', which is ill-formed |
| 5372 | // in C. Complain and provide the fix. |
| 5373 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5374 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5375 | } |
| 5376 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5377 | |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5378 | break; |
| 5379 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5380 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5381 | // Consume the comma. |
| 5382 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5383 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5384 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 5385 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5386 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5387 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 5388 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 5389 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 5390 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 5391 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5392 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 5393 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5394 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5395 | if (CheckProhibitedCXX11Attribute()) |
| 5396 | return; |
| 5397 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5398 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 5399 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5400 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5401 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 5402 | // This code does a fast path to handle some of the most obvious cases. |
| 5403 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5404 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5405 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5406 | MaybeParseCXX11Attributes(attrs); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5407 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5408 | // Remember that we parsed the empty array type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5409 | ExprResult NumElements; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5410 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5411 | T.getOpenLocation(), |
| 5412 | T.getCloseLocation()), |
| 5413 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5414 | return; |
| 5415 | } else if (Tok.getKind() == tok::numeric_constant && |
| 5416 | GetLookAheadToken(1).is(tok::r_square)) { |
| 5417 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5418 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5419 | ConsumeToken(); |
| 5420 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5421 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5422 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5423 | MaybeParseCXX11Attributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5424 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5425 | // Remember that we parsed a array type, and remember its features. |
Nikola Smiljanic | ebf0fa8 | 2013-01-11 08:33:05 +0000 | [diff] [blame] | 5426 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5427 | ExprRes.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5428 | T.getOpenLocation(), |
| 5429 | T.getCloseLocation()), |
| 5430 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5431 | return; |
| 5432 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5433 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5434 | // If valid, this location is the position where we read the 'static' keyword. |
| 5435 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5436 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5437 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5438 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5439 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5440 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5441 | DeclSpec DS(AttrFactory); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5442 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5443 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5444 | // If we haven't already read 'static', check to see if there is one after the |
| 5445 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5446 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5447 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5448 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5449 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 5450 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5451 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5452 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5453 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 5454 | // 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] | 5455 | // the token after the star is a ']'. Since stars in arrays are |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5456 | // infrequent, use of lookahead is not costly here. |
| 5457 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 5458 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5459 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5460 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5461 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5462 | StaticLoc = SourceLocation(); // Drop the static. |
| 5463 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5464 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5465 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5466 | // Note, in C89, this production uses the constant-expr production instead |
| 5467 | // of assignment-expr. The only difference is that assignment-expr allows |
| 5468 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 5469 | // 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] | 5470 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5471 | // Parse the constant-expression or assignment-expression now (depending |
| 5472 | // on dialect). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5473 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5474 | NumElements = ParseConstantExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5475 | } else { |
| 5476 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 5477 | Sema::ConstantEvaluated); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5478 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5479 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5480 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5481 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5482 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 5483 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 5484 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5485 | // If the expression was invalid, skip it. |
| 5486 | SkipUntil(tok::r_square); |
| 5487 | return; |
| 5488 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5489 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5490 | T.consumeClose(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5491 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5492 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5493 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5494 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5495 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5496 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5497 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5498 | NumElements.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5499 | T.getOpenLocation(), |
| 5500 | T.getCloseLocation()), |
| 5501 | attrs, T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5502 | } |
| 5503 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5504 | /// [GNU] typeof-specifier: |
| 5505 | /// typeof ( expressions ) |
| 5506 | /// typeof ( type-name ) |
| 5507 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5508 | /// |
| 5509 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5510 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5511 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5512 | SourceLocation StartLoc = ConsumeToken(); |
| 5513 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5514 | const bool hasParens = Tok.is(tok::l_paren); |
| 5515 | |
Eli Friedman | 80bfa3d | 2012-09-26 04:34:21 +0000 | [diff] [blame] | 5516 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated, |
| 5517 | Sema::ReuseLambdaContextDecl); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5518 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5519 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5520 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5521 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5522 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 5523 | CastTy, CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5524 | if (hasParens) |
| 5525 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5526 | |
| 5527 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5528 | // FIXME: Not accurate, the range gets one token more than it should. |
| 5529 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5530 | else |
| 5531 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5532 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5533 | if (isCastExpr) { |
| 5534 | if (!CastTy) { |
| 5535 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5536 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 5537 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5538 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5539 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5540 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5541 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5542 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5543 | DiagID, CastTy)) |
| 5544 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5545 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5546 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5547 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5548 | // If we get here, the operand to the typeof was an expresion. |
| 5549 | if (Operand.isInvalid()) { |
| 5550 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 5551 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5552 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5553 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5554 | // We might need to transform the operand if it is potentially evaluated. |
| 5555 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 5556 | if (Operand.isInvalid()) { |
| 5557 | DS.SetTypeSpecError(); |
| 5558 | return; |
| 5559 | } |
| 5560 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5561 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5562 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5563 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5564 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5565 | DiagID, Operand.get())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5566 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5567 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5568 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 5569 | /// [C11] atomic-specifier: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5570 | /// _Atomic ( type-name ) |
| 5571 | /// |
| 5572 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5573 | assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) && |
| 5574 | "Not an atomic specifier"); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5575 | |
| 5576 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5577 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5578 | if (T.consumeOpen()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5579 | return; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5580 | |
| 5581 | TypeResult Result = ParseTypeName(); |
| 5582 | if (Result.isInvalid()) { |
| 5583 | SkipUntil(tok::r_paren); |
| 5584 | return; |
| 5585 | } |
| 5586 | |
| 5587 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5588 | T.consumeClose(); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5589 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5590 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5591 | return; |
| 5592 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5593 | DS.setTypeofParensRange(T.getRange()); |
| 5594 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5595 | |
| 5596 | const char *PrevSpec = 0; |
| 5597 | unsigned DiagID; |
| 5598 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
| 5599 | DiagID, Result.release())) |
| 5600 | Diag(StartLoc, DiagID) << PrevSpec; |
| 5601 | } |
| 5602 | |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5603 | |
| 5604 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 5605 | /// from TryAltiVecVectorToken. |
| 5606 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 5607 | Token Next = NextToken(); |
| 5608 | switch (Next.getKind()) { |
| 5609 | default: return false; |
| 5610 | case tok::kw_short: |
| 5611 | case tok::kw_long: |
| 5612 | case tok::kw_signed: |
| 5613 | case tok::kw_unsigned: |
| 5614 | case tok::kw_void: |
| 5615 | case tok::kw_char: |
| 5616 | case tok::kw_int: |
| 5617 | case tok::kw_float: |
| 5618 | case tok::kw_double: |
| 5619 | case tok::kw_bool: |
| 5620 | case tok::kw___pixel: |
| 5621 | Tok.setKind(tok::kw___vector); |
| 5622 | return true; |
| 5623 | case tok::identifier: |
| 5624 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5625 | Tok.setKind(tok::kw___vector); |
| 5626 | return true; |
| 5627 | } |
Bill Schmidt | 3e3d20b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5628 | if (Next.getIdentifierInfo() == Ident_bool) { |
| 5629 | Tok.setKind(tok::kw___vector); |
| 5630 | return true; |
| 5631 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5632 | return false; |
| 5633 | } |
| 5634 | } |
| 5635 | |
| 5636 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 5637 | const char *&PrevSpec, unsigned &DiagID, |
| 5638 | bool &isInvalid) { |
| 5639 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 5640 | Token Next = NextToken(); |
| 5641 | switch (Next.getKind()) { |
| 5642 | case tok::kw_short: |
| 5643 | case tok::kw_long: |
| 5644 | case tok::kw_signed: |
| 5645 | case tok::kw_unsigned: |
| 5646 | case tok::kw_void: |
| 5647 | case tok::kw_char: |
| 5648 | case tok::kw_int: |
| 5649 | case tok::kw_float: |
| 5650 | case tok::kw_double: |
| 5651 | case tok::kw_bool: |
| 5652 | case tok::kw___pixel: |
| 5653 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5654 | return true; |
| 5655 | case tok::identifier: |
| 5656 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5657 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5658 | return true; |
| 5659 | } |
Bill Schmidt | 3e3d20b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5660 | if (Next.getIdentifierInfo() == Ident_bool) { |
| 5661 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5662 | return true; |
| 5663 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5664 | break; |
| 5665 | default: |
| 5666 | break; |
| 5667 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 5668 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5669 | DS.isTypeAltiVecVector()) { |
| 5670 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 5671 | return true; |
Bill Schmidt | 3e3d20b | 2013-07-03 20:54:09 +0000 | [diff] [blame] | 5672 | } else if ((Tok.getIdentifierInfo() == Ident_bool) && |
| 5673 | DS.isTypeAltiVecVector()) { |
| 5674 | isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID); |
| 5675 | return true; |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5676 | } |
| 5677 | return false; |
| 5678 | } |