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" |
Benjamin Kramer | 9852f58 | 2012-12-01 16:35:25 +0000 | [diff] [blame] | 16 | #include "clang/Basic/AddressSpaces.h" |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 17 | #include "clang/Basic/CharInfo.h" |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 18 | #include "clang/Basic/OpenCL.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 19 | #include "clang/Parse/ParseDiagnostic.h" |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 20 | #include "clang/Sema/Lookup.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 21 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 22 | #include "clang/Sema/PrettyDeclStackTrace.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 23 | #include "clang/Sema/Scope.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/SmallSet.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallString.h" |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringSwitch.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | // C99 6.7: Declarations. |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
| 33 | /// ParseTypeName |
| 34 | /// type-name: [C99 6.7.6] |
| 35 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 36 | /// |
| 37 | /// Called type-id in C++. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 38 | TypeResult Parser::ParseTypeName(SourceRange *Range, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 39 | Declarator::TheContext Context, |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 40 | AccessSpecifier AS, |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 41 | Decl **OwnedType, |
| 42 | ParsedAttributes *Attrs) { |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 43 | DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context); |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 44 | if (DSC == DSC_normal) |
| 45 | DSC = DSC_type_specifier; |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 46 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | // Parse the common declaration-specifiers piece. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 48 | DeclSpec DS(AttrFactory); |
Richard Smith | 6b3d3e5 | 2013-02-20 19:22:51 +0000 | [diff] [blame] | 49 | if (Attrs) |
| 50 | DS.addAttributes(Attrs->getList()); |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 51 | ParseSpecifierQualifierList(DS, AS, DSC); |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 52 | if (OwnedType) |
| 53 | *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0; |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 54 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 55 | // Parse the abstract-declarator, if present. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 56 | Declarator DeclaratorInfo(DS, Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 57 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 58 | if (Range) |
| 59 | *Range = DeclaratorInfo.getSourceRange(); |
| 60 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 61 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 62 | return true; |
| 63 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 64 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 67 | |
| 68 | /// isAttributeLateParsed - Return true if the attribute has arguments that |
| 69 | /// require late parsing. |
| 70 | static bool isAttributeLateParsed(const IdentifierInfo &II) { |
| 71 | return llvm::StringSwitch<bool>(II.getName()) |
| 72 | #include "clang/Parse/AttrLateParsed.inc" |
| 73 | .Default(false); |
| 74 | } |
| 75 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 76 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 77 | /// |
| 78 | /// [GNU] attributes: |
| 79 | /// attribute |
| 80 | /// attributes attribute |
| 81 | /// |
| 82 | /// [GNU] attribute: |
| 83 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 84 | /// |
| 85 | /// [GNU] attribute-list: |
| 86 | /// attrib |
| 87 | /// attribute_list ',' attrib |
| 88 | /// |
| 89 | /// [GNU] attrib: |
| 90 | /// empty |
| 91 | /// attrib-name |
| 92 | /// attrib-name '(' identifier ')' |
| 93 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 94 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 95 | /// |
| 96 | /// [GNU] attrib-name: |
| 97 | /// identifier |
| 98 | /// typespec |
| 99 | /// typequal |
| 100 | /// storageclass |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 102 | /// FIXME: The GCC grammar/code for this construct implies we need two |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 103 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 104 | /// which is followed by a comma or close parenthesis, then the arguments |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 105 | /// start with that identifier; otherwise they are an expression list." |
| 106 | /// |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 107 | /// GCC does not require the ',' between attribs in an attribute-list. |
| 108 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 109 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 110 | /// any attributes that don't work (based on my limited testing). Most |
| 111 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 112 | /// a pressing need to implement the 2 token lookahead. |
| 113 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 114 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 115 | SourceLocation *endLoc, |
| 116 | LateParsedAttrList *LateAttrs) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 117 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 119 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 120 | ConsumeToken(); |
| 121 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 122 | "attribute")) { |
| 123 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 124 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 125 | } |
| 126 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 127 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 128 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 129 | } |
| 130 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 131 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 132 | Tok.is(tok::comma)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 133 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 134 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 135 | ConsumeToken(); |
| 136 | continue; |
| 137 | } |
| 138 | // we have an identifier or declaration specifier (const, int, etc.) |
| 139 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 140 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 141 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 142 | if (Tok.is(tok::l_paren)) { |
| 143 | // handle "parameterized" attributes |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 144 | if (LateAttrs && isAttributeLateParsed(*AttrName)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 145 | LateParsedAttribute *LA = |
| 146 | new LateParsedAttribute(this, *AttrName, AttrNameLoc); |
| 147 | LateAttrs->push_back(LA); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 148 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 149 | // 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] | 150 | // with other late-parsed declarations. |
DeLesley Hutchins | 161db02 | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 151 | if (!ClassStack.empty() && !LateAttrs->parseSoon()) |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 152 | getCurrentClass().LateParsedDeclarations.push_back(LA); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 154 | // consume everything up to and including the matching right parens |
| 155 | ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 157 | Token Eof; |
| 158 | Eof.startToken(); |
| 159 | Eof.setLocation(Tok.getLocation()); |
| 160 | LA->Toks.push_back(Eof); |
| 161 | } else { |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 162 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 163 | 0, SourceLocation(), AttributeList::AS_GNU); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 164 | } |
| 165 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 166 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 167 | 0, SourceLocation(), 0, 0, AttributeList::AS_GNU); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 171 | SkipUntil(tok::r_paren, false); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 172 | SourceLocation Loc = Tok.getLocation(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 173 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 174 | SkipUntil(tok::r_paren, false); |
| 175 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 176 | if (endLoc) |
| 177 | *endLoc = Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 178 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Douglas Gregor | 92eb7d8 | 2013-05-02 23:25:32 +0000 | [diff] [blame] | 181 | /// \brief Determine whether the given attribute has all expression arguments. |
| 182 | static bool attributeHasExprArgs(const IdentifierInfo &II) { |
| 183 | return llvm::StringSwitch<bool>(II.getName()) |
| 184 | #include "clang/Parse/AttrExprArgs.inc" |
| 185 | .Default(false); |
| 186 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 187 | |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 188 | /// Parse the arguments to a parameterized GNU attribute or |
| 189 | /// a C++11 attribute in "gnu" namespace. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 190 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 191 | SourceLocation AttrNameLoc, |
| 192 | ParsedAttributes &Attrs, |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 193 | SourceLocation *EndLoc, |
| 194 | IdentifierInfo *ScopeName, |
| 195 | SourceLocation ScopeLoc, |
| 196 | AttributeList::Syntax Syntax) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 197 | |
| 198 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 199 | |
| 200 | // Availability attributes have their own grammar. |
| 201 | if (AttrName->isStr("availability")) { |
| 202 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 203 | return; |
| 204 | } |
| 205 | // Thread safety attributes fit into the FIXME case above, so we |
| 206 | // just parse the arguments as a list of expressions |
| 207 | if (IsThreadSafetyAttribute(AttrName->getName())) { |
| 208 | ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 209 | return; |
| 210 | } |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 211 | // Type safety attributes have their own grammar. |
| 212 | if (AttrName->isStr("type_tag_for_datatype")) { |
| 213 | ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 214 | return; |
| 215 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 216 | |
| 217 | ConsumeParen(); // ignore the left paren loc for now |
| 218 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 219 | IdentifierInfo *ParmName = 0; |
| 220 | SourceLocation ParmLoc; |
| 221 | bool BuiltinType = false; |
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 | |
| 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; |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 260 | ParmName = Tok.getIdentifierInfo(); |
| 261 | ParmLoc = ConsumeToken(); |
| 262 | break; |
| 263 | |
| 264 | default: |
| 265 | break; |
| 266 | } |
| 267 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 268 | ExprVector ArgExprs; |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 269 | bool isInvalid = false; |
| 270 | bool isParmType = false; |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 271 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 272 | if (!BuiltinType && !AttrName->isStr("vec_type_hint") && |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 273 | (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) { |
| 274 | // Eat the comma. |
| 275 | if (ParmLoc.isValid()) |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 276 | ConsumeToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 277 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 278 | // Parse the non-empty comma-separated list of expressions. |
| 279 | while (1) { |
| 280 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 281 | if (ArgExpr.isInvalid()) { |
| 282 | SkipUntil(tok::r_paren); |
| 283 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 284 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 285 | ArgExprs.push_back(ArgExpr.release()); |
| 286 | if (Tok.isNot(tok::comma)) |
| 287 | break; |
| 288 | ConsumeToken(); // Eat the comma, move to the next argument |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 289 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 290 | } |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 291 | else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) { |
| 292 | if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<", |
| 293 | tok::greater)) { |
Fariborz Jahanian | b224343 | 2011-10-18 23:13:50 +0000 | [diff] [blame] | 294 | while (Tok.is(tok::identifier)) { |
| 295 | ConsumeToken(); |
| 296 | if (Tok.is(tok::greater)) |
| 297 | break; |
| 298 | if (Tok.is(tok::comma)) { |
| 299 | ConsumeToken(); |
| 300 | continue; |
| 301 | } |
| 302 | } |
| 303 | if (Tok.isNot(tok::greater)) |
| 304 | Diag(Tok, diag::err_iboutletcollection_with_protocol); |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 305 | SkipUntil(tok::r_paren, false, true); // skip until ')' |
| 306 | } |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 307 | } else if (AttrName->isStr("vec_type_hint")) { |
| 308 | if (T.get() && !T.isInvalid()) |
| 309 | isParmType = true; |
| 310 | else { |
| 311 | if (Tok.is(tok::identifier)) |
| 312 | ConsumeToken(); |
| 313 | if (TypeParsed) |
| 314 | isInvalid = true; |
| 315 | } |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 316 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 317 | |
| 318 | SourceLocation RParen = Tok.getLocation(); |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 319 | if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen) && |
| 320 | !isInvalid) { |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 321 | SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc; |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 322 | if (isParmType) { |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 323 | Attrs.addNewTypeAttr(AttrName, SourceRange(AttrLoc, RParen), ScopeName, |
| 324 | ScopeLoc, ParmName, ParmLoc, T.get(), Syntax); |
| 325 | } else { |
| 326 | AttributeList *attr = Attrs.addNew( |
| 327 | AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc, ParmName, |
| 328 | ParmLoc, ArgExprs.data(), ArgExprs.size(), Syntax); |
| 329 | if (BuiltinType && |
| 330 | attr->getKind() == AttributeList::AT_IBOutletCollection) |
| 331 | Diag(Tok, diag::err_iboutletcollection_builtintype); |
| 332 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 333 | } |
| 334 | } |
| 335 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 336 | /// \brief Parses a single argument for a declspec, including the |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 337 | /// surrounding parens. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 338 | void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 339 | SourceLocation AttrNameLoc, |
| 340 | ParsedAttributes &Attrs) |
| 341 | { |
| 342 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 343 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 344 | AttrName->getNameStart(), tok::r_paren)) |
| 345 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 346 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 347 | ExprResult ArgExpr(ParseConstantExpression()); |
| 348 | if (ArgExpr.isInvalid()) { |
| 349 | T.skipToEnd(); |
| 350 | return; |
| 351 | } |
| 352 | Expr *ExprList = ArgExpr.take(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 353 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 354 | &ExprList, 1, AttributeList::AS_Declspec); |
| 355 | |
| 356 | T.consumeClose(); |
| 357 | } |
| 358 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 359 | /// \brief Determines whether a declspec is a "simple" one requiring no |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 360 | /// arguments. |
| 361 | bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) { |
| 362 | return llvm::StringSwitch<bool>(Ident->getName()) |
| 363 | .Case("dllimport", true) |
| 364 | .Case("dllexport", true) |
| 365 | .Case("noreturn", true) |
| 366 | .Case("nothrow", true) |
| 367 | .Case("noinline", true) |
| 368 | .Case("naked", true) |
| 369 | .Case("appdomain", true) |
| 370 | .Case("process", true) |
| 371 | .Case("jitintrinsic", true) |
| 372 | .Case("noalias", true) |
| 373 | .Case("restrict", true) |
| 374 | .Case("novtable", true) |
| 375 | .Case("selectany", true) |
| 376 | .Case("thread", true) |
Aaron Ballman | 3ce0de6 | 2013-05-04 16:58:37 +0000 | [diff] [blame] | 377 | .Case("safebuffers", true ) |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 378 | .Default(false); |
| 379 | } |
| 380 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 381 | /// \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] | 382 | /// parameters). Will return false if we properly handled the declspec, or |
| 383 | /// true if it is an unknown declspec. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 384 | void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 385 | SourceLocation Loc, |
| 386 | ParsedAttributes &Attrs) { |
| 387 | // Try to handle the easy case first -- these declspecs all take a single |
| 388 | // parameter as their argument. |
| 389 | if (llvm::StringSwitch<bool>(Ident->getName()) |
| 390 | .Case("uuid", true) |
| 391 | .Case("align", true) |
| 392 | .Case("allocate", true) |
| 393 | .Default(false)) { |
| 394 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 395 | } else if (Ident->getName() == "deprecated") { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 396 | // The deprecated declspec has an optional single argument, so we will |
| 397 | // 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] | 398 | // not. |
| 399 | if (Tok.getKind() == tok::l_paren) |
| 400 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 401 | else |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 402 | Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 403 | AttributeList::AS_Declspec); |
| 404 | } else if (Ident->getName() == "property") { |
| 405 | // 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] | 406 | // assignment expressions as a parameter, but the lhs of the assignment |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 407 | // must be named get or put. |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 408 | if (Tok.isNot(tok::l_paren)) { |
| 409 | Diag(Tok.getLocation(), diag::err_expected_lparen_after) |
| 410 | << Ident->getNameStart(); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 411 | return; |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 412 | } |
| 413 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 414 | T.expectAndConsume(diag::err_expected_lparen_after, |
| 415 | Ident->getNameStart(), tok::r_paren); |
| 416 | |
| 417 | enum AccessorKind { |
| 418 | AK_Invalid = -1, |
| 419 | AK_Put = 0, AK_Get = 1 // indices into AccessorNames |
| 420 | }; |
| 421 | IdentifierInfo *AccessorNames[] = { 0, 0 }; |
| 422 | bool HasInvalidAccessor = false; |
| 423 | |
| 424 | // Parse the accessor specifications. |
| 425 | while (true) { |
| 426 | // Stop if this doesn't look like an accessor spec. |
| 427 | if (!Tok.is(tok::identifier)) { |
| 428 | // If the user wrote a completely empty list, use a special diagnostic. |
| 429 | if (Tok.is(tok::r_paren) && !HasInvalidAccessor && |
| 430 | AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) { |
| 431 | Diag(Loc, diag::err_ms_property_no_getter_or_putter); |
| 432 | break; |
| 433 | } |
| 434 | |
| 435 | Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor); |
| 436 | break; |
| 437 | } |
| 438 | |
| 439 | AccessorKind Kind; |
| 440 | SourceLocation KindLoc = Tok.getLocation(); |
| 441 | StringRef KindStr = Tok.getIdentifierInfo()->getName(); |
| 442 | if (KindStr == "get") { |
| 443 | Kind = AK_Get; |
| 444 | } else if (KindStr == "put") { |
| 445 | Kind = AK_Put; |
| 446 | |
| 447 | // Recover from the common mistake of using 'set' instead of 'put'. |
| 448 | } else if (KindStr == "set") { |
| 449 | Diag(KindLoc, diag::err_ms_property_has_set_accessor) |
| 450 | << FixItHint::CreateReplacement(KindLoc, "put"); |
| 451 | Kind = AK_Put; |
| 452 | |
| 453 | // Handle the mistake of forgetting the accessor kind by skipping |
| 454 | // this accessor. |
| 455 | } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) { |
| 456 | Diag(KindLoc, diag::err_ms_property_missing_accessor_kind); |
| 457 | ConsumeToken(); |
| 458 | HasInvalidAccessor = true; |
| 459 | goto next_property_accessor; |
| 460 | |
| 461 | // Otherwise, complain about the unknown accessor kind. |
| 462 | } else { |
| 463 | Diag(KindLoc, diag::err_ms_property_unknown_accessor); |
| 464 | HasInvalidAccessor = true; |
| 465 | Kind = AK_Invalid; |
| 466 | |
| 467 | // Try to keep parsing unless it doesn't look like an accessor spec. |
| 468 | if (!NextToken().is(tok::equal)) break; |
| 469 | } |
| 470 | |
| 471 | // Consume the identifier. |
| 472 | ConsumeToken(); |
| 473 | |
| 474 | // Consume the '='. |
| 475 | if (Tok.is(tok::equal)) { |
| 476 | ConsumeToken(); |
| 477 | } else { |
| 478 | Diag(Tok.getLocation(), diag::err_ms_property_expected_equal) |
| 479 | << KindStr; |
| 480 | break; |
| 481 | } |
| 482 | |
| 483 | // Expect the method name. |
| 484 | if (!Tok.is(tok::identifier)) { |
| 485 | Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name); |
| 486 | break; |
| 487 | } |
| 488 | |
| 489 | if (Kind == AK_Invalid) { |
| 490 | // Just drop invalid accessors. |
| 491 | } else if (AccessorNames[Kind] != NULL) { |
| 492 | // Complain about the repeated accessor, ignore it, and keep parsing. |
| 493 | Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr; |
| 494 | } else { |
| 495 | AccessorNames[Kind] = Tok.getIdentifierInfo(); |
| 496 | } |
| 497 | ConsumeToken(); |
| 498 | |
| 499 | next_property_accessor: |
| 500 | // Keep processing accessors until we run out. |
| 501 | if (Tok.is(tok::comma)) { |
| 502 | ConsumeAnyToken(); |
| 503 | continue; |
| 504 | |
| 505 | // If we run into the ')', stop without consuming it. |
| 506 | } else if (Tok.is(tok::r_paren)) { |
| 507 | break; |
| 508 | } else { |
| 509 | Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen); |
| 510 | break; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | // Only add the property attribute if it was well-formed. |
| 515 | if (!HasInvalidAccessor) { |
| 516 | Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(), 0, |
| 517 | SourceLocation(), |
| 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. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 591 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 592 | 0, 0, AttributeList::AS_Declspec); |
| 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(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 608 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 609 | SourceLocation(), 0, 0, 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(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 618 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 619 | SourceLocation(), 0, 0, 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(); |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 628 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 629 | SourceLocation(), 0, 0, 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) { |
| 813 | SourceLocation PlatformLoc; |
| 814 | IdentifierInfo *Platform = 0; |
| 815 | |
| 816 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 817 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 818 | ExprResult MessageExpr; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 819 | |
| 820 | // Opening '('. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 821 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 822 | if (T.consumeOpen()) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 823 | Diag(Tok, diag::err_expected_lparen); |
| 824 | return; |
| 825 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 826 | |
| 827 | // Parse the platform name, |
| 828 | if (Tok.isNot(tok::identifier)) { |
| 829 | Diag(Tok, diag::err_availability_expected_platform); |
| 830 | SkipUntil(tok::r_paren); |
| 831 | return; |
| 832 | } |
| 833 | Platform = Tok.getIdentifierInfo(); |
| 834 | PlatformLoc = ConsumeToken(); |
| 835 | |
| 836 | // Parse the ',' following the platform name. |
| 837 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren)) |
| 838 | return; |
| 839 | |
| 840 | // If we haven't grabbed the pointers for the identifiers |
| 841 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 842 | if (!Ident_introduced) { |
| 843 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 844 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 845 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 846 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 847 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 851 | SourceLocation UnavailableLoc; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 852 | do { |
| 853 | if (Tok.isNot(tok::identifier)) { |
| 854 | Diag(Tok, diag::err_availability_expected_change); |
| 855 | SkipUntil(tok::r_paren); |
| 856 | return; |
| 857 | } |
| 858 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 859 | SourceLocation KeywordLoc = ConsumeToken(); |
| 860 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 861 | if (Keyword == Ident_unavailable) { |
| 862 | if (UnavailableLoc.isValid()) { |
| 863 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 864 | << Keyword << SourceRange(UnavailableLoc); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 865 | } |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 866 | UnavailableLoc = KeywordLoc; |
| 867 | |
| 868 | if (Tok.isNot(tok::comma)) |
| 869 | break; |
| 870 | |
| 871 | ConsumeToken(); |
| 872 | continue; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 875 | if (Tok.isNot(tok::equal)) { |
| 876 | Diag(Tok, diag::err_expected_equal_after) |
| 877 | << Keyword; |
| 878 | SkipUntil(tok::r_paren); |
| 879 | return; |
| 880 | } |
| 881 | ConsumeToken(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 882 | if (Keyword == Ident_message) { |
| 883 | if (!isTokenStringLiteral()) { |
Andy Gibbs | 97f8461 | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 884 | Diag(Tok, diag::err_expected_string_literal) |
| 885 | << /*Source='availability attribute'*/2; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 886 | SkipUntil(tok::r_paren); |
| 887 | return; |
| 888 | } |
| 889 | MessageExpr = ParseStringLiteralExpression(); |
| 890 | break; |
| 891 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 892 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 893 | SourceRange VersionRange; |
| 894 | VersionTuple Version = ParseVersionTuple(VersionRange); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 895 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 896 | if (Version.empty()) { |
| 897 | SkipUntil(tok::r_paren); |
| 898 | return; |
| 899 | } |
| 900 | |
| 901 | unsigned Index; |
| 902 | if (Keyword == Ident_introduced) |
| 903 | Index = Introduced; |
| 904 | else if (Keyword == Ident_deprecated) |
| 905 | Index = Deprecated; |
| 906 | else if (Keyword == Ident_obsoleted) |
| 907 | Index = Obsoleted; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 908 | else |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 909 | Index = Unknown; |
| 910 | |
| 911 | if (Index < Unknown) { |
| 912 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 913 | Diag(KeywordLoc, diag::err_availability_redundant) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 914 | << Keyword |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 915 | << SourceRange(Changes[Index].KeywordLoc, |
| 916 | Changes[Index].VersionRange.getEnd()); |
| 917 | } |
| 918 | |
| 919 | Changes[Index].KeywordLoc = KeywordLoc; |
| 920 | Changes[Index].Version = Version; |
| 921 | Changes[Index].VersionRange = VersionRange; |
| 922 | } else { |
| 923 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 924 | << Keyword << VersionRange; |
| 925 | } |
| 926 | |
| 927 | if (Tok.isNot(tok::comma)) |
| 928 | break; |
| 929 | |
| 930 | ConsumeToken(); |
| 931 | } while (true); |
| 932 | |
| 933 | // Closing ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 934 | if (T.consumeClose()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 935 | return; |
| 936 | |
| 937 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 938 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 939 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 940 | // The 'unavailable' availability cannot be combined with any other |
| 941 | // availability changes. Make sure that hasn't happened. |
| 942 | if (UnavailableLoc.isValid()) { |
| 943 | bool Complained = false; |
| 944 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 945 | if (Changes[Index].KeywordLoc.isValid()) { |
| 946 | if (!Complained) { |
| 947 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 948 | << SourceRange(Changes[Index].KeywordLoc, |
| 949 | Changes[Index].VersionRange.getEnd()); |
| 950 | Complained = true; |
| 951 | } |
| 952 | |
| 953 | // Clear out the availability. |
| 954 | Changes[Index] = AvailabilityChange(); |
| 955 | } |
| 956 | } |
| 957 | } |
| 958 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 959 | // Record this attribute |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 960 | attrs.addNew(&Availability, |
| 961 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Fariborz Jahanian | f96708d | 2012-01-23 23:38:32 +0000 | [diff] [blame] | 962 | 0, AvailabilityLoc, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 963 | Platform, PlatformLoc, |
| 964 | Changes[Introduced], |
| 965 | Changes[Deprecated], |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 966 | Changes[Obsoleted], |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 967 | UnavailableLoc, MessageExpr.take(), |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 968 | AttributeList::AS_GNU); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 969 | } |
| 970 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 971 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 972 | // Late Parsed Attributes: |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 973 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 974 | |
| 975 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 976 | |
| 977 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 978 | Self->ParseLexedAttributes(*Class); |
| 979 | } |
| 980 | |
| 981 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 982 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 983 | } |
| 984 | |
| 985 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 986 | /// scope appropriately. |
| 987 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 988 | // Deal with templates |
| 989 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 990 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 991 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 992 | HasTemplateScope); |
| 993 | if (HasTemplateScope) |
| 994 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 995 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 996 | // Set or update the scope flags. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 997 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 998 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 999 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 1000 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 1001 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1002 | // Enter the scope of nested classes |
| 1003 | if (!AlreadyHasClassScope) |
| 1004 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 1005 | Class.TagOrTemplate); |
Benjamin Kramer | 268efba | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 1006 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 1007 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 1008 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 1009 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1010 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1011 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1012 | if (!AlreadyHasClassScope) |
| 1013 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 1014 | Class.TagOrTemplate); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1015 | } |
| 1016 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1017 | |
| 1018 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 1019 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 1020 | bool EnterScope, bool OnDefinition) { |
DeLesley Hutchins | 161db02 | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1021 | assert(LAs.parseSoon() && |
| 1022 | "Attribute list should be marked for immediate parsing."); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1023 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 95526a4 | 2012-08-15 22:41:04 +0000 | [diff] [blame] | 1024 | if (D) |
| 1025 | LAs[i]->addDecl(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1026 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | d306cf7 | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 1027 | delete LAs[i]; |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1028 | } |
| 1029 | LAs.clear(); |
| 1030 | } |
| 1031 | |
| 1032 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1033 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 1034 | /// This will be called at the end of parsing a class declaration |
| 1035 | /// for each LateParsedAttribute. We consume the saved tokens and |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1036 | /// create an attribute with the arguments filled in. We add this |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1037 | /// to the Attribute list for the decl. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1038 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 1039 | bool EnterScope, bool OnDefinition) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1040 | // Save the current token position. |
| 1041 | SourceLocation OrigLoc = Tok.getLocation(); |
| 1042 | |
| 1043 | // Append the current token at the end of the new token stream so that it |
| 1044 | // doesn't get lost. |
| 1045 | LA.Toks.push_back(Tok); |
| 1046 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 1047 | // Consume the previously pushed token. |
Argyrios Kyrtzidis | ab2d09b | 2013-03-27 23:58:17 +0000 | [diff] [blame] | 1048 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1049 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1050 | if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) { |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1051 | // FIXME: Do not warn on C++11 attributes, once we start supporting |
| 1052 | // them here. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1053 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 1054 | << LA.AttrName.getName(); |
| 1055 | } |
| 1056 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1057 | ParsedAttributes Attrs(AttrFactory); |
| 1058 | SourceLocation endLoc; |
| 1059 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1060 | if (LA.Decls.size() > 0) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1061 | Decl *D = LA.Decls[0]; |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1062 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1063 | RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1064 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1065 | // Allow 'this' within late-parsed attributes. |
Richard Smith | cafeb94 | 2013-06-07 02:33:37 +0000 | [diff] [blame] | 1066 | Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0, |
| 1067 | ND && ND->isCXXInstanceMember()); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1068 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1069 | if (LA.Decls.size() == 1) { |
| 1070 | // If the Decl is templatized, add template parameters to scope. |
| 1071 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 1072 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 1073 | if (HasTemplateScope) |
| 1074 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1075 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1076 | // If the Decl is on a function, add function parameters to the scope. |
| 1077 | bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
| 1078 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope); |
| 1079 | if (HasFunScope) |
| 1080 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1081 | |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1082 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 1083 | 0, SourceLocation(), AttributeList::AS_GNU); |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1084 | |
| 1085 | if (HasFunScope) { |
| 1086 | Actions.ActOnExitFunctionContext(); |
| 1087 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 1088 | } |
| 1089 | if (HasTemplateScope) { |
| 1090 | TempScope.Exit(); |
| 1091 | } |
| 1092 | } else { |
| 1093 | // If there are multiple decls, then the decl cannot be within the |
| 1094 | // function scope. |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1095 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 1096 | 0, SourceLocation(), AttributeList::AS_GNU); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1097 | } |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 1098 | } else { |
| 1099 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1102 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) { |
| 1103 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
| 1104 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1105 | |
| 1106 | if (Tok.getLocation() != OrigLoc) { |
| 1107 | // Due to a parsing error, we either went over the cached tokens or |
| 1108 | // there are still cached tokens left, so we skip the leftover tokens. |
| 1109 | // Since this is an uncommon situation that should be avoided, use the |
| 1110 | // expensive isBeforeInTranslationUnit call. |
| 1111 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 1112 | OrigLoc)) |
| 1113 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
Douglas Gregor | d78ef5b | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 1114 | ConsumeAnyToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1115 | } |
| 1116 | } |
| 1117 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1118 | /// \brief Wrapper around a case statement checking if AttrName is |
| 1119 | /// one of the thread safety attributes |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1120 | bool Parser::IsThreadSafetyAttribute(StringRef AttrName) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1121 | return llvm::StringSwitch<bool>(AttrName) |
| 1122 | .Case("guarded_by", true) |
| 1123 | .Case("guarded_var", true) |
| 1124 | .Case("pt_guarded_by", true) |
| 1125 | .Case("pt_guarded_var", true) |
| 1126 | .Case("lockable", true) |
| 1127 | .Case("scoped_lockable", true) |
| 1128 | .Case("no_thread_safety_analysis", true) |
| 1129 | .Case("acquired_after", true) |
| 1130 | .Case("acquired_before", true) |
| 1131 | .Case("exclusive_lock_function", true) |
| 1132 | .Case("shared_lock_function", true) |
| 1133 | .Case("exclusive_trylock_function", true) |
| 1134 | .Case("shared_trylock_function", true) |
| 1135 | .Case("unlock_function", true) |
| 1136 | .Case("lock_returned", true) |
| 1137 | .Case("locks_excluded", true) |
| 1138 | .Case("exclusive_locks_required", true) |
| 1139 | .Case("shared_locks_required", true) |
| 1140 | .Default(false); |
| 1141 | } |
| 1142 | |
| 1143 | /// \brief Parse the contents of thread safety attributes. These |
| 1144 | /// should always be parsed as an expression list. |
| 1145 | /// |
| 1146 | /// We need to special case the parsing due to the fact that if the first token |
| 1147 | /// of the first argument is an identifier, the main parse loop will store |
| 1148 | /// that token as a "parameter" and the rest of |
| 1149 | /// the arguments will be added to a list of "arguments". However, |
| 1150 | /// subsequent tokens in the first argument are lost. We instead parse each |
| 1151 | /// argument as an expression and add all arguments to the list of "arguments". |
| 1152 | /// In future, we will take advantage of this special case to also |
| 1153 | /// deal with some argument scoping issues here (for example, referring to a |
| 1154 | /// function parameter in the attribute on that function). |
| 1155 | void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName, |
| 1156 | SourceLocation AttrNameLoc, |
| 1157 | ParsedAttributes &Attrs, |
| 1158 | SourceLocation *EndLoc) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1159 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1160 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1161 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1162 | T.consumeOpen(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1163 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1164 | ExprVector ArgExprs; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1165 | bool ArgExprsOk = true; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1166 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1167 | // now parse the list of expressions |
DeLesley Hutchins | 4805f15 | 2011-12-14 19:36:06 +0000 | [diff] [blame] | 1168 | while (Tok.isNot(tok::r_paren)) { |
DeLesley Hutchins | ed4330b | 2013-02-07 19:01:07 +0000 | [diff] [blame] | 1169 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1170 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 1171 | if (ArgExpr.isInvalid()) { |
| 1172 | ArgExprsOk = false; |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1173 | T.consumeClose(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1174 | break; |
| 1175 | } else { |
| 1176 | ArgExprs.push_back(ArgExpr.release()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1177 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1178 | if (Tok.isNot(tok::comma)) |
| 1179 | break; |
| 1180 | ConsumeToken(); // Eat the comma, move to the next argument |
| 1181 | } |
| 1182 | // Match the ')'. |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 1183 | if (ArgExprsOk && !T.consumeClose()) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1184 | Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1185 | ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1186 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1187 | if (EndLoc) |
| 1188 | *EndLoc = T.getCloseLocation(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1191 | void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName, |
| 1192 | SourceLocation AttrNameLoc, |
| 1193 | ParsedAttributes &Attrs, |
| 1194 | SourceLocation *EndLoc) { |
| 1195 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 1196 | |
| 1197 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1198 | T.consumeOpen(); |
| 1199 | |
| 1200 | if (Tok.isNot(tok::identifier)) { |
| 1201 | Diag(Tok, diag::err_expected_ident); |
| 1202 | T.skipToEnd(); |
| 1203 | return; |
| 1204 | } |
| 1205 | IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo(); |
| 1206 | SourceLocation ArgumentKindLoc = ConsumeToken(); |
| 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, |
| 1246 | ArgumentKind, ArgumentKindLoc, |
| 1247 | MatchingCType.release(), LayoutCompatible, |
| 1248 | MustBeNull, AttributeList::AS_GNU); |
| 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); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1669 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1); |
| 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 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1736 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1737 | DeclsInGroup.data(), |
| 1738 | DeclsInGroup.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1741 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1742 | /// declarator. Returns true on an error. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1743 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1744 | // If a simple-asm-expr is present, parse it. |
| 1745 | if (Tok.is(tok::kw_asm)) { |
| 1746 | SourceLocation Loc; |
| 1747 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1748 | if (AsmLabel.isInvalid()) { |
| 1749 | SkipUntil(tok::semi, true, true); |
| 1750 | return true; |
| 1751 | } |
| 1752 | |
| 1753 | D.setAsmLabel(AsmLabel.release()); |
| 1754 | D.SetRangeEnd(Loc); |
| 1755 | } |
| 1756 | |
| 1757 | MaybeParseGNUAttributes(D); |
| 1758 | return false; |
| 1759 | } |
| 1760 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1761 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1762 | /// declarator'. This method parses the remainder of the declaration |
| 1763 | /// (including any attributes or initializer, among other things) and |
| 1764 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1765 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1766 | /// init-declarator: [C99 6.7] |
| 1767 | /// declarator |
| 1768 | /// declarator '=' initializer |
| 1769 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1770 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1771 | /// [C++] declarator initializer[opt] |
| 1772 | /// |
| 1773 | /// [C++] initializer: |
| 1774 | /// [C++] '=' initializer-clause |
| 1775 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1776 | /// [C++0x] '=' 'default' [TODO] |
| 1777 | /// [C++0x] '=' 'delete' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1778 | /// [C++0x] braced-init-list |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1779 | /// |
| 1780 | /// According to the standard grammar, =default and =delete are function |
| 1781 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1782 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1783 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1784 | const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1785 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1786 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1787 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1788 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1789 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1790 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1791 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1792 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1793 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1794 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1795 | switch (TemplateInfo.Kind) { |
| 1796 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1797 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1798 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1799 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1800 | case ParsedTemplateInfo::Template: |
| 1801 | case ParsedTemplateInfo::ExplicitSpecialization: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1802 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1803 | *TemplateInfo.TemplateParams, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1804 | D); |
| 1805 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1806 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1807 | case ParsedTemplateInfo::ExplicitInstantiation: { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1808 | DeclResult ThisRes |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1809 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1810 | TemplateInfo.ExternLoc, |
| 1811 | TemplateInfo.TemplateLoc, |
| 1812 | D); |
| 1813 | if (ThisRes.isInvalid()) { |
| 1814 | SkipUntil(tok::semi, true, true); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1815 | return 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1816 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1817 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1818 | ThisDecl = ThisRes.get(); |
| 1819 | break; |
| 1820 | } |
| 1821 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1822 | |
Richard Smith | a2c3646 | 2013-04-26 16:15:35 +0000 | [diff] [blame] | 1823 | bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType(); |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1824 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1825 | // Parse declarator '=' initializer. |
Richard Trieu | d6c7c67 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1826 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | fcaf27e | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1827 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1828 | ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1829 | if (Tok.is(tok::kw_delete)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1830 | if (D.isFunctionDeclarator()) |
| 1831 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1832 | << 1 /* delete */; |
| 1833 | else |
| 1834 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Sean Hunt | fe2695e | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1835 | } else if (Tok.is(tok::kw_default)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1836 | if (D.isFunctionDeclarator()) |
Sebastian Redl | ecfcd56 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1837 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1838 | << 0 /* default */; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1839 | else |
| 1840 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1841 | } else { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1842 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1843 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1844 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1845 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1846 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1847 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1848 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Peter Collingbourne | ec98f2f | 2012-07-27 12:56:09 +0000 | [diff] [blame] | 1849 | Actions.FinalizeDeclaration(ThisDecl); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1850 | cutOffParsing(); |
| 1851 | return 0; |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1852 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1853 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1854 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1855 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1856 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1857 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1858 | ExitScope(); |
| 1859 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1860 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1861 | if (Init.isInvalid()) { |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1862 | SkipUntil(tok::comma, true, true); |
| 1863 | Actions.ActOnInitializerError(ThisDecl); |
| 1864 | } else |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1865 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1866 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1867 | } |
| 1868 | } else if (Tok.is(tok::l_paren)) { |
| 1869 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1870 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1871 | T.consumeOpen(); |
| 1872 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1873 | ExprVector Exprs; |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1874 | CommaLocsTy CommaLocs; |
| 1875 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1876 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1877 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1878 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1879 | } |
| 1880 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1881 | if (ParseExpressionList(Exprs, CommaLocs)) { |
David Blaikie | 3ea19c8 | 2012-10-10 23:15:05 +0000 | [diff] [blame] | 1882 | Actions.ActOnInitializerError(ThisDecl); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1883 | SkipUntil(tok::r_paren); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1884 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1885 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1886 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1887 | ExitScope(); |
| 1888 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1889 | } else { |
| 1890 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1891 | T.consumeClose(); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1892 | |
| 1893 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1894 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1895 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1896 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1897 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1898 | ExitScope(); |
| 1899 | } |
| 1900 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1901 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 1902 | T.getCloseLocation(), |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1903 | Exprs); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1904 | Actions.AddInitializerToDecl(ThisDecl, Initializer.take(), |
| 1905 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1906 | } |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1907 | } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) && |
Fariborz Jahanian | b0ed95c | 2012-07-03 23:22:13 +0000 | [diff] [blame] | 1908 | (!CurParsedObjCImpl || !D.isFunctionDeclarator())) { |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1909 | // Parse C++0x braced-init-list. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1910 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1911 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1912 | if (D.getCXXScopeSpec().isSet()) { |
| 1913 | EnterScope(0); |
| 1914 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1915 | } |
| 1916 | |
| 1917 | ExprResult Init(ParseBraceInitializer()); |
| 1918 | |
| 1919 | if (D.getCXXScopeSpec().isSet()) { |
| 1920 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1921 | ExitScope(); |
| 1922 | } |
| 1923 | |
| 1924 | if (Init.isInvalid()) { |
| 1925 | Actions.ActOnInitializerError(ThisDecl); |
| 1926 | } else |
| 1927 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1928 | /*DirectInit=*/true, TypeContainsAuto); |
| 1929 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1930 | } else { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1931 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1932 | } |
| 1933 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1934 | Actions.FinalizeDeclaration(ThisDecl); |
| 1935 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1936 | return ThisDecl; |
| 1937 | } |
| 1938 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1939 | /// ParseSpecifierQualifierList |
| 1940 | /// specifier-qualifier-list: |
| 1941 | /// type-specifier specifier-qualifier-list[opt] |
| 1942 | /// type-qualifier specifier-qualifier-list[opt] |
| 1943 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 1944 | /// |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1945 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 1946 | DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1947 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1948 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1949 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1950 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1951 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1952 | // Validate declspec for type-name. |
| 1953 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 1954 | if ((DSC == DSC_type_specifier || DSC == DSC_trailing) && |
| 1955 | !DS.hasTypeSpecifier()) { |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1956 | Diag(Tok, diag::err_expected_type); |
| 1957 | DS.SetTypeSpecError(); |
| 1958 | } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 1959 | !DS.hasAttributes()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1960 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1961 | if (!DS.hasTypeSpecifier()) |
| 1962 | DS.SetTypeSpecError(); |
| 1963 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1964 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1965 | // Issue diagnostic and remove storage class if present. |
| 1966 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 1967 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1968 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 1969 | else |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 1970 | Diag(DS.getThreadStorageClassSpecLoc(), |
| 1971 | diag::err_typename_invalid_storageclass); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1972 | DS.ClearStorageClassSpecs(); |
| 1973 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1974 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1975 | // Issue diagnostic and remove function specfier if present. |
| 1976 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1977 | if (DS.isInlineSpecified()) |
| 1978 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1979 | if (DS.isVirtualSpecified()) |
| 1980 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1981 | if (DS.isExplicitSpecified()) |
| 1982 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1983 | DS.ClearFunctionSpecs(); |
| 1984 | } |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1985 | |
| 1986 | // Issue diagnostic and remove constexpr specfier if present. |
| 1987 | if (DS.isConstexprSpecified()) { |
| 1988 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 1989 | DS.ClearConstexprSpec(); |
| 1990 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1993 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 1994 | /// specified token is valid after the identifier in a declarator which |
| 1995 | /// immediately follows the declspec. For example, these things are valid: |
| 1996 | /// |
| 1997 | /// int x [ 4]; // direct-declarator |
| 1998 | /// int x ( int y); // direct-declarator |
| 1999 | /// int(int x ) // direct-declarator |
| 2000 | /// int x ; // simple-declaration |
| 2001 | /// int x = 17; // init-declarator-list |
| 2002 | /// int x , y; // init-declarator-list |
| 2003 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2004 | /// int x : 4; // struct-declarator |
Chris Lattner | c83c27a | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 2005 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2006 | /// |
| 2007 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 2008 | /// ')' happens to be valid anyway). |
| 2009 | /// int (x) |
| 2010 | /// |
| 2011 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 2012 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 2013 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2014 | 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] | 2015 | } |
| 2016 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2017 | |
| 2018 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 2019 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 2020 | /// the declspec has no type specifier. In this case, the declspec is either |
| 2021 | /// malformed or is "implicit int" (in K&R and C89). |
| 2022 | /// |
| 2023 | /// This method handles diagnosing this prettily and returns false if the |
| 2024 | /// declspec is done being processed. If it recovers and thinks there may be |
| 2025 | /// other pieces of declspec after it, it returns true. |
| 2026 | /// |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2027 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2028 | const ParsedTemplateInfo &TemplateInfo, |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2029 | AccessSpecifier AS, DeclSpecContext DSC, |
| 2030 | ParsedAttributesWithRange &Attrs) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2031 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2032 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2033 | SourceLocation Loc = Tok.getLocation(); |
| 2034 | // If we see an identifier that is not a type name, we normally would |
| 2035 | // parse it as the identifer being declared. However, when a typename |
| 2036 | // is typo'd or the definition is not included, this will incorrectly |
| 2037 | // parse the typename as the identifier name and fall over misparsing |
| 2038 | // later parts of the diagnostic. |
| 2039 | // |
| 2040 | // As such, we try to do some look-ahead in cases where this would |
| 2041 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 2042 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 2043 | // an identifier with implicit int, we'd get a parse error because the |
| 2044 | // next token is obviously invalid for a type. Parse these as a case |
| 2045 | // with an invalid type specifier. |
| 2046 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2047 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2048 | // 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] | 2049 | // error, do lookahead to try to do better recovery. This never applies |
| 2050 | // within a type specifier. Outside of C++, we allow this even if the |
| 2051 | // language doesn't "officially" support implicit int -- we support |
Richard Smith | 58eb370 | 2013-04-30 22:43:51 +0000 | [diff] [blame] | 2052 | // implicit int as an extension in C99 and C11. |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 2053 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
Richard Smith | 58eb370 | 2013-04-30 22:43:51 +0000 | [diff] [blame] | 2054 | !getLangOpts().CPlusPlus && |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2055 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2056 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 2057 | // we just avoid eating the identifier, so it will be parsed as the |
| 2058 | // identifier in the declarator. |
| 2059 | return false; |
| 2060 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2061 | |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2062 | if (getLangOpts().CPlusPlus && |
| 2063 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 2064 | // Don't require a type specifier if we have the 'auto' storage class |
| 2065 | // specifier in C++98 -- we'll promote it to a type specifier. |
| 2066 | return false; |
| 2067 | } |
| 2068 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2069 | // Otherwise, if we don't consume this token, we are going to emit an |
| 2070 | // error anyway. Try to recover from various common problems. Check |
| 2071 | // to see if this was a reference to a tag name without a tag specified. |
| 2072 | // 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] | 2073 | // |
| 2074 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 2075 | if (SS == 0) { |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2076 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2077 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2078 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2079 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2080 | default: break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2081 | case DeclSpec::TST_enum: |
| 2082 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 2083 | case DeclSpec::TST_union: |
| 2084 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 2085 | case DeclSpec::TST_struct: |
| 2086 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2087 | case DeclSpec::TST_interface: |
| 2088 | TagName="__interface"; FixitTagName = "__interface "; |
| 2089 | TagKind=tok::kw___interface;break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2090 | case DeclSpec::TST_class: |
| 2091 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2092 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2093 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2094 | if (TagName) { |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2095 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 2096 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 2097 | Sema::LookupOrdinaryName); |
| 2098 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2099 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2100 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 2101 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 2102 | |
| 2103 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 2104 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 2105 | I != IEnd; ++I) |
Kaelyn Uhrain | 392b3f5 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 2106 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2107 | << TokenName << TagName; |
| 2108 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2109 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2110 | // Parse this as a tag as if the missing tag were present. |
| 2111 | if (TagKind == tok::kw_enum) |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2112 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2113 | else |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2114 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2115 | /*EnteringContext*/ false, DSC_normal, Attrs); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2116 | return true; |
| 2117 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2118 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2119 | |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2120 | // Determine whether this identifier could plausibly be the name of something |
Richard Smith | 7514db2 | 2012-05-15 21:42:17 +0000 | [diff] [blame] | 2121 | // being declared (with a missing type). |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2122 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
| 2123 | (!SS || DSC == DSC_top_level || DSC == DSC_class)) { |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2124 | // Look ahead to the next token to try to figure out what this declaration |
| 2125 | // was supposed to be. |
| 2126 | switch (NextToken().getKind()) { |
| 2127 | case tok::comma: |
| 2128 | case tok::equal: |
| 2129 | case tok::kw_asm: |
| 2130 | case tok::l_brace: |
| 2131 | case tok::l_square: |
| 2132 | case tok::semi: |
| 2133 | // This looks like a variable declaration. The type is probably missing. |
| 2134 | // We're done parsing decl-specifiers. |
| 2135 | return false; |
| 2136 | |
| 2137 | case tok::l_paren: { |
| 2138 | // static x(4); // 'x' is not a type |
| 2139 | // x(int n); // 'x' is not a type |
| 2140 | // x (*p)[]; // 'x' is a type |
| 2141 | // |
| 2142 | // Since we're in an error case (or the rare 'implicit int in C++' MS |
| 2143 | // extension), we can afford to perform a tentative parse to determine |
| 2144 | // which case we're in. |
| 2145 | TentativeParsingAction PA(*this); |
| 2146 | ConsumeToken(); |
| 2147 | TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false); |
| 2148 | PA.Revert(); |
| 2149 | if (TPR == TPResult::False()) |
| 2150 | return false; |
| 2151 | // The identifier is followed by a parenthesized declarator. |
| 2152 | // It's supposed to be a type. |
| 2153 | break; |
| 2154 | } |
| 2155 | |
| 2156 | default: |
| 2157 | // This is probably supposed to be a type. This includes cases like: |
| 2158 | // int f(itn); |
| 2159 | // struct S { unsinged : 4; }; |
| 2160 | break; |
| 2161 | } |
| 2162 | } |
| 2163 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2164 | // 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] | 2165 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2166 | ParsedType T; |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2167 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2168 | if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2169 | // The action emitted a diagnostic, so we don't have to. |
| 2170 | if (T) { |
| 2171 | // The action has suggested that the type T could be used. Set that as |
| 2172 | // the type in the declaration specifiers, consume the would-be type |
| 2173 | // name token, and we're done. |
| 2174 | const char *PrevSpec; |
| 2175 | unsigned DiagID; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2176 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2177 | DS.SetRangeEnd(Tok.getLocation()); |
| 2178 | ConsumeToken(); |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2179 | // There may be other declaration specifiers after this. |
| 2180 | return true; |
| 2181 | } else if (II != Tok.getIdentifierInfo()) { |
| 2182 | // If no type was suggested, the correction is to a keyword |
| 2183 | Tok.setKind(II->getTokenID()); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2184 | // There may be other declaration specifiers after this. |
| 2185 | return true; |
| 2186 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2187 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2188 | // Fall through; the action had no suggestion for us. |
| 2189 | } else { |
| 2190 | // The action did not emit a diagnostic, so emit one now. |
| 2191 | SourceRange R; |
| 2192 | if (SS) R = SS->getRange(); |
| 2193 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 2194 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2195 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2196 | // Mark this as an error. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2197 | DS.SetTypeSpecError(); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2198 | DS.SetRangeEnd(Tok.getLocation()); |
| 2199 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2200 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2201 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 2202 | // avoid rippling error messages on subsequent uses of the same type, |
| 2203 | // could be useful if #include was forgotten. |
| 2204 | return false; |
| 2205 | } |
| 2206 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2207 | /// \brief Determine the declaration specifier context from the declarator |
| 2208 | /// context. |
| 2209 | /// |
| 2210 | /// \param Context the declarator context, which is one of the |
| 2211 | /// Declarator::TheContext enumerator values. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2212 | Parser::DeclSpecContext |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2213 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 2214 | if (Context == Declarator::MemberContext) |
| 2215 | return DSC_class; |
| 2216 | if (Context == Declarator::FileContext) |
| 2217 | return DSC_top_level; |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 2218 | if (Context == Declarator::TrailingReturnContext) |
| 2219 | return DSC_trailing; |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2220 | return DSC_normal; |
| 2221 | } |
| 2222 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2223 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 2224 | /// |
| 2225 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 2226 | /// type. Ideally, the type should be propagated directly into Sema. |
| 2227 | /// |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2228 | /// [C11] type-id |
| 2229 | /// [C11] constant-expression |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2230 | /// [C++0x] type-id ...[opt] |
| 2231 | /// [C++0x] assignment-expression ...[opt] |
| 2232 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 2233 | SourceLocation &EllipsisLoc) { |
| 2234 | ExprResult ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2235 | if (isTypeIdInParens()) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2236 | SourceLocation TypeLoc = Tok.getLocation(); |
| 2237 | ParsedType Ty = ParseTypeName().get(); |
| 2238 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2239 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 2240 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2241 | } else |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2242 | ER = ParseConstantExpression(); |
| 2243 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2244 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis)) |
Peter Collingbourne | fe9b2a8 | 2011-10-24 17:56:00 +0000 | [diff] [blame] | 2245 | EllipsisLoc = ConsumeToken(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2246 | |
| 2247 | return ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 2251 | /// attribute to Attrs. |
| 2252 | /// |
| 2253 | /// alignment-specifier: |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2254 | /// [C11] '_Alignas' '(' type-id ')' |
| 2255 | /// [C11] '_Alignas' '(' constant-expression ')' |
Richard Smith | 33f04a2 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2256 | /// [C++11] 'alignas' '(' type-id ...[opt] ')' |
| 2257 | /// [C++11] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2258 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2259 | SourceLocation *EndLoc) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2260 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 2261 | "Not an alignment-specifier!"); |
| 2262 | |
Richard Smith | 33f04a2 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2263 | IdentifierInfo *KWName = Tok.getIdentifierInfo(); |
| 2264 | SourceLocation KWLoc = ConsumeToken(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2265 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2266 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2267 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2268 | return; |
| 2269 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2270 | SourceLocation EllipsisLoc; |
| 2271 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2272 | if (ArgExpr.isInvalid()) { |
| 2273 | SkipUntil(tok::r_paren); |
| 2274 | return; |
| 2275 | } |
| 2276 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2277 | T.consumeClose(); |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2278 | if (EndLoc) |
| 2279 | *EndLoc = T.getCloseLocation(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2280 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2281 | ExprVector ArgExprs; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2282 | ArgExprs.push_back(ArgExpr.release()); |
Richard Smith | 33f04a2 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2283 | Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(), |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2284 | ArgExprs.data(), 1, AttributeList::AS_Keyword, EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2285 | } |
| 2286 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2287 | /// ParseDeclarationSpecifiers |
| 2288 | /// declaration-specifiers: [C99 6.7] |
| 2289 | /// storage-class-specifier declaration-specifiers[opt] |
| 2290 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2291 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2292 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2293 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2294 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2295 | /// |
| 2296 | /// storage-class-specifier: [C99 6.7.1] |
| 2297 | /// 'typedef' |
| 2298 | /// 'extern' |
| 2299 | /// 'static' |
| 2300 | /// 'auto' |
| 2301 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2302 | /// [C++] 'mutable' |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2303 | /// [C++11] 'thread_local' |
| 2304 | /// [C11] '_Thread_local' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2305 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2306 | /// function-specifier: [C99 6.7.4] |
| 2307 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2308 | /// [C++] 'virtual' |
| 2309 | /// [C++] 'explicit' |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2310 | /// [OpenCL] '__kernel' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2311 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2312 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2313 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2314 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 2315 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2316 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2317 | AccessSpecifier AS, |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2318 | DeclSpecContext DSContext, |
| 2319 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2320 | if (DS.getSourceRange().isInvalid()) { |
| 2321 | DS.SetRangeStart(Tok.getLocation()); |
| 2322 | DS.SetRangeEnd(Tok.getLocation()); |
| 2323 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2324 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2325 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2326 | bool AttrsLastTime = false; |
| 2327 | ParsedAttributesWithRange attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2328 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2329 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2330 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2331 | unsigned DiagID = 0; |
| 2332 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2333 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2334 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2335 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2336 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2337 | DoneWithDeclSpec: |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2338 | if (!AttrsLastTime) |
| 2339 | ProhibitAttributes(attrs); |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2340 | else { |
| 2341 | // Reject C++11 attributes that appertain to decl specifiers as |
| 2342 | // we don't support any C++11 attributes that appertain to decl |
| 2343 | // specifiers. This also conforms to what g++ 4.8 is doing. |
| 2344 | ProhibitCXX11Attributes(attrs); |
| 2345 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2346 | DS.takeAttributesFrom(attrs); |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2347 | } |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 2348 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2349 | // If this is not a declaration specifier token, we're done reading decl |
| 2350 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2351 | DS.Finish(Diags, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2352 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2353 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2354 | case tok::l_square: |
| 2355 | case tok::kw_alignas: |
Richard Smith | 672edb0 | 2013-02-22 09:15:49 +0000 | [diff] [blame] | 2356 | if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier()) |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2357 | goto DoneWithDeclSpec; |
| 2358 | |
| 2359 | ProhibitAttributes(attrs); |
| 2360 | // FIXME: It would be good to recover by accepting the attributes, |
| 2361 | // but attempting to do that now would cause serious |
| 2362 | // madness in terms of diagnostics. |
| 2363 | attrs.clear(); |
| 2364 | attrs.Range = SourceRange(); |
| 2365 | |
| 2366 | ParseCXX11Attributes(attrs); |
| 2367 | AttrsLastTime = true; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2368 | continue; |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2369 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2370 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2371 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2372 | if (DS.hasTypeSpecifier()) { |
| 2373 | bool AllowNonIdentifiers |
| 2374 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 2375 | Scope::BlockScope | |
| 2376 | Scope::TemplateParamScope | |
| 2377 | Scope::FunctionPrototypeScope | |
| 2378 | Scope::AtCatchScope)) == 0; |
| 2379 | bool AllowNestedNameSpecifiers |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2380 | = DSContext == DSC_top_level || |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2381 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 2382 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2383 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2384 | AllowNonIdentifiers, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2385 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2386 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2387 | } |
| 2388 | |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2389 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 2390 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 2391 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2392 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2393 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2394 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2395 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 2396 | else if (CurParsedObjCImpl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2397 | CCC = Sema::PCC_ObjCImplementation; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2398 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2399 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2400 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2403 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2404 | // C++ scope specifier. Annotate and loop, or bail out on error. |
| 2405 | if (TryAnnotateCXXScopeToken(true)) { |
| 2406 | if (!DS.hasTypeSpecifier()) |
| 2407 | DS.SetTypeSpecError(); |
| 2408 | goto DoneWithDeclSpec; |
| 2409 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 2410 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 2411 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2412 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2413 | |
| 2414 | case tok::annot_cxxscope: { |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2415 | if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector()) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2416 | goto DoneWithDeclSpec; |
| 2417 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2418 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 2419 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 2420 | Tok.getAnnotationRange(), |
| 2421 | SS); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2422 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2423 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2424 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2425 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2426 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2427 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2428 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2429 | |
| 2430 | // C++ [class.qual]p2: |
| 2431 | // In a lookup in which the constructor is an acceptable lookup |
| 2432 | // result and the nested-name-specifier nominates a class C: |
| 2433 | // |
| 2434 | // - if the name specified after the |
| 2435 | // nested-name-specifier, when looked up in C, is the |
| 2436 | // injected-class-name of C (Clause 9), or |
| 2437 | // |
| 2438 | // - if the name specified after the nested-name-specifier |
| 2439 | // is the same as the identifier or the |
| 2440 | // simple-template-id's template-name in the last |
| 2441 | // component of the nested-name-specifier, |
| 2442 | // |
| 2443 | // the name is instead considered to name the constructor of |
| 2444 | // class C. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2445 | // |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2446 | // Thus, if the template-name is actually the constructor |
| 2447 | // name, then the code is ill-formed; this interpretation is |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2448 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2449 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2450 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2451 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2452 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2453 | if (isConstructorDeclarator()) { |
| 2454 | // The user meant this to be an out-of-line constructor |
| 2455 | // definition, but template arguments are not allowed |
| 2456 | // there. Just allow this as a constructor; we'll |
| 2457 | // complain about it later. |
| 2458 | goto DoneWithDeclSpec; |
| 2459 | } |
| 2460 | |
| 2461 | // The user meant this to name a type, but it actually names |
| 2462 | // a constructor with some extraneous template |
| 2463 | // arguments. Complain, then parse it as a type as the user |
| 2464 | // intended. |
| 2465 | Diag(TemplateId->TemplateNameLoc, |
| 2466 | diag::err_out_of_line_template_id_names_constructor) |
| 2467 | << TemplateId->Name; |
| 2468 | } |
| 2469 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2470 | DS.getTypeSpecScope() = SS; |
| 2471 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2472 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2473 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2474 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2475 | continue; |
| 2476 | } |
| 2477 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2478 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2479 | DS.getTypeSpecScope() = SS; |
| 2480 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2481 | if (Tok.getAnnotationValue()) { |
| 2482 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2483 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2484 | Tok.getAnnotationEndLoc(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2485 | PrevSpec, DiagID, T); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2486 | if (isInvalid) |
| 2487 | break; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2488 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2489 | else |
| 2490 | DS.SetTypeSpecError(); |
| 2491 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2492 | ConsumeToken(); // The typename |
| 2493 | } |
| 2494 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2495 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2496 | goto DoneWithDeclSpec; |
| 2497 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2498 | // If we're in a context where the identifier could be a class name, |
| 2499 | // check whether this is a constructor declaration. |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2500 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2501 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2502 | &SS)) { |
| 2503 | if (isConstructorDeclarator()) |
| 2504 | goto DoneWithDeclSpec; |
| 2505 | |
| 2506 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 2507 | // of the class is qualified in a context where it could name |
| 2508 | // a constructor, its a constructor name. However, we've |
| 2509 | // looked at the declarator, and the user probably meant this |
| 2510 | // to be a type. Complain that it isn't supposed to be treated |
| 2511 | // as a type, then proceed to parse it as a type. |
| 2512 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 2513 | << Next.getIdentifierInfo(); |
| 2514 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2515 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2516 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 2517 | Next.getLocation(), |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2518 | getCurScope(), &SS, |
| 2519 | false, false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2520 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2521 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2522 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2523 | // If the referenced identifier is not a type, then this declspec is |
| 2524 | // erroneous: We already checked about that it has no type specifier, and |
| 2525 | // 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] | 2526 | // typename. |
David Blaikie | 7247c88 | 2013-05-15 07:37:26 +0000 | [diff] [blame] | 2527 | if (!TypeRep) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2528 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2529 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2530 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) { |
| 2531 | if (!Attrs.empty()) { |
| 2532 | AttrsLastTime = true; |
| 2533 | attrs.takeAllFrom(Attrs); |
| 2534 | } |
| 2535 | continue; |
| 2536 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2537 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2538 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2539 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2540 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2541 | ConsumeToken(); // The C++ scope. |
| 2542 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2543 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2544 | DiagID, TypeRep); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2545 | if (isInvalid) |
| 2546 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2547 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2548 | DS.SetRangeEnd(Tok.getLocation()); |
| 2549 | ConsumeToken(); // The typename. |
| 2550 | |
| 2551 | continue; |
| 2552 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2553 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2554 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2555 | if (Tok.getAnnotationValue()) { |
| 2556 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | c43271e | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 2557 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2558 | DiagID, T); |
| 2559 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2560 | DS.SetTypeSpecError(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2561 | |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 2562 | if (isInvalid) |
| 2563 | break; |
| 2564 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2565 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2566 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2567 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2568 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2569 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2570 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2571 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2572 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2573 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2574 | continue; |
| 2575 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2576 | |
Douglas Gregor | bfad915 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2577 | case tok::kw___is_signed: |
| 2578 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 2579 | // typically treats it as a trait. If we see __is_signed as it appears |
| 2580 | // in libstdc++, e.g., |
| 2581 | // |
| 2582 | // static const bool __is_signed; |
| 2583 | // |
| 2584 | // then treat __is_signed as an identifier rather than as a keyword. |
| 2585 | if (DS.getTypeSpecType() == TST_bool && |
| 2586 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
| 2587 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 2588 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
| 2589 | Tok.setKind(tok::identifier); |
| 2590 | } |
| 2591 | |
| 2592 | // We're done with the declaration-specifiers. |
| 2593 | goto DoneWithDeclSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2594 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2595 | // typedef-name |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2596 | case tok::kw_decltype: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2597 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2598 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 2599 | // so handle it as such. This is important for ctor parsing. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2600 | if (getLangOpts().CPlusPlus) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2601 | if (TryAnnotateCXXScopeToken(true)) { |
| 2602 | if (!DS.hasTypeSpecifier()) |
| 2603 | DS.SetTypeSpecError(); |
| 2604 | goto DoneWithDeclSpec; |
| 2605 | } |
| 2606 | if (!Tok.is(tok::identifier)) |
| 2607 | continue; |
| 2608 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2609 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2610 | // This identifier can only be a typedef name if we haven't already seen |
| 2611 | // a type-specifier. Without this check we misparse: |
| 2612 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 2613 | if (DS.hasTypeSpecifier()) |
| 2614 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2615 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2616 | // Check for need to substitute AltiVec keyword tokens. |
| 2617 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2618 | break; |
| 2619 | |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2620 | // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not |
| 2621 | // allow the use of a typedef name as a type specifier. |
| 2622 | if (DS.isTypeAltiVecVector()) |
| 2623 | goto DoneWithDeclSpec; |
| 2624 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2625 | ParsedType TypeRep = |
| 2626 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 2627 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2628 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2629 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 2630 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2631 | if (!TypeRep) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2632 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2633 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) { |
| 2634 | if (!Attrs.empty()) { |
| 2635 | AttrsLastTime = true; |
| 2636 | attrs.takeAllFrom(Attrs); |
| 2637 | } |
| 2638 | continue; |
| 2639 | } |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2640 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2641 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2642 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2643 | // If we're in a context where the identifier could be a class name, |
| 2644 | // check whether this is a constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2645 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2646 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2647 | isConstructorDeclarator()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2648 | goto DoneWithDeclSpec; |
| 2649 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2650 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2651 | DiagID, TypeRep); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2652 | if (isInvalid) |
| 2653 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2654 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2655 | DS.SetRangeEnd(Tok.getLocation()); |
| 2656 | ConsumeToken(); // The identifier |
| 2657 | |
| 2658 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2659 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2660 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2661 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2662 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2663 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2664 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2665 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2666 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2667 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2668 | |
| 2669 | // type-name |
| 2670 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2671 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2672 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2673 | // This template-id does not refer to a type name, so we're |
| 2674 | // done with the type-specifiers. |
| 2675 | goto DoneWithDeclSpec; |
| 2676 | } |
| 2677 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2678 | // If we're in a context where the template-id could be a |
| 2679 | // constructor name or specialization, check whether this is a |
| 2680 | // constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2681 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2682 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2683 | isConstructorDeclarator()) |
| 2684 | goto DoneWithDeclSpec; |
| 2685 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2686 | // Turn the template-id annotation token into a type annotation |
| 2687 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2688 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2689 | continue; |
| 2690 | } |
| 2691 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2692 | // GNU attributes support. |
| 2693 | case tok::kw___attribute: |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2694 | ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2695 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2696 | |
| 2697 | // Microsoft declspec support. |
| 2698 | case tok::kw___declspec: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2699 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2700 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2701 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2702 | // Microsoft single token adornments. |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2703 | case tok::kw___forceinline: { |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2704 | isInvalid = DS.setFunctionSpecInline(Loc); |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2705 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2706 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 2707 | // FIXME: This does not work correctly if it is set to be a declspec |
| 2708 | // attribute, and a GNU attribute is simply incorrect. |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2709 | DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 2710 | SourceLocation(), 0, 0, AttributeList::AS_GNU); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2711 | break; |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2712 | } |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2713 | |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 2714 | case tok::kw___sptr: |
| 2715 | case tok::kw___uptr: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2716 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2717 | case tok::kw___ptr32: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2718 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2719 | case tok::kw___cdecl: |
| 2720 | case tok::kw___stdcall: |
| 2721 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2722 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2723 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2724 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2725 | continue; |
| 2726 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2727 | // Borland single token adornments. |
| 2728 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2729 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2730 | continue; |
| 2731 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2732 | // OpenCL single token adornments. |
| 2733 | case tok::kw___kernel: |
| 2734 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2735 | continue; |
| 2736 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2737 | // storage-class-specifier |
| 2738 | case tok::kw_typedef: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2739 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
| 2740 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2741 | break; |
| 2742 | case tok::kw_extern: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2743 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2744 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2745 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
| 2746 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2747 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2748 | case tok::kw___private_extern__: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2749 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
| 2750 | Loc, PrevSpec, DiagID); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2751 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2752 | case tok::kw_static: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2753 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2754 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2755 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
| 2756 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2757 | break; |
| 2758 | case tok::kw_auto: |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2759 | if (getLangOpts().CPlusPlus11) { |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2760 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2761 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2762 | PrevSpec, DiagID); |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2763 | if (!isInvalid) |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2764 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2765 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2766 | } else |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2767 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 2768 | DiagID); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2769 | } else |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2770 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2771 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2772 | break; |
| 2773 | case tok::kw_register: |
Richard Smith | 6b759f4 | 2013-06-14 21:05:24 +0000 | [diff] [blame] | 2774 | // In C++11, the 'register' storage class specifier is deprecated. |
| 2775 | // Suppress the warning in system macros, it's used in macros in some |
| 2776 | // popular C system headers, such as in glibc's htonl() macro. |
| 2777 | if (getLangOpts().CPlusPlus11 && |
| 2778 | !PP.getSourceManager().isInSystemMacro(Tok.getLocation())) |
Richard Smith | 79f4bb7 | 2013-06-13 02:02:51 +0000 | [diff] [blame] | 2779 | Diag(Tok, diag::warn_deprecated_register) |
| 2780 | << FixItHint::CreateRemoval(Tok.getLocation()); |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2781 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
| 2782 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2783 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2784 | case tok::kw_mutable: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2785 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
| 2786 | PrevSpec, DiagID); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2787 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2788 | case tok::kw___thread: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2789 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc, |
| 2790 | PrevSpec, DiagID); |
| 2791 | break; |
| 2792 | case tok::kw_thread_local: |
| 2793 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc, |
| 2794 | PrevSpec, DiagID); |
| 2795 | break; |
| 2796 | case tok::kw__Thread_local: |
| 2797 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local, |
| 2798 | Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2799 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2800 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2801 | // function-specifier |
| 2802 | case tok::kw_inline: |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2803 | isInvalid = DS.setFunctionSpecInline(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2804 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2805 | case tok::kw_virtual: |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2806 | isInvalid = DS.setFunctionSpecVirtual(Loc); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2807 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2808 | case tok::kw_explicit: |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2809 | isInvalid = DS.setFunctionSpecExplicit(Loc); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2810 | break; |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 2811 | case tok::kw__Noreturn: |
| 2812 | if (!getLangOpts().C11) |
| 2813 | Diag(Loc, diag::ext_c11_noreturn); |
| 2814 | isInvalid = DS.setFunctionSpecNoreturn(Loc); |
| 2815 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2816 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2817 | // alignment-specifier |
| 2818 | case tok::kw__Alignas: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2819 | if (!getLangOpts().C11) |
Jordan Rose | f70a886 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2820 | Diag(Tok, diag::ext_c11_alignment) << Tok.getName(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2821 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2822 | continue; |
| 2823 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2824 | // friend |
| 2825 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2826 | if (DSContext == DSC_class) |
| 2827 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 2828 | else { |
| 2829 | PrevSpec = ""; // not actually used by the diagnostic |
| 2830 | DiagID = diag::err_friend_invalid_in_context; |
| 2831 | isInvalid = true; |
| 2832 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2833 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2834 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2835 | // Modules |
| 2836 | case tok::kw___module_private__: |
| 2837 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 2838 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2839 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2840 | // constexpr |
| 2841 | case tok::kw_constexpr: |
| 2842 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 2843 | break; |
| 2844 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2845 | // type-specifier |
| 2846 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2847 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 2848 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2849 | break; |
| 2850 | case tok::kw_long: |
| 2851 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2852 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2853 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2854 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2855 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2856 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2857 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2858 | case tok::kw___int64: |
| 2859 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2860 | DiagID); |
| 2861 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2862 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2863 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 2864 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2865 | break; |
| 2866 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2867 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2868 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2869 | break; |
| 2870 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2871 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2872 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2873 | break; |
| 2874 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2875 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2876 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2877 | break; |
| 2878 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2879 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 2880 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2881 | break; |
| 2882 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2883 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 2884 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2885 | break; |
| 2886 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2887 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 2888 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2889 | break; |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 2890 | case tok::kw___int128: |
| 2891 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
| 2892 | DiagID); |
| 2893 | break; |
| 2894 | case tok::kw_half: |
| 2895 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
| 2896 | DiagID); |
| 2897 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2898 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2899 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 2900 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2901 | break; |
| 2902 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2903 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 2904 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2905 | break; |
| 2906 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2907 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 2908 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2909 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2910 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2911 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 2912 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2913 | break; |
| 2914 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2915 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 2916 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2917 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2918 | case tok::kw_bool: |
| 2919 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2920 | if (Tok.is(tok::kw_bool) && |
| 2921 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 2922 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 2923 | PrevSpec = ""; // Not used by the diagnostic. |
| 2924 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2925 | // For better error recovery. |
| 2926 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2927 | isInvalid = true; |
| 2928 | } else { |
| 2929 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 2930 | DiagID); |
| 2931 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2932 | break; |
| 2933 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2934 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2935 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2936 | break; |
| 2937 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2938 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2939 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2940 | break; |
| 2941 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2942 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2943 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2944 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2945 | case tok::kw___vector: |
| 2946 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2947 | break; |
| 2948 | case tok::kw___pixel: |
| 2949 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2950 | break; |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 2951 | case tok::kw_image1d_t: |
| 2952 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc, |
| 2953 | PrevSpec, DiagID); |
| 2954 | break; |
| 2955 | case tok::kw_image1d_array_t: |
| 2956 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc, |
| 2957 | PrevSpec, DiagID); |
| 2958 | break; |
| 2959 | case tok::kw_image1d_buffer_t: |
| 2960 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc, |
| 2961 | PrevSpec, DiagID); |
| 2962 | break; |
| 2963 | case tok::kw_image2d_t: |
| 2964 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc, |
| 2965 | PrevSpec, DiagID); |
| 2966 | break; |
| 2967 | case tok::kw_image2d_array_t: |
| 2968 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc, |
| 2969 | PrevSpec, DiagID); |
| 2970 | break; |
| 2971 | case tok::kw_image3d_t: |
| 2972 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc, |
| 2973 | PrevSpec, DiagID); |
| 2974 | break; |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2975 | case tok::kw_sampler_t: |
| 2976 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc, |
| 2977 | PrevSpec, DiagID); |
| 2978 | break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2979 | case tok::kw_event_t: |
| 2980 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc, |
| 2981 | PrevSpec, DiagID); |
| 2982 | break; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 2983 | case tok::kw___unknown_anytype: |
| 2984 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 2985 | PrevSpec, DiagID); |
| 2986 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2987 | |
| 2988 | // class-specifier: |
| 2989 | case tok::kw_class: |
| 2990 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2991 | case tok::kw___interface: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2992 | case tok::kw_union: { |
| 2993 | tok::TokenKind Kind = Tok.getKind(); |
| 2994 | ConsumeToken(); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2995 | |
| 2996 | // These are attributes following class specifiers. |
| 2997 | // To produce better diagnostic, we parse them when |
| 2998 | // parsing class specifier. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2999 | ParsedAttributesWithRange Attributes(AttrFactory); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3000 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3001 | EnteringContext, DSContext, Attributes); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3002 | |
| 3003 | // If there are attributes following class specifier, |
| 3004 | // take them over and handle them here. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3005 | if (!Attributes.empty()) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3006 | AttrsLastTime = true; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3007 | attrs.takeAllFrom(Attributes); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 3008 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3009 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3010 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3011 | |
| 3012 | // enum-specifier: |
| 3013 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3014 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3015 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3016 | continue; |
| 3017 | |
| 3018 | // cv-qualifier: |
| 3019 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3020 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3021 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3022 | break; |
| 3023 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3024 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3025 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3026 | break; |
| 3027 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3028 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3029 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3030 | break; |
| 3031 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3032 | // C++ typename-specifier: |
| 3033 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3034 | if (TryAnnotateTypeOrScopeToken()) { |
| 3035 | DS.SetTypeSpecError(); |
| 3036 | goto DoneWithDeclSpec; |
| 3037 | } |
| 3038 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3039 | continue; |
| 3040 | break; |
| 3041 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3042 | // GNU typeof support. |
| 3043 | case tok::kw_typeof: |
| 3044 | ParseTypeofSpecifier(DS); |
| 3045 | continue; |
| 3046 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3047 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 3048 | ParseDecltypeSpecifier(DS); |
| 3049 | continue; |
| 3050 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3051 | case tok::kw___underlying_type: |
| 3052 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3053 | continue; |
| 3054 | |
| 3055 | case tok::kw__Atomic: |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3056 | // C11 6.7.2.4/4: |
| 3057 | // If the _Atomic keyword is immediately followed by a left parenthesis, |
| 3058 | // it is interpreted as a type specifier (with a type name), not as a |
| 3059 | // type qualifier. |
| 3060 | if (NextToken().is(tok::l_paren)) { |
| 3061 | ParseAtomicSpecifier(DS); |
| 3062 | continue; |
| 3063 | } |
| 3064 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 3065 | getLangOpts()); |
| 3066 | break; |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3067 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3068 | // OpenCL qualifiers: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3069 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3070 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3071 | goto DoneWithDeclSpec; |
| 3072 | case tok::kw___private: |
| 3073 | case tok::kw___global: |
| 3074 | case tok::kw___local: |
| 3075 | case tok::kw___constant: |
| 3076 | case tok::kw___read_only: |
| 3077 | case tok::kw___write_only: |
| 3078 | case tok::kw___read_write: |
| 3079 | ParseOpenCLQualifiers(DS); |
| 3080 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3081 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 3082 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3083 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3084 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 3085 | // but we support it. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3086 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3087 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3088 | |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 3089 | if (!ParseObjCProtocolQualifiers(DS)) |
| 3090 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 3091 | << FixItHint::CreateInsertion(Loc, "id") |
| 3092 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3093 | |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 3094 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 3095 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 3096 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3097 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3098 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3099 | if (isInvalid) { |
| 3100 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3101 | assert(DiagID); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3102 | |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 3103 | if (DiagID == diag::ext_duplicate_declspec) |
| 3104 | Diag(Tok, DiagID) |
| 3105 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 3106 | else |
| 3107 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3108 | } |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3109 | |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 3110 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3111 | if (DiagID != diag::err_bool_redeclaration) |
| 3112 | ConsumeToken(); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3113 | |
| 3114 | AttrsLastTime = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3115 | } |
| 3116 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 3117 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3118 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 3119 | /// semicolon. |
| 3120 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3121 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3122 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3123 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3124 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3125 | /// struct-declarator-list: |
| 3126 | /// struct-declarator |
| 3127 | /// struct-declarator-list ',' struct-declarator |
| 3128 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 3129 | /// struct-declarator: |
| 3130 | /// declarator |
| 3131 | /// [GNU] declarator attributes[opt] |
| 3132 | /// declarator[opt] ':' constant-expression |
| 3133 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 3134 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3135 | void Parser:: |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3136 | ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3137 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3138 | if (Tok.is(tok::kw___extension__)) { |
| 3139 | // __extension__ silences extension warnings in the subexpression. |
| 3140 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3141 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3142 | return ParseStructDeclaration(DS, Fields); |
| 3143 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3144 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3145 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3146 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3147 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3148 | // If there are no declarators, this is a free-standing declaration |
| 3149 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3150 | if (Tok.is(tok::semi)) { |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3151 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
| 3152 | DS); |
| 3153 | DS.complete(TheDecl); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3154 | return; |
| 3155 | } |
| 3156 | |
| 3157 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3158 | bool FirstDeclarator = true; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3159 | SourceLocation CommaLoc; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3160 | while (1) { |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3161 | ParsingFieldDeclarator DeclaratorInfo(*this, DS); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3162 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3163 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3164 | // Attributes are only allowed here on successive declarators. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3165 | if (!FirstDeclarator) |
| 3166 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3167 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3168 | /// struct-declarator: declarator |
| 3169 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3170 | if (Tok.isNot(tok::colon)) { |
| 3171 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 3172 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3173 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3174 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3175 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3176 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3177 | ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3178 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3179 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3180 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 3181 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 3182 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3183 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3184 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3185 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3186 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3187 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3188 | // We're done with this declarator; invoke the callback. |
Eli Friedman | 817a886 | 2012-08-08 23:35:12 +0000 | [diff] [blame] | 3189 | Fields.invoke(DeclaratorInfo); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3190 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3191 | // If we don't have a comma, it is either the end of the list (a ';') |
| 3192 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3193 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3194 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3195 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3196 | // Consume the comma. |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3197 | CommaLoc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3198 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3199 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3200 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3201 | } |
| 3202 | |
| 3203 | /// ParseStructUnionBody |
| 3204 | /// struct-contents: |
| 3205 | /// struct-declaration-list |
| 3206 | /// [EXT] empty |
| 3207 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 3208 | /// struct-declaration-list: |
| 3209 | /// struct-declaration |
| 3210 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3211 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3212 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3213 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3214 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3215 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 3216 | "parsing struct/union body"); |
Andy Gibbs | f50f3f7 | 2013-04-03 09:31:19 +0000 | [diff] [blame] | 3217 | assert(!getLangOpts().CPlusPlus && "C++ declarations not supported"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3218 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3219 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3220 | if (T.consumeOpen()) |
| 3221 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3222 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3223 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3224 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3225 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3226 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3227 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3228 | // 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] | 3229 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3230 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3231 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3232 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3233 | if (Tok.is(tok::semi)) { |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3234 | ConsumeExtraSemi(InsideStruct, TagType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3235 | continue; |
| 3236 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3237 | |
Andy Gibbs | 74b9fa1 | 2013-04-03 09:46:04 +0000 | [diff] [blame] | 3238 | // Parse _Static_assert declaration. |
| 3239 | if (Tok.is(tok::kw__Static_assert)) { |
| 3240 | SourceLocation DeclEnd; |
| 3241 | ParseStaticAssertDeclaration(DeclEnd); |
| 3242 | continue; |
| 3243 | } |
| 3244 | |
Argyrios Kyrtzidis | bd95745 | 2013-04-18 01:42:35 +0000 | [diff] [blame] | 3245 | if (Tok.is(tok::annot_pragma_pack)) { |
| 3246 | HandlePragmaPack(); |
| 3247 | continue; |
| 3248 | } |
| 3249 | |
| 3250 | if (Tok.is(tok::annot_pragma_align)) { |
| 3251 | HandlePragmaAlign(); |
| 3252 | continue; |
| 3253 | } |
| 3254 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3255 | if (!Tok.is(tok::at)) { |
| 3256 | struct CFieldCallback : FieldCallback { |
| 3257 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3258 | Decl *TagDecl; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3259 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3260 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3261 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3262 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3263 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 3264 | |
Eli Friedman | dcdff46 | 2012-08-08 23:53:27 +0000 | [diff] [blame] | 3265 | void invoke(ParsingFieldDeclarator &FD) { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3266 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3267 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 3268 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 3269 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3270 | FieldDecls.push_back(Field); |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3271 | FD.complete(Field); |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3272 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3273 | } Callback(*this, TagDecl, FieldDecls); |
| 3274 | |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3275 | // Parse all the comma separated declarators. |
| 3276 | ParsingDeclSpec DS(*this); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3277 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3278 | } else { // Handle @defs |
| 3279 | ConsumeToken(); |
| 3280 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 3281 | Diag(Tok, diag::err_unexpected_at); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3282 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3283 | continue; |
| 3284 | } |
| 3285 | ConsumeToken(); |
| 3286 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 3287 | if (!Tok.is(tok::identifier)) { |
| 3288 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3289 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3290 | continue; |
| 3291 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3292 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3293 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3294 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3295 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 3296 | ConsumeToken(); |
| 3297 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3298 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3299 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3300 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3301 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3302 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3303 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3304 | break; |
| 3305 | } else { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3306 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 3307 | // 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] | 3308 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3309 | // If we stopped at a ';', eat it. |
| 3310 | if (Tok.is(tok::semi)) ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3311 | } |
| 3312 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3313 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3314 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3315 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3316 | ParsedAttributes attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3317 | // If attributes exist after struct contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3318 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 3319 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3320 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 3321 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3322 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3323 | attrs.getList()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3324 | StructScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3325 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 3326 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3329 | /// ParseEnumSpecifier |
| 3330 | /// enum-specifier: [C99 6.7.2.2] |
| 3331 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3332 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3333 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3334 | /// '}' attributes[opt] |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 3335 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3336 | /// '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3337 | /// 'enum' identifier |
| 3338 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3339 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3340 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 3341 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3342 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3343 | /// enum-head: [C++11] |
| 3344 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 3345 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 3346 | /// identifier enum-base[opt] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3347 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3348 | /// enum-key: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3349 | /// 'enum' |
| 3350 | /// 'enum' 'class' |
| 3351 | /// 'enum' 'struct' |
| 3352 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3353 | /// enum-base: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3354 | /// ':' type-specifier-seq |
| 3355 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3356 | /// [C++] elaborated-type-specifier: |
| 3357 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 3358 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3359 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 3360 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3361 | AccessSpecifier AS, DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3362 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3363 | if (Tok.is(tok::code_completion)) { |
| 3364 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3365 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3366 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3367 | } |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3368 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3369 | // If attributes exist after tag, parse them. |
| 3370 | ParsedAttributesWithRange attrs(AttrFactory); |
| 3371 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3372 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3373 | |
| 3374 | // If declspecs exist after tag, parse them. |
| 3375 | while (Tok.is(tok::kw___declspec)) |
| 3376 | ParseMicrosoftDeclSpec(attrs); |
| 3377 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3378 | SourceLocation ScopedEnumKWLoc; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3379 | bool IsScopedUsingClassTag = false; |
| 3380 | |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3381 | // In C++11, recognize 'enum class' and 'enum struct'. |
Richard Trieu | ed5a292 | 2013-04-23 02:47:36 +0000 | [diff] [blame] | 3382 | if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) { |
| 3383 | Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum |
| 3384 | : diag::ext_scoped_enum); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3385 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3386 | ScopedEnumKWLoc = ConsumeToken(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3387 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3388 | // Attributes are not allowed between these keywords. Diagnose, |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3389 | // but then just treat them like they appeared in the right place. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3390 | ProhibitAttributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3391 | |
| 3392 | // They are allowed afterwards, though. |
| 3393 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3394 | MaybeParseCXX11Attributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3395 | while (Tok.is(tok::kw___declspec)) |
| 3396 | ParseMicrosoftDeclSpec(attrs); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3397 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3398 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3399 | // C++11 [temp.explicit]p12: |
| 3400 | // The usual access controls do not apply to names used to specify |
| 3401 | // explicit instantiations. |
| 3402 | // We extend this to also cover explicit specializations. Note that |
| 3403 | // we don't suppress if this turns out to be an elaborated type |
| 3404 | // specifier. |
| 3405 | bool shouldDelayDiagsInTag = |
| 3406 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 3407 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 3408 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3409 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3410 | // Enum definitions should not be parsed in a trailing-return-type. |
| 3411 | bool AllowDeclaration = DSC != DSC_trailing; |
| 3412 | |
| 3413 | bool AllowFixedUnderlyingType = AllowDeclaration && |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3414 | (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt || |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3415 | getLangOpts().ObjC2); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3416 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3417 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3418 | if (getLangOpts().CPlusPlus) { |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3419 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 3420 | // if a fixed underlying type is allowed. |
| 3421 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3422 | |
| 3423 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Richard Smith | 725fe0e | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 3424 | /*EnteringContext=*/true)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3425 | return; |
| 3426 | |
| 3427 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3428 | Diag(Tok, diag::err_expected_ident); |
| 3429 | if (Tok.isNot(tok::l_brace)) { |
| 3430 | // Has no name and is not a definition. |
| 3431 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3432 | SkipUntil(tok::comma, true); |
| 3433 | return; |
| 3434 | } |
| 3435 | } |
| 3436 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3437 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3438 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3439 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3440 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3441 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3442 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3443 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3444 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3445 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3446 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3447 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3448 | // If an identifier is present, consume and remember it. |
| 3449 | IdentifierInfo *Name = 0; |
| 3450 | SourceLocation NameLoc; |
| 3451 | if (Tok.is(tok::identifier)) { |
| 3452 | Name = Tok.getIdentifierInfo(); |
| 3453 | NameLoc = ConsumeToken(); |
| 3454 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3455 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3456 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3457 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 3458 | // declaration of a scoped enumeration. |
| 3459 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3460 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3461 | IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3462 | } |
| 3463 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3464 | // Okay, end the suppression area. We'll decide whether to emit the |
| 3465 | // diagnostics in a second. |
| 3466 | if (shouldDelayDiagsInTag) |
| 3467 | diagsFromTag.done(); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3468 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3469 | TypeResult BaseType; |
| 3470 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3471 | // Parse the fixed underlying type. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3472 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3473 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3474 | bool PossibleBitfield = false; |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3475 | if (CanBeBitfield) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3476 | // If we're in class scope, this can either be an enum declaration with |
| 3477 | // an underlying type, or a declaration of a bitfield member. We try to |
| 3478 | // use a simple disambiguation scheme first to catch the common cases |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3479 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 3480 | // anything that's a simple-type-specifier followed by '(' as an |
| 3481 | // expression. This suffices because function types are not valid |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3482 | // underlying types anyway. |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 3483 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 3484 | Sema::ConstantEvaluated); |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3485 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3486 | // 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] | 3487 | // bit-field. This is the common case. |
| 3488 | if (TPR == TPResult::True()) |
| 3489 | PossibleBitfield = true; |
| 3490 | // If the next token starts a type-specifier-seq, it may be either a |
| 3491 | // 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] | 3492 | // 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] | 3493 | // fixed underlying type. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3494 | else if (TPR == TPResult::False() && |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3495 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 3496 | // Consume the ':'. |
| 3497 | ConsumeToken(); |
| 3498 | } else { |
| 3499 | // We have the start of a type-specifier-seq, so we have to perform |
| 3500 | // tentative parsing to determine whether we have an expression or a |
| 3501 | // type. |
| 3502 | TentativeParsingAction TPA(*this); |
| 3503 | |
| 3504 | // Consume the ':'. |
| 3505 | ConsumeToken(); |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3506 | |
| 3507 | // If we see a type specifier followed by an open-brace, we have an |
| 3508 | // ambiguity between an underlying type and a C++11 braced |
| 3509 | // function-style cast. Resolve this by always treating it as an |
| 3510 | // underlying type. |
| 3511 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 3512 | // this case. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3513 | if ((getLangOpts().CPlusPlus && |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3514 | isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3515 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3516 | // We'll parse this as a bitfield later. |
| 3517 | PossibleBitfield = true; |
| 3518 | TPA.Revert(); |
| 3519 | } else { |
| 3520 | // We have a type-specifier-seq. |
| 3521 | TPA.Commit(); |
| 3522 | } |
| 3523 | } |
| 3524 | } else { |
| 3525 | // Consume the ':'. |
| 3526 | ConsumeToken(); |
| 3527 | } |
| 3528 | |
| 3529 | if (!PossibleBitfield) { |
| 3530 | SourceRange Range; |
| 3531 | BaseType = ParseTypeName(&Range); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3532 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3533 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3534 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Eli Friedman | cef3a7b | 2012-11-02 01:34:28 +0000 | [diff] [blame] | 3535 | } else if (!getLangOpts().ObjC2) { |
| 3536 | if (getLangOpts().CPlusPlus) |
| 3537 | Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range; |
| 3538 | else |
| 3539 | Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range; |
| 3540 | } |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3541 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3542 | } |
| 3543 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3544 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 3545 | // friend declaration, and cannot have an accompanying definition. If we have |
| 3546 | // 'enum foo;', then this is a forward declaration. If we have |
| 3547 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 3548 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3549 | // |
| 3550 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 3551 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 3552 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 3553 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3554 | Sema::TagUseKind TUK; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3555 | if (!AllowDeclaration) { |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3556 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3557 | } else if (Tok.is(tok::l_brace)) { |
| 3558 | if (DS.isFriendSpecified()) { |
| 3559 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
| 3560 | << SourceRange(DS.getFriendSpecLoc()); |
| 3561 | ConsumeBrace(); |
| 3562 | SkipUntil(tok::r_brace); |
| 3563 | TUK = Sema::TUK_Friend; |
| 3564 | } else { |
| 3565 | TUK = Sema::TUK_Definition; |
| 3566 | } |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3567 | } else if (DSC != DSC_type_specifier && |
| 3568 | (Tok.is(tok::semi) || |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3569 | (Tok.isAtStartOfLine() && |
| 3570 | !isValidAfterTypeSpecifier(CanBeBitfield)))) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3571 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
| 3572 | if (Tok.isNot(tok::semi)) { |
| 3573 | // A semicolon was missing after this declaration. Diagnose and recover. |
| 3574 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, |
| 3575 | "enum"); |
| 3576 | PP.EnterToken(Tok); |
| 3577 | Tok.setKind(tok::semi); |
| 3578 | } |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3579 | } else { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3580 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3581 | } |
| 3582 | |
| 3583 | // If this is an elaborated type specifier, and we delayed |
| 3584 | // diagnostics before, just merge them into the current pool. |
| 3585 | if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) { |
| 3586 | diagsFromTag.redelay(); |
| 3587 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3588 | |
| 3589 | MultiTemplateParamsArg TParams; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3590 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3591 | TUK != Sema::TUK_Reference) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3592 | if (!getLangOpts().CPlusPlus11 || !SS.isSet()) { |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3593 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3594 | Diag(Tok, diag::err_enum_template); |
| 3595 | SkipUntil(tok::comma, true); |
| 3596 | return; |
| 3597 | } |
| 3598 | |
| 3599 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 3600 | // Enumerations can't be explicitly instantiated. |
| 3601 | DS.SetTypeSpecError(); |
| 3602 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 3603 | return; |
| 3604 | } |
| 3605 | |
| 3606 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 3607 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 3608 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3609 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3610 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3611 | if (TUK == Sema::TUK_Reference) |
| 3612 | ProhibitAttributes(attrs); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3613 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3614 | if (!Name && TUK != Sema::TUK_Definition) { |
| 3615 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3616 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3617 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3618 | SkipUntil(tok::comma, true); |
| 3619 | return; |
| 3620 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3621 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3622 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3623 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3624 | const char *PrevSpec = 0; |
| 3625 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3626 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3627 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3628 | AS, DS.getModulePrivateSpecLoc(), TParams, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3629 | Owned, IsDependent, ScopedEnumKWLoc, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3630 | IsScopedUsingClassTag, BaseType); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3631 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3632 | if (IsDependent) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3633 | // This enum has a dependent nested-name-specifier. Handle it as a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3634 | // dependent tag. |
| 3635 | if (!Name) { |
| 3636 | DS.SetTypeSpecError(); |
| 3637 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 3638 | return; |
| 3639 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3640 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3641 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3642 | TUK, SS, Name, StartLoc, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3643 | NameLoc); |
| 3644 | if (Type.isInvalid()) { |
| 3645 | DS.SetTypeSpecError(); |
| 3646 | return; |
| 3647 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3648 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3649 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 3650 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3651 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3652 | Diag(StartLoc, DiagID) << PrevSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3653 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3654 | return; |
| 3655 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3656 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3657 | if (!TagDecl) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3658 | // The action failed to produce an enumeration tag. If this is a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3659 | // definition, consume the entire definition. |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3660 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3661 | ConsumeBrace(); |
| 3662 | SkipUntil(tok::r_brace); |
| 3663 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3664 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3665 | DS.SetTypeSpecError(); |
| 3666 | return; |
| 3667 | } |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3668 | |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3669 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3670 | ParseEnumBody(StartLoc, TagDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3671 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3672 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3673 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3674 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3675 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3676 | } |
| 3677 | |
| 3678 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3679 | /// enumerator-list: |
| 3680 | /// enumerator |
| 3681 | /// enumerator-list ',' enumerator |
| 3682 | /// enumerator: |
| 3683 | /// enumeration-constant |
| 3684 | /// enumeration-constant '=' constant-expression |
| 3685 | /// enumeration-constant: |
| 3686 | /// identifier |
| 3687 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3688 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3689 | // Enter the scope of the enum body and start the definition. |
| 3690 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3691 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3692 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3693 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3694 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3695 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3696 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3697 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3698 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3699 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3700 | SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3701 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3702 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3703 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3704 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3705 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3706 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3707 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3708 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3709 | // If attributes exist after the enumerator, parse them. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3710 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3711 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3712 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3713 | ProhibitAttributes(attrs); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3714 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3715 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3716 | ExprResult AssignedVal; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 3717 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3718 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3719 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3720 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3721 | AssignedVal = ParseConstantExpression(); |
| 3722 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3723 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3724 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3725 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3726 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3727 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3728 | LastEnumConstDecl, |
| 3729 | IdentLoc, Ident, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3730 | attrs.getList(), EqualLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3731 | AssignedVal.release()); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3732 | PD.complete(EnumConstDecl); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3733 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3734 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3735 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3736 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3737 | if (Tok.is(tok::identifier)) { |
| 3738 | // We're missing a comma between enumerators. |
| 3739 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3740 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3741 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3742 | continue; |
| 3743 | } |
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.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3746 | break; |
| 3747 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3748 | |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3749 | if (Tok.isNot(tok::identifier)) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3750 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11) |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3751 | Diag(CommaLoc, getLangOpts().CPlusPlus ? |
| 3752 | diag::ext_enumerator_list_comma_cxx : |
| 3753 | diag::ext_enumerator_list_comma_c) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3754 | << FixItHint::CreateRemoval(CommaLoc); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3755 | else if (getLangOpts().CPlusPlus11) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3756 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3757 | << FixItHint::CreateRemoval(CommaLoc); |
| 3758 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3759 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3760 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3761 | // Eat the }. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3762 | T.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3763 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3764 | // If attributes exist after the identifier list, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3765 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3766 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3767 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3768 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
Dmitri Gribenko | 9ff2b42 | 2013-04-27 20:23:52 +0000 | [diff] [blame] | 3769 | EnumDecl, EnumConstantDecls, |
| 3770 | getCurScope(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3771 | attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3772 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3773 | EnumScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3774 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3775 | T.getCloseLocation()); |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3776 | |
| 3777 | // The next token must be valid after an enum definition. If not, a ';' |
| 3778 | // was probably forgotten. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3779 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
| 3780 | if (!isValidAfterTypeSpecifier(CanBeBitfield)) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3781 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum"); |
| 3782 | // Push this token back into the preprocessor and change our current token |
| 3783 | // to ';' so that the rest of the code recovers as though there were an |
| 3784 | // ';' after the definition. |
| 3785 | PP.EnterToken(Tok); |
| 3786 | Tok.setKind(tok::semi); |
| 3787 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3788 | } |
| 3789 | |
| 3790 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3791 | /// start of a type-qualifier-list. |
| 3792 | bool Parser::isTypeQualifier() const { |
| 3793 | switch (Tok.getKind()) { |
| 3794 | default: return false; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3795 | |
| 3796 | // type-qualifier only in OpenCL |
| 3797 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3798 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3799 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3800 | // type-qualifier |
| 3801 | case tok::kw_const: |
| 3802 | case tok::kw_volatile: |
| 3803 | case tok::kw_restrict: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3804 | case tok::kw___private: |
| 3805 | case tok::kw___local: |
| 3806 | case tok::kw___global: |
| 3807 | case tok::kw___constant: |
| 3808 | case tok::kw___read_only: |
| 3809 | case tok::kw___read_write: |
| 3810 | case tok::kw___write_only: |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3811 | return true; |
| 3812 | } |
| 3813 | } |
| 3814 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3815 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3816 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3817 | /// specifier or if we're not sure. |
| 3818 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3819 | switch (Tok.getKind()) { |
| 3820 | default: return false; |
| 3821 | // type-specifiers |
| 3822 | case tok::kw_short: |
| 3823 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3824 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3825 | case tok::kw___int128: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3826 | case tok::kw_signed: |
| 3827 | case tok::kw_unsigned: |
| 3828 | case tok::kw__Complex: |
| 3829 | case tok::kw__Imaginary: |
| 3830 | case tok::kw_void: |
| 3831 | case tok::kw_char: |
| 3832 | case tok::kw_wchar_t: |
| 3833 | case tok::kw_char16_t: |
| 3834 | case tok::kw_char32_t: |
| 3835 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3836 | case tok::kw_half: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3837 | case tok::kw_float: |
| 3838 | case tok::kw_double: |
| 3839 | case tok::kw_bool: |
| 3840 | case tok::kw__Bool: |
| 3841 | case tok::kw__Decimal32: |
| 3842 | case tok::kw__Decimal64: |
| 3843 | case tok::kw__Decimal128: |
| 3844 | case tok::kw___vector: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3845 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3846 | // OpenCL specific types: |
| 3847 | case tok::kw_image1d_t: |
| 3848 | case tok::kw_image1d_array_t: |
| 3849 | case tok::kw_image1d_buffer_t: |
| 3850 | case tok::kw_image2d_t: |
| 3851 | case tok::kw_image2d_array_t: |
| 3852 | case tok::kw_image3d_t: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3853 | case tok::kw_sampler_t: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3854 | case tok::kw_event_t: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3855 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3856 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3857 | case tok::kw_class: |
| 3858 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3859 | case tok::kw___interface: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3860 | case tok::kw_union: |
| 3861 | // enum-specifier |
| 3862 | case tok::kw_enum: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3863 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3864 | // typedef-name |
| 3865 | case tok::annot_typename: |
| 3866 | return true; |
| 3867 | } |
| 3868 | } |
| 3869 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3870 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3871 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3872 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3873 | switch (Tok.getKind()) { |
| 3874 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3875 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3876 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3877 | if (TryAltiVecVectorToken()) |
| 3878 | return true; |
| 3879 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3880 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3881 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3882 | // recurse to handle whatever we get. |
| 3883 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3884 | return true; |
| 3885 | if (Tok.is(tok::identifier)) |
| 3886 | return false; |
| 3887 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3888 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3889 | case tok::coloncolon: // ::foo::bar |
| 3890 | if (NextToken().is(tok::kw_new) || // ::new |
| 3891 | NextToken().is(tok::kw_delete)) // ::delete |
| 3892 | return false; |
| 3893 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3894 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3895 | return true; |
| 3896 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3897 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3898 | // GNU attributes support. |
| 3899 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3900 | // GNU typeof support. |
| 3901 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3902 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3903 | // type-specifiers |
| 3904 | case tok::kw_short: |
| 3905 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3906 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3907 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3908 | case tok::kw_signed: |
| 3909 | case tok::kw_unsigned: |
| 3910 | case tok::kw__Complex: |
| 3911 | case tok::kw__Imaginary: |
| 3912 | case tok::kw_void: |
| 3913 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3914 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3915 | case tok::kw_char16_t: |
| 3916 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3917 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3918 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3919 | case tok::kw_float: |
| 3920 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3921 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3922 | case tok::kw__Bool: |
| 3923 | case tok::kw__Decimal32: |
| 3924 | case tok::kw__Decimal64: |
| 3925 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3926 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3927 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3928 | // OpenCL specific types: |
| 3929 | case tok::kw_image1d_t: |
| 3930 | case tok::kw_image1d_array_t: |
| 3931 | case tok::kw_image1d_buffer_t: |
| 3932 | case tok::kw_image2d_t: |
| 3933 | case tok::kw_image2d_array_t: |
| 3934 | case tok::kw_image3d_t: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3935 | case tok::kw_sampler_t: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3936 | case tok::kw_event_t: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3937 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3938 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3939 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3940 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3941 | case tok::kw___interface: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3942 | case tok::kw_union: |
| 3943 | // enum-specifier |
| 3944 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3945 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3946 | // type-qualifier |
| 3947 | case tok::kw_const: |
| 3948 | case tok::kw_volatile: |
| 3949 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3950 | |
John McCall | b8a8de3 | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 3951 | // Debugger support. |
| 3952 | case tok::kw___unknown_anytype: |
| 3953 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3954 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 3955 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3956 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3957 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 3958 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3959 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3960 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3961 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3962 | case tok::kw___cdecl: |
| 3963 | case tok::kw___stdcall: |
| 3964 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3965 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3966 | case tok::kw___w64: |
| 3967 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3968 | case tok::kw___ptr32: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3969 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3970 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3971 | |
| 3972 | case tok::kw___private: |
| 3973 | case tok::kw___local: |
| 3974 | case tok::kw___global: |
| 3975 | case tok::kw___constant: |
| 3976 | case tok::kw___read_only: |
| 3977 | case tok::kw___read_write: |
| 3978 | case tok::kw___write_only: |
| 3979 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3980 | return true; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3981 | |
| 3982 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3983 | return getLangOpts().OpenCL; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3984 | |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3985 | // C11 _Atomic |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3986 | case tok::kw__Atomic: |
| 3987 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3988 | } |
| 3989 | } |
| 3990 | |
| 3991 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 3992 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3993 | /// |
| 3994 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 3995 | /// this check is to disambiguate between an expression and a declaration. |
| 3996 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3997 | switch (Tok.getKind()) { |
| 3998 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3999 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4000 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4001 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4002 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4003 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4004 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4005 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 4006 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4007 | if (TryAltiVecVectorToken()) |
| 4008 | return true; |
| 4009 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4010 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 4011 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4012 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4013 | // recurse to handle whatever we get. |
| 4014 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4015 | return true; |
| 4016 | if (Tok.is(tok::identifier)) |
| 4017 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4018 | |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4019 | // 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] | 4020 | // by an identifier and then either ':' or ']', in a place where an |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4021 | // expression is permitted, then this is probably a class message send |
| 4022 | // missing the initial '['. In this case, we won't consider this to be |
| 4023 | // the start of a declaration. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4024 | if (DisambiguatingWithExpression && |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4025 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 4026 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4027 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4028 | return isDeclarationSpecifier(); |
| 4029 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4030 | case tok::coloncolon: // ::foo::bar |
| 4031 | if (NextToken().is(tok::kw_new) || // ::new |
| 4032 | NextToken().is(tok::kw_delete)) // ::delete |
| 4033 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4034 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4035 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4036 | // recurse to handle whatever we get. |
| 4037 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4038 | return true; |
| 4039 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4040 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4041 | // storage-class-specifier |
| 4042 | case tok::kw_typedef: |
| 4043 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 4044 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4045 | case tok::kw_static: |
| 4046 | case tok::kw_auto: |
| 4047 | case tok::kw_register: |
| 4048 | case tok::kw___thread: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4049 | case tok::kw_thread_local: |
| 4050 | case tok::kw__Thread_local: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4051 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 4052 | // Modules |
| 4053 | case tok::kw___module_private__: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4054 | |
John McCall | b8a8de3 | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4055 | // Debugger support |
| 4056 | case tok::kw___unknown_anytype: |
| 4057 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4058 | // type-specifiers |
| 4059 | case tok::kw_short: |
| 4060 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4061 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4062 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4063 | case tok::kw_signed: |
| 4064 | case tok::kw_unsigned: |
| 4065 | case tok::kw__Complex: |
| 4066 | case tok::kw__Imaginary: |
| 4067 | case tok::kw_void: |
| 4068 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4069 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4070 | case tok::kw_char16_t: |
| 4071 | case tok::kw_char32_t: |
| 4072 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4073 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4074 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4075 | case tok::kw_float: |
| 4076 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4077 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4078 | case tok::kw__Bool: |
| 4079 | case tok::kw__Decimal32: |
| 4080 | case tok::kw__Decimal64: |
| 4081 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4082 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4083 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 4084 | // OpenCL specific types: |
| 4085 | case tok::kw_image1d_t: |
| 4086 | case tok::kw_image1d_array_t: |
| 4087 | case tok::kw_image1d_buffer_t: |
| 4088 | case tok::kw_image2d_t: |
| 4089 | case tok::kw_image2d_array_t: |
| 4090 | case tok::kw_image3d_t: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4091 | case tok::kw_sampler_t: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4092 | case tok::kw_event_t: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 4093 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4094 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4095 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4096 | case tok::kw_struct: |
| 4097 | case tok::kw_union: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4098 | case tok::kw___interface: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4099 | // enum-specifier |
| 4100 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4101 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4102 | // type-qualifier |
| 4103 | case tok::kw_const: |
| 4104 | case tok::kw_volatile: |
| 4105 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4106 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4107 | // function-specifier |
| 4108 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4109 | case tok::kw_virtual: |
| 4110 | case tok::kw_explicit: |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4111 | case tok::kw__Noreturn: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4112 | |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 4113 | // alignment-specifier |
| 4114 | case tok::kw__Alignas: |
| 4115 | |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4116 | // friend keyword. |
| 4117 | case tok::kw_friend: |
| 4118 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 4119 | // static_assert-declaration |
| 4120 | case tok::kw__Static_assert: |
| 4121 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4122 | // GNU typeof support. |
| 4123 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4124 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4125 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4126 | case tok::kw___attribute: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4127 | |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4128 | // C++11 decltype and constexpr. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4129 | case tok::annot_decltype: |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4130 | case tok::kw_constexpr: |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 4131 | |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4132 | // C11 _Atomic |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4133 | case tok::kw__Atomic: |
| 4134 | return true; |
| 4135 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 4136 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4137 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4138 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4139 | |
Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 4140 | // typedef-name |
| 4141 | case tok::annot_typename: |
| 4142 | return !DisambiguatingWithExpression || |
| 4143 | !isStartOfObjCClassMessageMissingOpenBracket(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4144 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 4145 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4146 | case tok::kw___cdecl: |
| 4147 | case tok::kw___stdcall: |
| 4148 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4149 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4150 | case tok::kw___w64: |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4151 | case tok::kw___sptr: |
| 4152 | case tok::kw___uptr: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4153 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4154 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4155 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4156 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4157 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4158 | |
| 4159 | case tok::kw___private: |
| 4160 | case tok::kw___local: |
| 4161 | case tok::kw___global: |
| 4162 | case tok::kw___constant: |
| 4163 | case tok::kw___read_only: |
| 4164 | case tok::kw___read_write: |
| 4165 | case tok::kw___write_only: |
| 4166 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4167 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4168 | } |
| 4169 | } |
| 4170 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4171 | bool Parser::isConstructorDeclarator() { |
| 4172 | TentativeParsingAction TPA(*this); |
| 4173 | |
| 4174 | // Parse the C++ scope specifier. |
| 4175 | CXXScopeSpec SS; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4176 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4177 | /*EnteringContext=*/true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4178 | TPA.Revert(); |
| 4179 | return false; |
| 4180 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4181 | |
| 4182 | // Parse the constructor name. |
| 4183 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 4184 | // We already know that we have a constructor name; just consume |
| 4185 | // the token. |
| 4186 | ConsumeToken(); |
| 4187 | } else { |
| 4188 | TPA.Revert(); |
| 4189 | return false; |
| 4190 | } |
| 4191 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4192 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4193 | if (Tok.isNot(tok::l_paren)) { |
| 4194 | TPA.Revert(); |
| 4195 | return false; |
| 4196 | } |
| 4197 | ConsumeParen(); |
| 4198 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4199 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 4200 | // that we have a constructor. |
| 4201 | if (Tok.is(tok::r_paren) || |
| 4202 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4203 | TPA.Revert(); |
| 4204 | return true; |
| 4205 | } |
| 4206 | |
| 4207 | // If we need to, enter the specified scope. |
| 4208 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4209 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4210 | DeclScopeObj.EnterDeclaratorScope(); |
| 4211 | |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4212 | // Optionally skip Microsoft attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4213 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4214 | MaybeParseMicrosoftAttributes(Attrs); |
| 4215 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4216 | // Check whether the next token(s) are part of a declaration |
| 4217 | // specifier, in which case we have the start of a parameter and, |
| 4218 | // therefore, we know that this is a constructor. |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4219 | bool IsConstructor = false; |
| 4220 | if (isDeclarationSpecifier()) |
| 4221 | IsConstructor = true; |
| 4222 | else if (Tok.is(tok::identifier) || |
| 4223 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 4224 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 4225 | // This might be a parenthesized member name, but is more likely to |
| 4226 | // be a constructor declaration with an invalid argument type. Keep |
| 4227 | // looking. |
| 4228 | if (Tok.is(tok::annot_cxxscope)) |
| 4229 | ConsumeToken(); |
| 4230 | ConsumeToken(); |
| 4231 | |
| 4232 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4233 | // which must have one of the following syntactic forms (see the |
| 4234 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4235 | switch (Tok.getKind()) { |
| 4236 | case tok::l_paren: |
| 4237 | // C(X ( int)); |
| 4238 | case tok::l_square: |
| 4239 | // C(X [ 5]); |
| 4240 | // C(X [ [attribute]]); |
| 4241 | case tok::coloncolon: |
| 4242 | // C(X :: Y); |
| 4243 | // C(X :: *p); |
| 4244 | case tok::r_paren: |
| 4245 | // C(X ) |
| 4246 | // Assume this isn't a constructor, rather than assuming it's a |
| 4247 | // constructor with an unnamed parameter of an ill-formed type. |
| 4248 | break; |
| 4249 | |
| 4250 | default: |
| 4251 | IsConstructor = true; |
| 4252 | break; |
| 4253 | } |
| 4254 | } |
| 4255 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4256 | TPA.Revert(); |
| 4257 | return IsConstructor; |
| 4258 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4259 | |
| 4260 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4261 | /// type-qualifier-list: [C99 6.7.5] |
| 4262 | /// type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4263 | /// [vendor] attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4264 | /// [ only if VendorAttributesAllowed=true ] |
| 4265 | /// type-qualifier-list type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4266 | /// [vendor] type-qualifier-list attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4267 | /// [ only if VendorAttributesAllowed=true ] |
| 4268 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4269 | /// [ only if CXX11AttributesAllowed=true ] |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4270 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4271 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4272 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 4273 | bool VendorAttributesAllowed, |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4274 | bool CXX11AttributesAllowed, |
| 4275 | bool AtomicAllowed) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4276 | if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed && |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4277 | isCXX11AttributeSpecifier()) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4278 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4279 | ParseCXX11Attributes(attrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4280 | DS.takeAttributesFrom(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4281 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4282 | |
| 4283 | SourceLocation EndLoc; |
| 4284 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4285 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4286 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4287 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4288 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4289 | SourceLocation Loc = Tok.getLocation(); |
| 4290 | |
| 4291 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4292 | case tok::code_completion: |
| 4293 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 4294 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4295 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4296 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4297 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4298 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4299 | break; |
| 4300 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4301 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4302 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4303 | break; |
| 4304 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4305 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4306 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4307 | break; |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4308 | case tok::kw__Atomic: |
| 4309 | if (!AtomicAllowed) |
| 4310 | goto DoneWithTypeQuals; |
| 4311 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 4312 | getLangOpts()); |
| 4313 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4314 | |
| 4315 | // OpenCL qualifiers: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4316 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4317 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4318 | goto DoneWithTypeQuals; |
| 4319 | case tok::kw___private: |
| 4320 | case tok::kw___global: |
| 4321 | case tok::kw___local: |
| 4322 | case tok::kw___constant: |
| 4323 | case tok::kw___read_only: |
| 4324 | case tok::kw___write_only: |
| 4325 | case tok::kw___read_write: |
| 4326 | ParseOpenCLQualifiers(DS); |
| 4327 | break; |
| 4328 | |
Aaron Ballman | aa9df09 | 2013-05-22 23:25:32 +0000 | [diff] [blame] | 4329 | case tok::kw___sptr: |
| 4330 | case tok::kw___uptr: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4331 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 4332 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4333 | case tok::kw___ptr32: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4334 | case tok::kw___cdecl: |
| 4335 | case tok::kw___stdcall: |
| 4336 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4337 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4338 | case tok::kw___unaligned: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4339 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4340 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4341 | continue; |
| 4342 | } |
| 4343 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4344 | case tok::kw___pascal: |
| 4345 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4346 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4347 | continue; |
| 4348 | } |
| 4349 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4350 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4351 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4352 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4353 | continue; // do *not* consume the next token! |
| 4354 | } |
| 4355 | // otherwise, FALL THROUGH! |
| 4356 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4357 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4358 | // If this is not a type-qualifier token, we're done reading type |
| 4359 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 4360 | DS.Finish(Diags, PP); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4361 | if (EndLoc.isValid()) |
| 4362 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4363 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4364 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4365 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4366 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 4367 | if (isInvalid) { |
| 4368 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4369 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4370 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4371 | EndLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4372 | } |
| 4373 | } |
| 4374 | |
| 4375 | |
| 4376 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 4377 | /// |
| 4378 | void Parser::ParseDeclarator(Declarator &D) { |
| 4379 | /// This implements the 'declarator' production in the C grammar, then checks |
| 4380 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4381 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4382 | } |
| 4383 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4384 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) { |
| 4385 | if (Kind == tok::star || Kind == tok::caret) |
| 4386 | return true; |
| 4387 | |
| 4388 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 4389 | if (!Lang.CPlusPlus) |
| 4390 | return false; |
| 4391 | |
| 4392 | return Kind == tok::amp || Kind == tok::ampamp; |
| 4393 | } |
| 4394 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4395 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 4396 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 4397 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4398 | /// ptr-operator production. |
| 4399 | /// |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4400 | /// 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] | 4401 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 4402 | /// isConstructorDeclarator. |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4403 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4404 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 4405 | /// [C] pointer[opt] direct-declarator |
| 4406 | /// [C++] direct-declarator |
| 4407 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4408 | /// |
| 4409 | /// pointer: [C99 6.7.5] |
| 4410 | /// '*' type-qualifier-list[opt] |
| 4411 | /// '*' type-qualifier-list[opt] pointer |
| 4412 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4413 | /// ptr-operator: |
| 4414 | /// '*' cv-qualifier-seq[opt] |
| 4415 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4416 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4417 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4418 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4419 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4420 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 4421 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 4422 | if (Diags.hasAllExtensionsSilenced()) |
| 4423 | D.setExtension(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4424 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4425 | // C++ member pointers start with a '::' or a nested-name. |
| 4426 | // Member pointers get special handling, since there's no place for the |
| 4427 | // scope spec in the generic path below. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4428 | if (getLangOpts().CPlusPlus && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 4429 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 4430 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4431 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4432 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4433 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4434 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4435 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 4436 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4437 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4438 | // The scope spec really belongs to the direct-declarator. |
Richard Smith | 6a502c4 | 2013-01-08 22:43:49 +0000 | [diff] [blame] | 4439 | if (D.mayHaveIdentifier()) |
| 4440 | D.getCXXScopeSpec() = SS; |
| 4441 | else |
| 4442 | AnnotateScopeToken(SS, true); |
| 4443 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4444 | if (DirectDeclParser) |
| 4445 | (this->*DirectDeclParser)(D); |
| 4446 | return; |
| 4447 | } |
| 4448 | |
| 4449 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4450 | D.SetRangeEnd(Loc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4451 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4452 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4453 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4454 | |
| 4455 | // Recurse to parse whatever is left. |
| 4456 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 4457 | |
| 4458 | // Sema will have to catch (syntactically invalid) pointers into global |
| 4459 | // scope. It has to catch pointers into namespace scope anyway. |
| 4460 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4461 | Loc), |
| 4462 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4463 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4464 | return; |
| 4465 | } |
| 4466 | } |
| 4467 | |
| 4468 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4469 | // Not a pointer, C++ reference, or block. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4470 | if (!isPtrOperatorToken(Kind, getLangOpts())) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4471 | if (DirectDeclParser) |
| 4472 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4473 | return; |
| 4474 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4475 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4476 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 4477 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4478 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4479 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4480 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 4481 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4482 | // Is a pointer. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4483 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4484 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4485 | // FIXME: GNU attributes are not allowed here in a new-type-id. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4486 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4487 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4488 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4489 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4490 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4491 | if (Kind == tok::star) |
| 4492 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 4493 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 4494 | DS.getConstSpecLoc(), |
| 4495 | DS.getVolatileSpecLoc(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4496 | DS.getRestrictSpecLoc()), |
| 4497 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4498 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4499 | else |
| 4500 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4501 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4502 | Loc), |
| 4503 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4504 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4505 | } else { |
| 4506 | // Is a reference |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4507 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4508 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4509 | // Complain about rvalue references in C++03, but then go on and build |
| 4510 | // the declarator. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4511 | if (Kind == tok::ampamp) |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4512 | Diag(Loc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4513 | diag::warn_cxx98_compat_rvalue_reference : |
| 4514 | diag::ext_rvalue_reference); |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4515 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4516 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 4517 | ParseTypeQualifierListOpt(DS); |
| 4518 | D.ExtendWithDeclSpec(DS); |
| 4519 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4520 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 4521 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 4522 | // template type argument, in which case the cv-qualifiers are ignored. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4523 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 4524 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 4525 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4526 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4527 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 4528 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4529 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4530 | // 'restrict' is permitted as an extension. |
| 4531 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 4532 | Diag(DS.getAtomicSpecLoc(), |
| 4533 | diag::err_invalid_reference_qualifier_application) << "_Atomic"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4534 | } |
| 4535 | |
| 4536 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4537 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4538 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4539 | if (D.getNumTypeObjects() > 0) { |
| 4540 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 4541 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 4542 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 4543 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 4544 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4545 | << II; |
| 4546 | else |
| 4547 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4548 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4549 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4550 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4551 | // can go ahead and build the (technically ill-formed) |
| 4552 | // declarator: reference collapsing will take care of it. |
| 4553 | } |
| 4554 | } |
| 4555 | |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4556 | // Remember that we parsed a reference type. |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4557 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4558 | Kind == tok::amp), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4559 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4560 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4561 | } |
| 4562 | } |
| 4563 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4564 | static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D, |
| 4565 | SourceLocation EllipsisLoc) { |
| 4566 | if (EllipsisLoc.isValid()) { |
| 4567 | FixItHint Insertion; |
| 4568 | if (!D.getEllipsisLoc().isValid()) { |
| 4569 | Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "..."); |
| 4570 | D.setEllipsisLoc(EllipsisLoc); |
| 4571 | } |
| 4572 | P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) |
| 4573 | << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName(); |
| 4574 | } |
| 4575 | } |
| 4576 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4577 | /// ParseDirectDeclarator |
| 4578 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4579 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4580 | /// '(' declarator ')' |
| 4581 | /// [GNU] '(' attributes declarator ')' |
| 4582 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4583 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4584 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4585 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4586 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4587 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 4588 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4589 | /// direct-declarator '(' parameter-type-list ')' |
| 4590 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4591 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4592 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 4593 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 4594 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4595 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 4596 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 4597 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4598 | /// [C++] declarator-id |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4599 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4600 | /// |
| 4601 | /// declarator-id: [C++ 8] |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4602 | /// '...'[opt] id-expression |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4603 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 4604 | /// |
| 4605 | /// id-expression: [C++ 5.1] |
| 4606 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4607 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4608 | /// |
| 4609 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4610 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4611 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4612 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4613 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 4614 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 4615 | /// |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4616 | /// Note, any additional constructs added here may need corresponding changes |
| 4617 | /// in isConstructorDeclarator. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4618 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4619 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4620 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4621 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4622 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4623 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4624 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4625 | D.getContext() == Declarator::MemberContext; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4626 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4627 | EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4628 | } |
| 4629 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4630 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4631 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 4632 | // Change the declaration context for name lookup, until this function |
| 4633 | // is exited (and the declarator has been parsed). |
| 4634 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4635 | } |
| 4636 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4637 | // C++0x [dcl.fct]p14: |
| 4638 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4639 | // of a parameter-declaration-clause without a preceding comma. In |
| 4640 | // this case, the ellipsis is parsed as part of the |
| 4641 | // abstract-declarator if the type of the parameter names a template |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4642 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 4643 | // as part of the parameter-declaration-clause. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4644 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4645 | !((D.getContext() == Declarator::PrototypeContext || |
| 4646 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4647 | NextToken().is(tok::r_paren) && |
Richard Smith | 30f2a74 | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4648 | !D.hasGroupingParens() && |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4649 | !Actions.containsUnexpandedParameterPacks(D))) { |
| 4650 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 4651 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) { |
| 4652 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 4653 | // the user what they should have done. |
| 4654 | ParseDeclarator(D); |
| 4655 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4656 | return; |
| 4657 | } else |
| 4658 | D.setEllipsisLoc(EllipsisLoc); |
| 4659 | |
| 4660 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 4661 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 4662 | // the l_paren token. |
| 4663 | } |
| 4664 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4665 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 4666 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 4667 | // We found something that indicates the start of an unqualified-id. |
| 4668 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4669 | bool AllowConstructorName; |
| 4670 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 4671 | AllowConstructorName = false; |
| 4672 | else if (D.getCXXScopeSpec().isSet()) |
| 4673 | AllowConstructorName = |
| 4674 | (D.getContext() == Declarator::FileContext || |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 4675 | D.getContext() == Declarator::MemberContext); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4676 | else |
| 4677 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 4678 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4679 | SourceLocation TemplateKWLoc; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4680 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 4681 | /*EnteringContext=*/true, |
| 4682 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4683 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4684 | ParsedType(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4685 | TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4686 | D.getName()) || |
| 4687 | // Once we're past the identifier, if the scope was bad, mark the |
| 4688 | // whole declarator bad. |
| 4689 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4690 | D.SetIdentifier(0, Tok.getLocation()); |
| 4691 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4692 | } else { |
| 4693 | // Parsed the unqualified-id; update range information and move along. |
| 4694 | if (D.getSourceRange().getBegin().isInvalid()) |
| 4695 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 4696 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4697 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4698 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4699 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4700 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4701 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4702 | "There's a C++-specific check for tok::identifier above"); |
| 4703 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 4704 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 4705 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4706 | goto PastIdentifier; |
| 4707 | } |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4708 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4709 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4710 | // direct-declarator: '(' declarator ')' |
| 4711 | // direct-declarator: '(' attributes declarator ')' |
| 4712 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 4713 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4714 | |
| 4715 | // If the declarator was parenthesized, we entered the declarator |
| 4716 | // scope when parsing the parenthesized declarator, then exited |
| 4717 | // the scope already. Re-enter the scope, if we need to. |
| 4718 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4719 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4720 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4721 | if (!D.isInvalidType() && |
| 4722 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4723 | // Change the declaration context for name lookup, until this function |
| 4724 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4725 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4726 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4727 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4728 | // This could be something simple like "int" (in which case the declarator |
| 4729 | // portion is empty), if an abstract-declarator is allowed. |
| 4730 | D.SetIdentifier(0, Tok.getLocation()); |
Richard Smith | 30f2a74 | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4731 | |
| 4732 | // The grammar for abstract-pack-declarator does not allow grouping parens. |
| 4733 | // FIXME: Revisit this once core issue 1488 is resolved. |
| 4734 | if (D.hasEllipsis() && D.hasGroupingParens()) |
| 4735 | Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()), |
| 4736 | diag::ext_abstract_pack_declarator_parens); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4737 | } else { |
David Blaikie | e75d9cf | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 4738 | if (Tok.getKind() == tok::annot_pragma_parser_crash) |
David Blaikie | 377da4c | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 4739 | LLVM_BUILTIN_TRAP; |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 4740 | if (D.getContext() == Declarator::MemberContext) |
| 4741 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 4742 | << D.getDeclSpec().getSourceRange(); |
Richard Trieu | db55c04c | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 4743 | else if (getLangOpts().CPlusPlus) { |
| 4744 | if (Tok.is(tok::period) || Tok.is(tok::arrow)) |
| 4745 | Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow); |
| 4746 | else |
| 4747 | Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus; |
| 4748 | } else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4749 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4750 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 4751 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4752 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4753 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4754 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4755 | assert(D.isPastIdentifier() && |
| 4756 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4757 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4758 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 4759 | if (D.hasName() && !D.getNumTypeObjects()) |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4760 | MaybeParseCXX11Attributes(D); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4761 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4762 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4763 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4764 | // Enter function-declaration scope, limiting any declarators to the |
| 4765 | // function prototype scope, including parameter declarators. |
| 4766 | ParseScope PrototypeScope(this, |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4767 | Scope::FunctionPrototypeScope|Scope::DeclScope| |
| 4768 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 4769 | ? Scope::FunctionDeclarationScope : 0)); |
| 4770 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4771 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 4772 | // In such a case, check if we actually have a function declarator; if it |
| 4773 | // is not, the declarator has been fully parsed. |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4774 | bool IsAmbiguous = false; |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 4775 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 4776 | // The name of the declarator, if any, is tentatively declared within |
| 4777 | // a possible direct initializer. |
| 4778 | TentativelyDeclaredIdentifiers.push_back(D.getIdentifier()); |
| 4779 | bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous); |
| 4780 | TentativelyDeclaredIdentifiers.pop_back(); |
| 4781 | if (!IsFunctionDecl) |
| 4782 | break; |
| 4783 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4784 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4785 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4786 | T.consumeOpen(); |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4787 | ParseFunctionDeclarator(D, attrs, T, IsAmbiguous); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4788 | PrototypeScope.Exit(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4789 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4790 | ParseBracketDeclarator(D); |
| 4791 | } else { |
| 4792 | break; |
| 4793 | } |
| 4794 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4795 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4796 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4797 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 4798 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4799 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4800 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 4801 | /// |
| 4802 | /// direct-declarator: |
| 4803 | /// '(' declarator ')' |
| 4804 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4805 | /// direct-declarator '(' parameter-type-list ')' |
| 4806 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4807 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4808 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4809 | /// |
| 4810 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4811 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4812 | T.consumeOpen(); |
| 4813 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4814 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4815 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4816 | // Eat any attributes before we look at whether this is a grouping or function |
| 4817 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 4818 | // the type being built up, for example: |
| 4819 | // int (__attribute__(()) *x)(long y) |
| 4820 | // If this ends up not being a grouping paren, the attribute applies to the |
| 4821 | // first argument, for example: |
| 4822 | // int (__attribute__(()) int x) |
| 4823 | // In either case, we need to eat any attributes to be able to determine what |
| 4824 | // sort of paren this is. |
| 4825 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4826 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4827 | bool RequiresArg = false; |
| 4828 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4829 | ParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4830 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4831 | // We require that the argument list (if this is a non-grouping paren) be |
| 4832 | // present even if the attribute list was empty. |
| 4833 | RequiresArg = true; |
| 4834 | } |
Chad Rosier | 9cab1c9 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 4835 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4836 | // Eat any Microsoft extensions. |
Chad Rosier | 9cab1c9 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 4837 | ParseMicrosoftTypeAttributes(attrs); |
| 4838 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4839 | // Eat any Borland extensions. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 4840 | if (Tok.is(tok::kw___pascal)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4841 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4842 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4843 | // If we haven't past the identifier yet (or where the identifier would be |
| 4844 | // stored, if this is an abstract declarator), then this is probably just |
| 4845 | // grouping parens. However, if this could be an abstract-declarator, then |
| 4846 | // this could also be the start of function arguments (consider 'void()'). |
| 4847 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4848 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4849 | if (!D.mayOmitIdentifier()) { |
| 4850 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 4851 | // paren, because we haven't seen the identifier yet. |
| 4852 | isGrouping = true; |
| 4853 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4854 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 4855 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4856 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 4857 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4858 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 4859 | // considered to be a type, not a K&R identifier-list. |
| 4860 | isGrouping = false; |
| 4861 | } else { |
| 4862 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 4863 | isGrouping = true; |
| 4864 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4865 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4866 | // If this is a grouping paren, handle: |
| 4867 | // direct-declarator: '(' declarator ')' |
| 4868 | // direct-declarator: '(' attributes declarator ')' |
| 4869 | if (isGrouping) { |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4870 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 4871 | D.setEllipsisLoc(SourceLocation()); |
| 4872 | |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4873 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4874 | D.setGroupingParens(true); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4875 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4876 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4877 | T.consumeClose(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4878 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4879 | T.getCloseLocation()), |
| 4880 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4881 | |
| 4882 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4883 | |
| 4884 | // An ellipsis cannot be placed outside parentheses. |
| 4885 | if (EllipsisLoc.isValid()) |
| 4886 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4887 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4888 | return; |
| 4889 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4890 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4891 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 4892 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4893 | // identifier (and remember where it would have been), then call into |
| 4894 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4895 | D.SetIdentifier(0, Tok.getLocation()); |
| 4896 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4897 | // Enter function-declaration scope, limiting any declarators to the |
| 4898 | // function prototype scope, including parameter declarators. |
| 4899 | ParseScope PrototypeScope(this, |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4900 | Scope::FunctionPrototypeScope | Scope::DeclScope | |
| 4901 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 4902 | ? Scope::FunctionDeclarationScope : 0)); |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4903 | ParseFunctionDeclarator(D, attrs, T, false, RequiresArg); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4904 | PrototypeScope.Exit(); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4905 | } |
| 4906 | |
| 4907 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 4908 | /// declarator D up to a paren, which indicates that we are parsing function |
| 4909 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4910 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4911 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 4912 | /// immediately after the open paren - they should be considered to be the |
| 4913 | /// first argument of a parameter. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4914 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4915 | /// If RequiresArg is true, then the first argument of the function is required |
| 4916 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4917 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4918 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 4919 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 4920 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 4921 | /// |
| 4922 | /// [C++11] exception-specification: |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4923 | /// dynamic-exception-specification |
| 4924 | /// noexcept-specification |
| 4925 | /// |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4926 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4927 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4928 | BalancedDelimiterTracker &Tracker, |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4929 | bool IsAmbiguous, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4930 | bool RequiresArg) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4931 | assert(getCurScope()->isFunctionPrototypeScope() && |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4932 | "Should call from a Function scope"); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4933 | // lparen is already consumed! |
| 4934 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 4935 | |
| 4936 | // This should be true when the function has typed arguments. |
| 4937 | // Otherwise, it is treated as a K&R-style function. |
| 4938 | bool HasProto = false; |
| 4939 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4940 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4941 | // Remember where we see an ellipsis, if any. |
| 4942 | SourceLocation EllipsisLoc; |
| 4943 | |
| 4944 | DeclSpec DS(AttrFactory); |
| 4945 | bool RefQualifierIsLValueRef = true; |
| 4946 | SourceLocation RefQualifierLoc; |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4947 | SourceLocation ConstQualifierLoc; |
| 4948 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4949 | ExceptionSpecificationType ESpecType = EST_None; |
| 4950 | SourceRange ESpecRange; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4951 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 4952 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4953 | ExprResult NoexceptExpr; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4954 | ParsedAttributes FnAttrs(AttrFactory); |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 4955 | TypeResult TrailingReturnType; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4956 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4957 | Actions.ActOnStartFunctionDeclarator(); |
| 4958 | |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4959 | /* LocalEndLoc is the end location for the local FunctionTypeLoc. |
| 4960 | EndLoc is the end location for the function declarator. |
| 4961 | They differ for trailing return types. */ |
| 4962 | SourceLocation StartLoc, LocalEndLoc, EndLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4963 | SourceLocation LParenLoc, RParenLoc; |
| 4964 | LParenLoc = Tracker.getOpenLocation(); |
| 4965 | StartLoc = LParenLoc; |
| 4966 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4967 | if (isFunctionDeclaratorIdentifierList()) { |
| 4968 | if (RequiresArg) |
| 4969 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4970 | |
| 4971 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 4972 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4973 | Tracker.consumeClose(); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4974 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4975 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4976 | EndLoc = RParenLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4977 | } else { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4978 | if (Tok.isNot(tok::r_paren)) |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4979 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4980 | else if (RequiresArg) |
| 4981 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4982 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4983 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4984 | |
| 4985 | // If we have the closing ')', eat it. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4986 | Tracker.consumeClose(); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4987 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4988 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4989 | EndLoc = RParenLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4990 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4991 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4992 | // FIXME: Accept these components in any order, and produce fixits to |
| 4993 | // correct the order if the user gets it wrong. Ideally we should deal |
| 4994 | // with the virt-specifier-seq and pure-specifier in the same way. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4995 | |
| 4996 | // Parse cv-qualifier-seq[opt]. |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4997 | ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false, |
| 4998 | /*CXX11AttributesAllowed*/ false, |
| 4999 | /*AtomicAllowed*/ false); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5000 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 5001 | EndLoc = DS.getSourceRange().getEnd(); |
| 5002 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 5003 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 5004 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5005 | |
| 5006 | // Parse ref-qualifier[opt]. |
| 5007 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5008 | Diag(Tok, getLangOpts().CPlusPlus11 ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5009 | diag::warn_cxx98_compat_ref_qualifier : |
| 5010 | diag::ext_ref_qualifier); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5011 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5012 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 5013 | RefQualifierLoc = ConsumeToken(); |
| 5014 | EndLoc = RefQualifierLoc; |
| 5015 | } |
| 5016 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5017 | // C++11 [expr.prim.general]p3: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5018 | // If a declaration declares a member function or member function |
| 5019 | // 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] | 5020 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5021 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5022 | // declarator. |
Richard Smith | d922779 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5023 | // FIXME: currently, "static" case isn't handled correctly. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5024 | bool IsCXX11MemberFunction = |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5025 | getLangOpts().CPlusPlus11 && |
Richard Smith | d922779 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5026 | (D.getContext() == Declarator::MemberContext |
| 5027 | ? !D.getDeclSpec().isFriendSpecified() |
| 5028 | : D.getContext() == Declarator::FileContext && |
| 5029 | D.getCXXScopeSpec().isValid() && |
| 5030 | Actions.CurContext->isRecord()); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5031 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 5032 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
Richard Smith | 7b19cb1 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 5033 | DS.getTypeQualifiers() | |
Richard Smith | 8404626 | 2013-04-21 01:08:50 +0000 | [diff] [blame] | 5034 | (D.getDeclSpec().isConstexprSpecified() && |
| 5035 | !getLangOpts().CPlusPlus1y |
Richard Smith | 7b19cb1 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 5036 | ? Qualifiers::Const : 0), |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5037 | IsCXX11MemberFunction); |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5038 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5039 | // Parse exception-specification[opt]. |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5040 | ESpecType = tryParseExceptionSpecification(ESpecRange, |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 5041 | DynamicExceptions, |
| 5042 | DynamicExceptionRanges, |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5043 | NoexceptExpr); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5044 | if (ESpecType != EST_None) |
| 5045 | EndLoc = ESpecRange.getEnd(); |
| 5046 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5047 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 5048 | // after the exception-specification. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5049 | MaybeParseCXX11Attributes(FnAttrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5050 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5051 | // Parse trailing-return-type[opt]. |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5052 | LocalEndLoc = EndLoc; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5053 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5054 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5055 | if (D.getDeclSpec().getTypeSpecType() == TST_auto) |
| 5056 | StartLoc = D.getDeclSpec().getTypeSpecTypeLoc(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5057 | LocalEndLoc = Tok.getLocation(); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 5058 | SourceRange Range; |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 5059 | TrailingReturnType = ParseTrailingReturnType(Range); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5060 | EndLoc = Range.getEnd(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5061 | } |
| 5062 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5063 | } |
| 5064 | |
| 5065 | // Remember that we parsed a function type, and remember the attributes. |
| 5066 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5067 | IsAmbiguous, |
| 5068 | LParenLoc, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5069 | ParamInfo.data(), ParamInfo.size(), |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5070 | EllipsisLoc, RParenLoc, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5071 | DS.getTypeQualifiers(), |
| 5072 | RefQualifierIsLValueRef, |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 5073 | RefQualifierLoc, ConstQualifierLoc, |
| 5074 | VolatileQualifierLoc, |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 5075 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5076 | ESpecType, ESpecRange.getBegin(), |
| 5077 | DynamicExceptions.data(), |
| 5078 | DynamicExceptionRanges.data(), |
| 5079 | DynamicExceptions.size(), |
| 5080 | NoexceptExpr.isUsable() ? |
| 5081 | NoexceptExpr.get() : 0, |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5082 | StartLoc, LocalEndLoc, D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5083 | TrailingReturnType), |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5084 | FnAttrs, EndLoc); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 5085 | |
| 5086 | Actions.ActOnEndFunctionDeclarator(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5087 | } |
| 5088 | |
| 5089 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 5090 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 5091 | /// |
| 5092 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 5093 | /// abstract-declarators. |
| 5094 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5095 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5096 | && Tok.is(tok::identifier) |
| 5097 | && !TryAltiVecVectorToken() |
| 5098 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 5099 | // 6.7.5.3p11. |
| 5100 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 5101 | // Identifier lists follow a really simple grammar: the identifiers can |
| 5102 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 5103 | // identifier lists are really rare in the brave new modern world, and |
| 5104 | // it is very common for someone to typo a type in a non-K&R style |
| 5105 | // list. If we are presented with something like: "void foo(intptr x, |
| 5106 | // float y)", we don't want to start parsing the function declarator as |
| 5107 | // though it is a K&R style declarator just because intptr is an |
| 5108 | // invalid type. |
| 5109 | // |
| 5110 | // To handle this, we check to see if the token after the first |
| 5111 | // identifier is a "," or ")". Only then do we parse it as an |
| 5112 | // identifier list. |
| 5113 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 5114 | } |
| 5115 | |
| 5116 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 5117 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 5118 | /// |
| 5119 | /// After returning, ParamInfo will hold the parsed parameters. |
| 5120 | /// |
| 5121 | /// identifier-list: [C99 6.7.5] |
| 5122 | /// identifier |
| 5123 | /// identifier-list ',' identifier |
| 5124 | /// |
| 5125 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 5126 | Declarator &D, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5127 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5128 | // If there was no identifier specified for the declarator, either we are in |
| 5129 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 5130 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 5131 | // diagnose this. |
| 5132 | if (!D.getIdentifier()) |
| 5133 | Diag(Tok, diag::ext_ident_list_in_param); |
| 5134 | |
| 5135 | // Maintain an efficient lookup of params we have seen so far. |
| 5136 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 5137 | |
| 5138 | while (1) { |
| 5139 | // If this isn't an identifier, report the error and skip until ')'. |
| 5140 | if (Tok.isNot(tok::identifier)) { |
| 5141 | Diag(Tok, diag::err_expected_ident); |
| 5142 | SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 5143 | // Forget we parsed anything. |
| 5144 | ParamInfo.clear(); |
| 5145 | return; |
| 5146 | } |
| 5147 | |
| 5148 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 5149 | |
| 5150 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 5151 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 5152 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 5153 | |
| 5154 | // Verify that the argument identifier has not already been mentioned. |
| 5155 | if (!ParamsSoFar.insert(ParmII)) { |
| 5156 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 5157 | } else { |
| 5158 | // Remember this identifier in ParamInfo. |
| 5159 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 5160 | Tok.getLocation(), |
| 5161 | 0)); |
| 5162 | } |
| 5163 | |
| 5164 | // Eat the identifier. |
| 5165 | ConsumeToken(); |
| 5166 | |
| 5167 | // The list continues if we see a comma. |
| 5168 | if (Tok.isNot(tok::comma)) |
| 5169 | break; |
| 5170 | ConsumeToken(); |
| 5171 | } |
| 5172 | } |
| 5173 | |
| 5174 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 5175 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 5176 | /// identifier list. |
| 5177 | /// |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5178 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 5179 | /// caller parsed those arguments immediately after the open paren - they should |
| 5180 | /// be considered to be part of the first parameter. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5181 | /// |
| 5182 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 5183 | /// be the location of the ellipsis, if any was parsed. |
| 5184 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5185 | /// parameter-type-list: [C99 6.7.5] |
| 5186 | /// parameter-list |
| 5187 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5188 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5189 | /// |
| 5190 | /// parameter-list: [C99 6.7.5] |
| 5191 | /// parameter-declaration |
| 5192 | /// parameter-list ',' parameter-declaration |
| 5193 | /// |
| 5194 | /// parameter-declaration: [C99 6.7.5] |
| 5195 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5196 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5197 | /// [C++11] initializer-clause |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5198 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 5199 | /// declaration-specifiers abstract-declarator[opt] |
| 5200 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 5201 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5202 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5203 | /// [C++11] attribute-specifier-seq parameter-declaration |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5204 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5205 | void Parser::ParseParameterDeclarationClause( |
| 5206 | Declarator &D, |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5207 | ParsedAttributes &FirstArgAttrs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5208 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5209 | SourceLocation &EllipsisLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5210 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5211 | while (1) { |
| 5212 | if (Tok.is(tok::ellipsis)) { |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5213 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 5214 | // before deciding this was a parameter-declaration-clause. |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 5215 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5216 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5217 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5218 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5219 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5220 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5221 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5222 | |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5223 | // Parse any C++11 attributes. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5224 | MaybeParseCXX11Attributes(DS.getAttributes()); |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5225 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5226 | // Skip any Microsoft attributes before a param. |
Chad Rosier | 16f90bf | 2012-12-20 20:37:53 +0000 | [diff] [blame] | 5227 | MaybeParseMicrosoftAttributes(DS.getAttributes()); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5228 | |
| 5229 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5230 | |
| 5231 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5232 | // 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] | 5233 | // 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] | 5234 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 5235 | // too much hassle. |
| 5236 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5237 | |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 5238 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5239 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5240 | // Parse the declarator. This is "PrototypeContext", because we must |
| 5241 | // accept either 'declarator' or 'abstract-declarator' here. |
| 5242 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 5243 | ParseDeclarator(ParmDecl); |
| 5244 | |
| 5245 | // Parse GNU attributes, if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5246 | MaybeParseGNUAttributes(ParmDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5247 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5248 | // Remember this parsed parameter in ParamInfo. |
| 5249 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5250 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5251 | // DefArgToks is used when the parsing of default arguments needs |
| 5252 | // to be delayed. |
| 5253 | CachedTokens *DefArgToks = 0; |
| 5254 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5255 | // If no parameter was specified, verify that *something* was specified, |
| 5256 | // otherwise we have a missing type and identifier. |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 5257 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 5258 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5259 | // Completely missing, emit error. |
| 5260 | Diag(DSStart, diag::err_missing_param); |
| 5261 | } else { |
| 5262 | // Otherwise, we have something. Add it and let semantic analysis try |
| 5263 | // 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] | 5264 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5265 | // Inform the actions module about the parameter declarator, so it gets |
| 5266 | // added to the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5267 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5268 | |
| 5269 | // Parse the default argument, if any. We parse the default |
| 5270 | // arguments in all dialects; the semantic analysis in |
| 5271 | // ActOnParamDefaultArgument will reject the default argument in |
| 5272 | // C. |
| 5273 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5274 | SourceLocation EqualLoc = Tok.getLocation(); |
| 5275 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5276 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5277 | if (D.getContext() == Declarator::MemberContext) { |
| 5278 | // If we're inside a class definition, cache the tokens |
| 5279 | // corresponding to the default argument. We'll actually parse |
| 5280 | // them when we see the end of the class definition. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5281 | // FIXME: Can we use a smart pointer for Toks? |
| 5282 | DefArgToks = new CachedTokens; |
| 5283 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5284 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 5285 | /*StopAtSemi=*/true, |
| 5286 | /*ConsumeFinalToken=*/false)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5287 | delete DefArgToks; |
| 5288 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5289 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5290 | } else { |
| 5291 | // Mark the end of the default argument so that we know when to |
| 5292 | // stop when we parse it later on. |
| 5293 | Token DefArgEnd; |
| 5294 | DefArgEnd.startToken(); |
| 5295 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 5296 | DefArgEnd.setLocation(Tok.getLocation()); |
| 5297 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5298 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 5299 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5300 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5301 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5302 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5303 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5304 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5305 | // The argument isn't actually potentially evaluated unless it is |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5306 | // used. |
| 5307 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 5308 | Sema::PotentiallyEvaluatedIfUsed, |
| 5309 | Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5310 | |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5311 | ExprResult DefArgResult; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5312 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5313 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5314 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5315 | } else |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5316 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5317 | if (DefArgResult.isInvalid()) { |
| 5318 | Actions.ActOnParamDefaultArgumentError(Param); |
| 5319 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 5320 | } else { |
| 5321 | // Inform the actions module about the default argument |
| 5322 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5323 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5324 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5325 | } |
| 5326 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5327 | |
| 5328 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 5329 | ParmDecl.getIdentifierLoc(), Param, |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5330 | DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5331 | } |
| 5332 | |
| 5333 | // 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] | 5334 | if (Tok.isNot(tok::comma)) { |
| 5335 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5336 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5337 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5338 | if (!getLangOpts().CPlusPlus) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5339 | // We have ellipsis without a preceding ',', which is ill-formed |
| 5340 | // in C. Complain and provide the fix. |
| 5341 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5342 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5343 | } |
| 5344 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5345 | |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5346 | break; |
| 5347 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5348 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5349 | // Consume the comma. |
| 5350 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5351 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5352 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 5353 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5354 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5355 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 5356 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 5357 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 5358 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 5359 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5360 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 5361 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5362 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5363 | if (CheckProhibitedCXX11Attribute()) |
| 5364 | return; |
| 5365 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5366 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 5367 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5368 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5369 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 5370 | // This code does a fast path to handle some of the most obvious cases. |
| 5371 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5372 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5373 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5374 | MaybeParseCXX11Attributes(attrs); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5375 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5376 | // Remember that we parsed the empty array type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5377 | ExprResult NumElements; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5378 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5379 | T.getOpenLocation(), |
| 5380 | T.getCloseLocation()), |
| 5381 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5382 | return; |
| 5383 | } else if (Tok.getKind() == tok::numeric_constant && |
| 5384 | GetLookAheadToken(1).is(tok::r_square)) { |
| 5385 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5386 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5387 | ConsumeToken(); |
| 5388 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5389 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5390 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5391 | MaybeParseCXX11Attributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5392 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5393 | // Remember that we parsed a array type, and remember its features. |
Nikola Smiljanic | ebf0fa8 | 2013-01-11 08:33:05 +0000 | [diff] [blame] | 5394 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5395 | ExprRes.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5396 | T.getOpenLocation(), |
| 5397 | T.getCloseLocation()), |
| 5398 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5399 | return; |
| 5400 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5401 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5402 | // If valid, this location is the position where we read the 'static' keyword. |
| 5403 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5404 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5405 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5406 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5407 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5408 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5409 | DeclSpec DS(AttrFactory); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5410 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5411 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5412 | // If we haven't already read 'static', check to see if there is one after the |
| 5413 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5414 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5415 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5416 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5417 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 5418 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5419 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5420 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5421 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 5422 | // 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] | 5423 | // the token after the star is a ']'. Since stars in arrays are |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5424 | // infrequent, use of lookahead is not costly here. |
| 5425 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 5426 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5427 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5428 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5429 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5430 | StaticLoc = SourceLocation(); // Drop the static. |
| 5431 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5432 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5433 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5434 | // Note, in C89, this production uses the constant-expr production instead |
| 5435 | // of assignment-expr. The only difference is that assignment-expr allows |
| 5436 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 5437 | // 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] | 5438 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5439 | // Parse the constant-expression or assignment-expression now (depending |
| 5440 | // on dialect). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5441 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5442 | NumElements = ParseConstantExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5443 | } else { |
| 5444 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 5445 | Sema::ConstantEvaluated); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5446 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5447 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5448 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5449 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5450 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 5451 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 5452 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5453 | // If the expression was invalid, skip it. |
| 5454 | SkipUntil(tok::r_square); |
| 5455 | return; |
| 5456 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5457 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5458 | T.consumeClose(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5459 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5460 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5461 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5462 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5463 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5464 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5465 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5466 | NumElements.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5467 | T.getOpenLocation(), |
| 5468 | T.getCloseLocation()), |
| 5469 | attrs, T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5470 | } |
| 5471 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5472 | /// [GNU] typeof-specifier: |
| 5473 | /// typeof ( expressions ) |
| 5474 | /// typeof ( type-name ) |
| 5475 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5476 | /// |
| 5477 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5478 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5479 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5480 | SourceLocation StartLoc = ConsumeToken(); |
| 5481 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5482 | const bool hasParens = Tok.is(tok::l_paren); |
| 5483 | |
Eli Friedman | 80bfa3d | 2012-09-26 04:34:21 +0000 | [diff] [blame] | 5484 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated, |
| 5485 | Sema::ReuseLambdaContextDecl); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5486 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5487 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5488 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5489 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5490 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 5491 | CastTy, CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5492 | if (hasParens) |
| 5493 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5494 | |
| 5495 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5496 | // FIXME: Not accurate, the range gets one token more than it should. |
| 5497 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5498 | else |
| 5499 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5500 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5501 | if (isCastExpr) { |
| 5502 | if (!CastTy) { |
| 5503 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5504 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 5505 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5506 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5507 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5508 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5509 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5510 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5511 | DiagID, CastTy)) |
| 5512 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5513 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5514 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5515 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5516 | // If we get here, the operand to the typeof was an expresion. |
| 5517 | if (Operand.isInvalid()) { |
| 5518 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 5519 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5520 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5521 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5522 | // We might need to transform the operand if it is potentially evaluated. |
| 5523 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 5524 | if (Operand.isInvalid()) { |
| 5525 | DS.SetTypeSpecError(); |
| 5526 | return; |
| 5527 | } |
| 5528 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5529 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5530 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5531 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5532 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5533 | DiagID, Operand.get())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5534 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5535 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5536 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 5537 | /// [C11] atomic-specifier: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5538 | /// _Atomic ( type-name ) |
| 5539 | /// |
| 5540 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5541 | assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) && |
| 5542 | "Not an atomic specifier"); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5543 | |
| 5544 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5545 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5546 | if (T.consumeOpen()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5547 | return; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5548 | |
| 5549 | TypeResult Result = ParseTypeName(); |
| 5550 | if (Result.isInvalid()) { |
| 5551 | SkipUntil(tok::r_paren); |
| 5552 | return; |
| 5553 | } |
| 5554 | |
| 5555 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5556 | T.consumeClose(); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5557 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5558 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5559 | return; |
| 5560 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5561 | DS.setTypeofParensRange(T.getRange()); |
| 5562 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5563 | |
| 5564 | const char *PrevSpec = 0; |
| 5565 | unsigned DiagID; |
| 5566 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
| 5567 | DiagID, Result.release())) |
| 5568 | Diag(StartLoc, DiagID) << PrevSpec; |
| 5569 | } |
| 5570 | |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5571 | |
| 5572 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 5573 | /// from TryAltiVecVectorToken. |
| 5574 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 5575 | Token Next = NextToken(); |
| 5576 | switch (Next.getKind()) { |
| 5577 | default: return false; |
| 5578 | case tok::kw_short: |
| 5579 | case tok::kw_long: |
| 5580 | case tok::kw_signed: |
| 5581 | case tok::kw_unsigned: |
| 5582 | case tok::kw_void: |
| 5583 | case tok::kw_char: |
| 5584 | case tok::kw_int: |
| 5585 | case tok::kw_float: |
| 5586 | case tok::kw_double: |
| 5587 | case tok::kw_bool: |
| 5588 | case tok::kw___pixel: |
| 5589 | Tok.setKind(tok::kw___vector); |
| 5590 | return true; |
| 5591 | case tok::identifier: |
| 5592 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5593 | Tok.setKind(tok::kw___vector); |
| 5594 | return true; |
| 5595 | } |
| 5596 | return false; |
| 5597 | } |
| 5598 | } |
| 5599 | |
| 5600 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 5601 | const char *&PrevSpec, unsigned &DiagID, |
| 5602 | bool &isInvalid) { |
| 5603 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 5604 | Token Next = NextToken(); |
| 5605 | switch (Next.getKind()) { |
| 5606 | case tok::kw_short: |
| 5607 | case tok::kw_long: |
| 5608 | case tok::kw_signed: |
| 5609 | case tok::kw_unsigned: |
| 5610 | case tok::kw_void: |
| 5611 | case tok::kw_char: |
| 5612 | case tok::kw_int: |
| 5613 | case tok::kw_float: |
| 5614 | case tok::kw_double: |
| 5615 | case tok::kw_bool: |
| 5616 | case tok::kw___pixel: |
| 5617 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5618 | return true; |
| 5619 | case tok::identifier: |
| 5620 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5621 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5622 | return true; |
| 5623 | } |
| 5624 | break; |
| 5625 | default: |
| 5626 | break; |
| 5627 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 5628 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5629 | DS.isTypeAltiVecVector()) { |
| 5630 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 5631 | return true; |
| 5632 | } |
| 5633 | return false; |
| 5634 | } |