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 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 181 | |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 182 | /// Parse the arguments to a parameterized GNU attribute or |
| 183 | /// a C++11 attribute in "gnu" namespace. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 184 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 185 | SourceLocation AttrNameLoc, |
| 186 | ParsedAttributes &Attrs, |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 187 | SourceLocation *EndLoc, |
| 188 | IdentifierInfo *ScopeName, |
| 189 | SourceLocation ScopeLoc, |
| 190 | AttributeList::Syntax Syntax) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 191 | |
| 192 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 193 | |
| 194 | // Availability attributes have their own grammar. |
| 195 | if (AttrName->isStr("availability")) { |
| 196 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 197 | return; |
| 198 | } |
| 199 | // Thread safety attributes fit into the FIXME case above, so we |
| 200 | // just parse the arguments as a list of expressions |
| 201 | if (IsThreadSafetyAttribute(AttrName->getName())) { |
| 202 | ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 203 | return; |
| 204 | } |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 205 | // Type safety attributes have their own grammar. |
| 206 | if (AttrName->isStr("type_tag_for_datatype")) { |
| 207 | ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 208 | return; |
| 209 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 210 | |
| 211 | ConsumeParen(); // ignore the left paren loc for now |
| 212 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 213 | IdentifierInfo *ParmName = 0; |
| 214 | SourceLocation ParmLoc; |
| 215 | bool BuiltinType = false; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 216 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 217 | TypeResult T; |
| 218 | SourceRange TypeRange; |
| 219 | bool TypeParsed = false; |
| 220 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 221 | switch (Tok.getKind()) { |
| 222 | case tok::kw_char: |
| 223 | case tok::kw_wchar_t: |
| 224 | case tok::kw_char16_t: |
| 225 | case tok::kw_char32_t: |
| 226 | case tok::kw_bool: |
| 227 | case tok::kw_short: |
| 228 | case tok::kw_int: |
| 229 | case tok::kw_long: |
| 230 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 231 | case tok::kw___int128: |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 232 | case tok::kw_signed: |
| 233 | case tok::kw_unsigned: |
| 234 | case tok::kw_float: |
| 235 | case tok::kw_double: |
| 236 | case tok::kw_void: |
| 237 | case tok::kw_typeof: |
| 238 | // __attribute__(( vec_type_hint(char) )) |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 239 | BuiltinType = true; |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 240 | T = ParseTypeName(&TypeRange); |
| 241 | TypeParsed = true; |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 242 | break; |
| 243 | |
| 244 | case tok::identifier: |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 245 | if (AttrName->isStr("vec_type_hint")) { |
| 246 | T = ParseTypeName(&TypeRange); |
| 247 | TypeParsed = true; |
| 248 | break; |
| 249 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 250 | ParmName = Tok.getIdentifierInfo(); |
| 251 | ParmLoc = ConsumeToken(); |
| 252 | break; |
| 253 | |
| 254 | default: |
| 255 | break; |
| 256 | } |
| 257 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 258 | ExprVector ArgExprs; |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 259 | bool isInvalid = false; |
| 260 | bool isParmType = false; |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 261 | |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 262 | if (!BuiltinType && !AttrName->isStr("vec_type_hint") && |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 263 | (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) { |
| 264 | // Eat the comma. |
| 265 | if (ParmLoc.isValid()) |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 266 | ConsumeToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 267 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 268 | // Parse the non-empty comma-separated list of expressions. |
| 269 | while (1) { |
| 270 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 271 | if (ArgExpr.isInvalid()) { |
| 272 | SkipUntil(tok::r_paren); |
| 273 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 274 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 275 | ArgExprs.push_back(ArgExpr.release()); |
| 276 | if (Tok.isNot(tok::comma)) |
| 277 | break; |
| 278 | ConsumeToken(); // Eat the comma, move to the next argument |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 279 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 280 | } |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 281 | else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) { |
| 282 | if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<", |
| 283 | tok::greater)) { |
Fariborz Jahanian | b224343 | 2011-10-18 23:13:50 +0000 | [diff] [blame] | 284 | while (Tok.is(tok::identifier)) { |
| 285 | ConsumeToken(); |
| 286 | if (Tok.is(tok::greater)) |
| 287 | break; |
| 288 | if (Tok.is(tok::comma)) { |
| 289 | ConsumeToken(); |
| 290 | continue; |
| 291 | } |
| 292 | } |
| 293 | if (Tok.isNot(tok::greater)) |
| 294 | Diag(Tok, diag::err_iboutletcollection_with_protocol); |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 295 | SkipUntil(tok::r_paren, false, true); // skip until ')' |
| 296 | } |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 297 | } else if (AttrName->isStr("vec_type_hint")) { |
| 298 | if (T.get() && !T.isInvalid()) |
| 299 | isParmType = true; |
| 300 | else { |
| 301 | if (Tok.is(tok::identifier)) |
| 302 | ConsumeToken(); |
| 303 | if (TypeParsed) |
| 304 | isInvalid = true; |
| 305 | } |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 306 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 307 | |
| 308 | SourceLocation RParen = Tok.getLocation(); |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 309 | if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen) && |
| 310 | !isInvalid) { |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 311 | SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc; |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 312 | if (isParmType) { |
Joey Gouly | 37453b9 | 2013-03-08 09:42:32 +0000 | [diff] [blame] | 313 | Attrs.addNewTypeAttr(AttrName, SourceRange(AttrLoc, RParen), ScopeName, |
| 314 | ScopeLoc, ParmName, ParmLoc, T.get(), Syntax); |
| 315 | } else { |
| 316 | AttributeList *attr = Attrs.addNew( |
| 317 | AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc, ParmName, |
| 318 | ParmLoc, ArgExprs.data(), ArgExprs.size(), Syntax); |
| 319 | if (BuiltinType && |
| 320 | attr->getKind() == AttributeList::AT_IBOutletCollection) |
| 321 | Diag(Tok, diag::err_iboutletcollection_builtintype); |
| 322 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 326 | /// \brief Parses a single argument for a declspec, including the |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 327 | /// surrounding parens. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 328 | void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 329 | SourceLocation AttrNameLoc, |
| 330 | ParsedAttributes &Attrs) |
| 331 | { |
| 332 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 333 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 334 | AttrName->getNameStart(), tok::r_paren)) |
| 335 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 336 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 337 | ExprResult ArgExpr(ParseConstantExpression()); |
| 338 | if (ArgExpr.isInvalid()) { |
| 339 | T.skipToEnd(); |
| 340 | return; |
| 341 | } |
| 342 | Expr *ExprList = ArgExpr.take(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 343 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 344 | &ExprList, 1, AttributeList::AS_Declspec); |
| 345 | |
| 346 | T.consumeClose(); |
| 347 | } |
| 348 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 349 | /// \brief Determines whether a declspec is a "simple" one requiring no |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 350 | /// arguments. |
| 351 | bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) { |
| 352 | return llvm::StringSwitch<bool>(Ident->getName()) |
| 353 | .Case("dllimport", true) |
| 354 | .Case("dllexport", true) |
| 355 | .Case("noreturn", true) |
| 356 | .Case("nothrow", true) |
| 357 | .Case("noinline", true) |
| 358 | .Case("naked", true) |
| 359 | .Case("appdomain", true) |
| 360 | .Case("process", true) |
| 361 | .Case("jitintrinsic", true) |
| 362 | .Case("noalias", true) |
| 363 | .Case("restrict", true) |
| 364 | .Case("novtable", true) |
| 365 | .Case("selectany", true) |
| 366 | .Case("thread", true) |
| 367 | .Default(false); |
| 368 | } |
| 369 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 370 | /// \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] | 371 | /// parameters). Will return false if we properly handled the declspec, or |
| 372 | /// true if it is an unknown declspec. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 373 | void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 374 | SourceLocation Loc, |
| 375 | ParsedAttributes &Attrs) { |
| 376 | // Try to handle the easy case first -- these declspecs all take a single |
| 377 | // parameter as their argument. |
| 378 | if (llvm::StringSwitch<bool>(Ident->getName()) |
| 379 | .Case("uuid", true) |
| 380 | .Case("align", true) |
| 381 | .Case("allocate", true) |
| 382 | .Default(false)) { |
| 383 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 384 | } else if (Ident->getName() == "deprecated") { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 385 | // The deprecated declspec has an optional single argument, so we will |
| 386 | // 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] | 387 | // not. |
| 388 | if (Tok.getKind() == tok::l_paren) |
| 389 | ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs); |
| 390 | else |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 391 | Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 392 | AttributeList::AS_Declspec); |
| 393 | } else if (Ident->getName() == "property") { |
| 394 | // 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] | 395 | // assignment expressions as a parameter, but the lhs of the assignment |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 396 | // must be named get or put. |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 397 | if (Tok.isNot(tok::l_paren)) { |
| 398 | Diag(Tok.getLocation(), diag::err_expected_lparen_after) |
| 399 | << Ident->getNameStart(); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 400 | return; |
John McCall | 76da55d | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 401 | } |
| 402 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 403 | T.expectAndConsume(diag::err_expected_lparen_after, |
| 404 | Ident->getNameStart(), tok::r_paren); |
| 405 | |
| 406 | enum AccessorKind { |
| 407 | AK_Invalid = -1, |
| 408 | AK_Put = 0, AK_Get = 1 // indices into AccessorNames |
| 409 | }; |
| 410 | IdentifierInfo *AccessorNames[] = { 0, 0 }; |
| 411 | bool HasInvalidAccessor = false; |
| 412 | |
| 413 | // Parse the accessor specifications. |
| 414 | while (true) { |
| 415 | // Stop if this doesn't look like an accessor spec. |
| 416 | if (!Tok.is(tok::identifier)) { |
| 417 | // If the user wrote a completely empty list, use a special diagnostic. |
| 418 | if (Tok.is(tok::r_paren) && !HasInvalidAccessor && |
| 419 | AccessorNames[AK_Put] == 0 && AccessorNames[AK_Get] == 0) { |
| 420 | Diag(Loc, diag::err_ms_property_no_getter_or_putter); |
| 421 | break; |
| 422 | } |
| 423 | |
| 424 | Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor); |
| 425 | break; |
| 426 | } |
| 427 | |
| 428 | AccessorKind Kind; |
| 429 | SourceLocation KindLoc = Tok.getLocation(); |
| 430 | StringRef KindStr = Tok.getIdentifierInfo()->getName(); |
| 431 | if (KindStr == "get") { |
| 432 | Kind = AK_Get; |
| 433 | } else if (KindStr == "put") { |
| 434 | Kind = AK_Put; |
| 435 | |
| 436 | // Recover from the common mistake of using 'set' instead of 'put'. |
| 437 | } else if (KindStr == "set") { |
| 438 | Diag(KindLoc, diag::err_ms_property_has_set_accessor) |
| 439 | << FixItHint::CreateReplacement(KindLoc, "put"); |
| 440 | Kind = AK_Put; |
| 441 | |
| 442 | // Handle the mistake of forgetting the accessor kind by skipping |
| 443 | // this accessor. |
| 444 | } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) { |
| 445 | Diag(KindLoc, diag::err_ms_property_missing_accessor_kind); |
| 446 | ConsumeToken(); |
| 447 | HasInvalidAccessor = true; |
| 448 | goto next_property_accessor; |
| 449 | |
| 450 | // Otherwise, complain about the unknown accessor kind. |
| 451 | } else { |
| 452 | Diag(KindLoc, diag::err_ms_property_unknown_accessor); |
| 453 | HasInvalidAccessor = true; |
| 454 | Kind = AK_Invalid; |
| 455 | |
| 456 | // Try to keep parsing unless it doesn't look like an accessor spec. |
| 457 | if (!NextToken().is(tok::equal)) break; |
| 458 | } |
| 459 | |
| 460 | // Consume the identifier. |
| 461 | ConsumeToken(); |
| 462 | |
| 463 | // Consume the '='. |
| 464 | if (Tok.is(tok::equal)) { |
| 465 | ConsumeToken(); |
| 466 | } else { |
| 467 | Diag(Tok.getLocation(), diag::err_ms_property_expected_equal) |
| 468 | << KindStr; |
| 469 | break; |
| 470 | } |
| 471 | |
| 472 | // Expect the method name. |
| 473 | if (!Tok.is(tok::identifier)) { |
| 474 | Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name); |
| 475 | break; |
| 476 | } |
| 477 | |
| 478 | if (Kind == AK_Invalid) { |
| 479 | // Just drop invalid accessors. |
| 480 | } else if (AccessorNames[Kind] != NULL) { |
| 481 | // Complain about the repeated accessor, ignore it, and keep parsing. |
| 482 | Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr; |
| 483 | } else { |
| 484 | AccessorNames[Kind] = Tok.getIdentifierInfo(); |
| 485 | } |
| 486 | ConsumeToken(); |
| 487 | |
| 488 | next_property_accessor: |
| 489 | // Keep processing accessors until we run out. |
| 490 | if (Tok.is(tok::comma)) { |
| 491 | ConsumeAnyToken(); |
| 492 | continue; |
| 493 | |
| 494 | // If we run into the ')', stop without consuming it. |
| 495 | } else if (Tok.is(tok::r_paren)) { |
| 496 | break; |
| 497 | } else { |
| 498 | Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen); |
| 499 | break; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | // Only add the property attribute if it was well-formed. |
| 504 | if (!HasInvalidAccessor) { |
| 505 | Attrs.addNewPropertyAttr(Ident, Loc, 0, SourceLocation(), 0, |
| 506 | SourceLocation(), |
| 507 | AccessorNames[AK_Get], AccessorNames[AK_Put], |
| 508 | AttributeList::AS_Declspec); |
| 509 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 510 | T.skipToEnd(); |
| 511 | } else { |
| 512 | // We don't recognize this as a valid declspec, but instead of creating the |
| 513 | // attribute and allowing sema to warn about it, we will warn here instead. |
| 514 | // This is because some attributes have multiple spellings, but we need to |
| 515 | // disallow that for declspecs (such as align vs aligned). If we made the |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 516 | // attribute, we'd have to split the valid declspec spelling logic into |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 517 | // both locations. |
| 518 | Diag(Loc, diag::warn_ms_declspec_unknown) << Ident; |
| 519 | |
| 520 | // If there's an open paren, we should eat the open and close parens under |
| 521 | // the assumption that this unknown declspec has parameters. |
| 522 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 523 | if (!T.consumeOpen()) |
| 524 | T.skipToEnd(); |
| 525 | } |
| 526 | } |
| 527 | |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 528 | /// [MS] decl-specifier: |
| 529 | /// __declspec ( extended-decl-modifier-seq ) |
| 530 | /// |
| 531 | /// [MS] extended-decl-modifier-seq: |
| 532 | /// extended-decl-modifier[opt] |
| 533 | /// extended-decl-modifier extended-decl-modifier-seq |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 534 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) { |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 535 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 536 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 537 | ConsumeToken(); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 538 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 539 | if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec", |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 540 | tok::r_paren)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 541 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 542 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 543 | // An empty declspec is perfectly legal and should not warn. Additionally, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 544 | // you can specify multiple attributes per declspec. |
| 545 | while (Tok.getKind() != tok::r_paren) { |
| 546 | // We expect either a well-known identifier or a generic string. Anything |
| 547 | // else is a malformed declspec. |
| 548 | bool IsString = Tok.getKind() == tok::string_literal ? true : false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 549 | if (!IsString && Tok.getKind() != tok::identifier && |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 550 | Tok.getKind() != tok::kw_restrict) { |
| 551 | Diag(Tok, diag::err_ms_declspec_type); |
| 552 | T.skipToEnd(); |
| 553 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 554 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 555 | |
| 556 | IdentifierInfo *AttrName; |
| 557 | SourceLocation AttrNameLoc; |
| 558 | if (IsString) { |
| 559 | SmallString<8> StrBuffer; |
| 560 | bool Invalid = false; |
| 561 | StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid); |
| 562 | if (Invalid) { |
| 563 | T.skipToEnd(); |
| 564 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 565 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 566 | AttrName = PP.getIdentifierInfo(Str); |
| 567 | AttrNameLoc = ConsumeStringToken(); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 568 | } else { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 569 | AttrName = Tok.getIdentifierInfo(); |
| 570 | AttrNameLoc = ConsumeToken(); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 571 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 572 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 573 | if (IsString || IsSimpleMicrosoftDeclSpec(AttrName)) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 574 | // If we have a generic string, we will allow it because there is no |
| 575 | // documented list of allowable string declspecs, but we know they exist |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 576 | // (for instance, SAL declspecs in older versions of MSVC). |
| 577 | // |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 578 | // Alternatively, if the identifier is a simple one, then it requires no |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 579 | // arguments and can be turned into an attribute directly. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 580 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 581 | 0, 0, AttributeList::AS_Declspec); |
| 582 | else |
| 583 | ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 584 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 585 | T.consumeClose(); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 586 | } |
| 587 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 588 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 589 | // Treat these like attributes |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 590 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 591 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 592 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
Chad Rosier | ccbb402 | 2012-12-21 21:27:13 +0000 | [diff] [blame] | 593 | Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 594 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 595 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 596 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 597 | SourceLocation(), 0, 0, AttributeList::AS_Keyword); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 598 | } |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 599 | } |
| 600 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 601 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 602 | // Treat these like attributes |
| 603 | while (Tok.is(tok::kw___pascal)) { |
| 604 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 605 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 606 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 607 | SourceLocation(), 0, 0, AttributeList::AS_Keyword); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 608 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 611 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 612 | // Treat these like attributes |
| 613 | while (Tok.is(tok::kw___kernel)) { |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 614 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 615 | SourceLocation AttrNameLoc = ConsumeToken(); |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 616 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 617 | SourceLocation(), 0, 0, AttributeList::AS_Keyword); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 618 | } |
| 619 | } |
| 620 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 621 | void Parser::ParseOpenCLQualifiers(DeclSpec &DS) { |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 622 | // FIXME: The mapping from attribute spelling to semantics should be |
| 623 | // performed in Sema, not here. |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 624 | SourceLocation Loc = Tok.getLocation(); |
| 625 | switch(Tok.getKind()) { |
| 626 | // OpenCL qualifiers: |
| 627 | case tok::kw___private: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 628 | case tok::kw_private: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 629 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 630 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 631 | PP.getIdentifierInfo("address_space"), Loc, 0); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 632 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 633 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 634 | case tok::kw___global: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 635 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 636 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 637 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 638 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 639 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 640 | case tok::kw___local: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 641 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 642 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 643 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local); |
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___constant: |
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_constant); |
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___read_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 653 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 654 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 655 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only); |
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___write_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 659 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 660 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 661 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only); |
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_write: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 665 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +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_write); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 668 | break; |
| 669 | default: break; |
| 670 | } |
| 671 | } |
| 672 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 673 | /// \brief Parse a version number. |
| 674 | /// |
| 675 | /// version: |
| 676 | /// simple-integer |
| 677 | /// simple-integer ',' simple-integer |
| 678 | /// simple-integer ',' simple-integer ',' simple-integer |
| 679 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 680 | Range = Tok.getLocation(); |
| 681 | |
| 682 | if (!Tok.is(tok::numeric_constant)) { |
| 683 | Diag(Tok, diag::err_expected_version); |
| 684 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 685 | return VersionTuple(); |
| 686 | } |
| 687 | |
| 688 | // Parse the major (and possibly minor and subminor) versions, which |
| 689 | // are stored in the numeric constant. We utilize a quirk of the |
| 690 | // lexer, which is that it handles something like 1.2.3 as a single |
| 691 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 692 | SmallString<512> Buffer; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 693 | Buffer.resize(Tok.getLength()+1); |
| 694 | const char *ThisTokBegin = &Buffer[0]; |
| 695 | |
| 696 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 697 | bool Invalid = false; |
| 698 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 699 | if (Invalid) |
| 700 | return VersionTuple(); |
| 701 | |
| 702 | // Parse the major version. |
| 703 | unsigned AfterMajor = 0; |
| 704 | unsigned Major = 0; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 705 | while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 706 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 707 | ++AfterMajor; |
| 708 | } |
| 709 | |
| 710 | if (AfterMajor == 0) { |
| 711 | Diag(Tok, diag::err_expected_version); |
| 712 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 713 | return VersionTuple(); |
| 714 | } |
| 715 | |
| 716 | if (AfterMajor == ActualLength) { |
| 717 | ConsumeToken(); |
| 718 | |
| 719 | // We only had a single version component. |
| 720 | if (Major == 0) { |
| 721 | Diag(Tok, diag::err_zero_version); |
| 722 | return VersionTuple(); |
| 723 | } |
| 724 | |
| 725 | return VersionTuple(Major); |
| 726 | } |
| 727 | |
| 728 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 729 | Diag(Tok, diag::err_expected_version); |
| 730 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 731 | return VersionTuple(); |
| 732 | } |
| 733 | |
| 734 | // Parse the minor version. |
| 735 | unsigned AfterMinor = AfterMajor + 1; |
| 736 | unsigned Minor = 0; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 737 | while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 738 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 739 | ++AfterMinor; |
| 740 | } |
| 741 | |
| 742 | if (AfterMinor == ActualLength) { |
| 743 | ConsumeToken(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 744 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 745 | // We had major.minor. |
| 746 | if (Major == 0 && Minor == 0) { |
| 747 | Diag(Tok, diag::err_zero_version); |
| 748 | return VersionTuple(); |
| 749 | } |
| 750 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 751 | return VersionTuple(Major, Minor); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | // If what follows is not a '.', we have a problem. |
| 755 | if (ThisTokBegin[AfterMinor] != '.') { |
| 756 | Diag(Tok, diag::err_expected_version); |
| 757 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 758 | return VersionTuple(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | // Parse the subminor version. |
| 762 | unsigned AfterSubminor = AfterMinor + 1; |
| 763 | unsigned Subminor = 0; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 764 | while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 765 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 766 | ++AfterSubminor; |
| 767 | } |
| 768 | |
| 769 | if (AfterSubminor != ActualLength) { |
| 770 | Diag(Tok, diag::err_expected_version); |
| 771 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 772 | return VersionTuple(); |
| 773 | } |
| 774 | ConsumeToken(); |
| 775 | return VersionTuple(Major, Minor, Subminor); |
| 776 | } |
| 777 | |
| 778 | /// \brief Parse the contents of the "availability" attribute. |
| 779 | /// |
| 780 | /// availability-attribute: |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 781 | /// 'availability' '(' platform ',' version-arg-list, opt-message')' |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 782 | /// |
| 783 | /// platform: |
| 784 | /// identifier |
| 785 | /// |
| 786 | /// version-arg-list: |
| 787 | /// version-arg |
| 788 | /// version-arg ',' version-arg-list |
| 789 | /// |
| 790 | /// version-arg: |
| 791 | /// 'introduced' '=' version |
| 792 | /// 'deprecated' '=' version |
Douglas Gregor | 93a7067 | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 793 | /// 'obsoleted' = version |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 794 | /// 'unavailable' |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 795 | /// opt-message: |
| 796 | /// 'message' '=' <string> |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 797 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 798 | SourceLocation AvailabilityLoc, |
| 799 | ParsedAttributes &attrs, |
| 800 | SourceLocation *endLoc) { |
| 801 | SourceLocation PlatformLoc; |
| 802 | IdentifierInfo *Platform = 0; |
| 803 | |
| 804 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 805 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 806 | ExprResult MessageExpr; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 807 | |
| 808 | // Opening '('. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 809 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 810 | if (T.consumeOpen()) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 811 | Diag(Tok, diag::err_expected_lparen); |
| 812 | return; |
| 813 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 814 | |
| 815 | // Parse the platform name, |
| 816 | if (Tok.isNot(tok::identifier)) { |
| 817 | Diag(Tok, diag::err_availability_expected_platform); |
| 818 | SkipUntil(tok::r_paren); |
| 819 | return; |
| 820 | } |
| 821 | Platform = Tok.getIdentifierInfo(); |
| 822 | PlatformLoc = ConsumeToken(); |
| 823 | |
| 824 | // Parse the ',' following the platform name. |
| 825 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren)) |
| 826 | return; |
| 827 | |
| 828 | // If we haven't grabbed the pointers for the identifiers |
| 829 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 830 | if (!Ident_introduced) { |
| 831 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 832 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 833 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 834 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 835 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 836 | } |
| 837 | |
| 838 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 839 | SourceLocation UnavailableLoc; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 840 | do { |
| 841 | if (Tok.isNot(tok::identifier)) { |
| 842 | Diag(Tok, diag::err_availability_expected_change); |
| 843 | SkipUntil(tok::r_paren); |
| 844 | return; |
| 845 | } |
| 846 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 847 | SourceLocation KeywordLoc = ConsumeToken(); |
| 848 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 849 | if (Keyword == Ident_unavailable) { |
| 850 | if (UnavailableLoc.isValid()) { |
| 851 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 852 | << Keyword << SourceRange(UnavailableLoc); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 853 | } |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 854 | UnavailableLoc = KeywordLoc; |
| 855 | |
| 856 | if (Tok.isNot(tok::comma)) |
| 857 | break; |
| 858 | |
| 859 | ConsumeToken(); |
| 860 | continue; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 863 | if (Tok.isNot(tok::equal)) { |
| 864 | Diag(Tok, diag::err_expected_equal_after) |
| 865 | << Keyword; |
| 866 | SkipUntil(tok::r_paren); |
| 867 | return; |
| 868 | } |
| 869 | ConsumeToken(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 870 | if (Keyword == Ident_message) { |
| 871 | if (!isTokenStringLiteral()) { |
Andy Gibbs | 97f8461 | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 872 | Diag(Tok, diag::err_expected_string_literal) |
| 873 | << /*Source='availability attribute'*/2; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 874 | SkipUntil(tok::r_paren); |
| 875 | return; |
| 876 | } |
| 877 | MessageExpr = ParseStringLiteralExpression(); |
| 878 | break; |
| 879 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 880 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 881 | SourceRange VersionRange; |
| 882 | VersionTuple Version = ParseVersionTuple(VersionRange); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 883 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 884 | if (Version.empty()) { |
| 885 | SkipUntil(tok::r_paren); |
| 886 | return; |
| 887 | } |
| 888 | |
| 889 | unsigned Index; |
| 890 | if (Keyword == Ident_introduced) |
| 891 | Index = Introduced; |
| 892 | else if (Keyword == Ident_deprecated) |
| 893 | Index = Deprecated; |
| 894 | else if (Keyword == Ident_obsoleted) |
| 895 | Index = Obsoleted; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 896 | else |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 897 | Index = Unknown; |
| 898 | |
| 899 | if (Index < Unknown) { |
| 900 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 901 | Diag(KeywordLoc, diag::err_availability_redundant) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 902 | << Keyword |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 903 | << SourceRange(Changes[Index].KeywordLoc, |
| 904 | Changes[Index].VersionRange.getEnd()); |
| 905 | } |
| 906 | |
| 907 | Changes[Index].KeywordLoc = KeywordLoc; |
| 908 | Changes[Index].Version = Version; |
| 909 | Changes[Index].VersionRange = VersionRange; |
| 910 | } else { |
| 911 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 912 | << Keyword << VersionRange; |
| 913 | } |
| 914 | |
| 915 | if (Tok.isNot(tok::comma)) |
| 916 | break; |
| 917 | |
| 918 | ConsumeToken(); |
| 919 | } while (true); |
| 920 | |
| 921 | // Closing ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 922 | if (T.consumeClose()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 923 | return; |
| 924 | |
| 925 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 926 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 927 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 928 | // The 'unavailable' availability cannot be combined with any other |
| 929 | // availability changes. Make sure that hasn't happened. |
| 930 | if (UnavailableLoc.isValid()) { |
| 931 | bool Complained = false; |
| 932 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 933 | if (Changes[Index].KeywordLoc.isValid()) { |
| 934 | if (!Complained) { |
| 935 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 936 | << SourceRange(Changes[Index].KeywordLoc, |
| 937 | Changes[Index].VersionRange.getEnd()); |
| 938 | Complained = true; |
| 939 | } |
| 940 | |
| 941 | // Clear out the availability. |
| 942 | Changes[Index] = AvailabilityChange(); |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 947 | // Record this attribute |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 948 | attrs.addNew(&Availability, |
| 949 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Fariborz Jahanian | f96708d | 2012-01-23 23:38:32 +0000 | [diff] [blame] | 950 | 0, AvailabilityLoc, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 951 | Platform, PlatformLoc, |
| 952 | Changes[Introduced], |
| 953 | Changes[Deprecated], |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 954 | Changes[Obsoleted], |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 955 | UnavailableLoc, MessageExpr.take(), |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 956 | AttributeList::AS_GNU); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 957 | } |
| 958 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 959 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 960 | // Late Parsed Attributes: |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 961 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 962 | |
| 963 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 964 | |
| 965 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 966 | Self->ParseLexedAttributes(*Class); |
| 967 | } |
| 968 | |
| 969 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 970 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 974 | /// scope appropriately. |
| 975 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 976 | // Deal with templates |
| 977 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 978 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 979 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 980 | HasTemplateScope); |
| 981 | if (HasTemplateScope) |
| 982 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 983 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 984 | // Set or update the scope flags. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 985 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 986 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 987 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 988 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 989 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 990 | // Enter the scope of nested classes |
| 991 | if (!AlreadyHasClassScope) |
| 992 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 993 | Class.TagOrTemplate); |
Benjamin Kramer | 268efba | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 994 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 995 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 996 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 997 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 998 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 999 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 1000 | if (!AlreadyHasClassScope) |
| 1001 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 1002 | Class.TagOrTemplate); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1005 | |
| 1006 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 1007 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 1008 | bool EnterScope, bool OnDefinition) { |
DeLesley Hutchins | 161db02 | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1009 | assert(LAs.parseSoon() && |
| 1010 | "Attribute list should be marked for immediate parsing."); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1011 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 95526a4 | 2012-08-15 22:41:04 +0000 | [diff] [blame] | 1012 | if (D) |
| 1013 | LAs[i]->addDecl(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1014 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | d306cf7 | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 1015 | delete LAs[i]; |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1016 | } |
| 1017 | LAs.clear(); |
| 1018 | } |
| 1019 | |
| 1020 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1021 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 1022 | /// This will be called at the end of parsing a class declaration |
| 1023 | /// for each LateParsedAttribute. We consume the saved tokens and |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1024 | /// create an attribute with the arguments filled in. We add this |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1025 | /// to the Attribute list for the decl. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1026 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 1027 | bool EnterScope, bool OnDefinition) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1028 | // Save the current token position. |
| 1029 | SourceLocation OrigLoc = Tok.getLocation(); |
| 1030 | |
| 1031 | // Append the current token at the end of the new token stream so that it |
| 1032 | // doesn't get lost. |
| 1033 | LA.Toks.push_back(Tok); |
| 1034 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 1035 | // Consume the previously pushed token. |
Argyrios Kyrtzidis | ab2d09b | 2013-03-27 23:58:17 +0000 | [diff] [blame] | 1036 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1037 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1038 | if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) { |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 1039 | // FIXME: Do not warn on C++11 attributes, once we start supporting |
| 1040 | // them here. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1041 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 1042 | << LA.AttrName.getName(); |
| 1043 | } |
| 1044 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1045 | ParsedAttributes Attrs(AttrFactory); |
| 1046 | SourceLocation endLoc; |
| 1047 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1048 | if (LA.Decls.size() > 0) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1049 | Decl *D = LA.Decls[0]; |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1050 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 1051 | RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1052 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1053 | // Allow 'this' within late-parsed attributes. |
| 1054 | Sema::CXXThisScopeRAII ThisScope(Actions, RD, |
| 1055 | /*TypeQuals=*/0, |
| 1056 | ND && RD && ND->isCXXInstanceMember()); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1057 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1058 | if (LA.Decls.size() == 1) { |
| 1059 | // If the Decl is templatized, add template parameters to scope. |
| 1060 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 1061 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 1062 | if (HasTemplateScope) |
| 1063 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1064 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1065 | // If the Decl is on a function, add function parameters to the scope. |
| 1066 | bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
| 1067 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope); |
| 1068 | if (HasFunScope) |
| 1069 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1070 | |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1071 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 1072 | 0, SourceLocation(), AttributeList::AS_GNU); |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 1073 | |
| 1074 | if (HasFunScope) { |
| 1075 | Actions.ActOnExitFunctionContext(); |
| 1076 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 1077 | } |
| 1078 | if (HasTemplateScope) { |
| 1079 | TempScope.Exit(); |
| 1080 | } |
| 1081 | } else { |
| 1082 | // If there are multiple decls, then the decl cannot be within the |
| 1083 | // function scope. |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 1084 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 1085 | 0, SourceLocation(), AttributeList::AS_GNU); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1086 | } |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 1087 | } else { |
| 1088 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 1089 | } |
| 1090 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1091 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) { |
| 1092 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
| 1093 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1094 | |
| 1095 | if (Tok.getLocation() != OrigLoc) { |
| 1096 | // Due to a parsing error, we either went over the cached tokens or |
| 1097 | // there are still cached tokens left, so we skip the leftover tokens. |
| 1098 | // Since this is an uncommon situation that should be avoided, use the |
| 1099 | // expensive isBeforeInTranslationUnit call. |
| 1100 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 1101 | OrigLoc)) |
| 1102 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
Douglas Gregor | d78ef5b | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 1103 | ConsumeAnyToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1107 | /// \brief Wrapper around a case statement checking if AttrName is |
| 1108 | /// one of the thread safety attributes |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1109 | bool Parser::IsThreadSafetyAttribute(StringRef AttrName) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1110 | return llvm::StringSwitch<bool>(AttrName) |
| 1111 | .Case("guarded_by", true) |
| 1112 | .Case("guarded_var", true) |
| 1113 | .Case("pt_guarded_by", true) |
| 1114 | .Case("pt_guarded_var", true) |
| 1115 | .Case("lockable", true) |
| 1116 | .Case("scoped_lockable", true) |
| 1117 | .Case("no_thread_safety_analysis", true) |
| 1118 | .Case("acquired_after", true) |
| 1119 | .Case("acquired_before", true) |
| 1120 | .Case("exclusive_lock_function", true) |
| 1121 | .Case("shared_lock_function", true) |
| 1122 | .Case("exclusive_trylock_function", true) |
| 1123 | .Case("shared_trylock_function", true) |
| 1124 | .Case("unlock_function", true) |
| 1125 | .Case("lock_returned", true) |
| 1126 | .Case("locks_excluded", true) |
| 1127 | .Case("exclusive_locks_required", true) |
| 1128 | .Case("shared_locks_required", true) |
| 1129 | .Default(false); |
| 1130 | } |
| 1131 | |
| 1132 | /// \brief Parse the contents of thread safety attributes. These |
| 1133 | /// should always be parsed as an expression list. |
| 1134 | /// |
| 1135 | /// We need to special case the parsing due to the fact that if the first token |
| 1136 | /// of the first argument is an identifier, the main parse loop will store |
| 1137 | /// that token as a "parameter" and the rest of |
| 1138 | /// the arguments will be added to a list of "arguments". However, |
| 1139 | /// subsequent tokens in the first argument are lost. We instead parse each |
| 1140 | /// argument as an expression and add all arguments to the list of "arguments". |
| 1141 | /// In future, we will take advantage of this special case to also |
| 1142 | /// deal with some argument scoping issues here (for example, referring to a |
| 1143 | /// function parameter in the attribute on that function). |
| 1144 | void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName, |
| 1145 | SourceLocation AttrNameLoc, |
| 1146 | ParsedAttributes &Attrs, |
| 1147 | SourceLocation *EndLoc) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1148 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1149 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1150 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1151 | T.consumeOpen(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1152 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1153 | ExprVector ArgExprs; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1154 | bool ArgExprsOk = true; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1155 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1156 | // now parse the list of expressions |
DeLesley Hutchins | 4805f15 | 2011-12-14 19:36:06 +0000 | [diff] [blame] | 1157 | while (Tok.isNot(tok::r_paren)) { |
DeLesley Hutchins | ed4330b | 2013-02-07 19:01:07 +0000 | [diff] [blame] | 1158 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1159 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 1160 | if (ArgExpr.isInvalid()) { |
| 1161 | ArgExprsOk = false; |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1162 | T.consumeClose(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1163 | break; |
| 1164 | } else { |
| 1165 | ArgExprs.push_back(ArgExpr.release()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1166 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1167 | if (Tok.isNot(tok::comma)) |
| 1168 | break; |
| 1169 | ConsumeToken(); // Eat the comma, move to the next argument |
| 1170 | } |
| 1171 | // Match the ')'. |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 1172 | if (ArgExprsOk && !T.consumeClose()) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1173 | Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1174 | ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1175 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1176 | if (EndLoc) |
| 1177 | *EndLoc = T.getCloseLocation(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1178 | } |
| 1179 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1180 | void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName, |
| 1181 | SourceLocation AttrNameLoc, |
| 1182 | ParsedAttributes &Attrs, |
| 1183 | SourceLocation *EndLoc) { |
| 1184 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 1185 | |
| 1186 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1187 | T.consumeOpen(); |
| 1188 | |
| 1189 | if (Tok.isNot(tok::identifier)) { |
| 1190 | Diag(Tok, diag::err_expected_ident); |
| 1191 | T.skipToEnd(); |
| 1192 | return; |
| 1193 | } |
| 1194 | IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo(); |
| 1195 | SourceLocation ArgumentKindLoc = ConsumeToken(); |
| 1196 | |
| 1197 | if (Tok.isNot(tok::comma)) { |
| 1198 | Diag(Tok, diag::err_expected_comma); |
| 1199 | T.skipToEnd(); |
| 1200 | return; |
| 1201 | } |
| 1202 | ConsumeToken(); |
| 1203 | |
| 1204 | SourceRange MatchingCTypeRange; |
| 1205 | TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange); |
| 1206 | if (MatchingCType.isInvalid()) { |
| 1207 | T.skipToEnd(); |
| 1208 | return; |
| 1209 | } |
| 1210 | |
| 1211 | bool LayoutCompatible = false; |
| 1212 | bool MustBeNull = false; |
| 1213 | while (Tok.is(tok::comma)) { |
| 1214 | ConsumeToken(); |
| 1215 | if (Tok.isNot(tok::identifier)) { |
| 1216 | Diag(Tok, diag::err_expected_ident); |
| 1217 | T.skipToEnd(); |
| 1218 | return; |
| 1219 | } |
| 1220 | IdentifierInfo *Flag = Tok.getIdentifierInfo(); |
| 1221 | if (Flag->isStr("layout_compatible")) |
| 1222 | LayoutCompatible = true; |
| 1223 | else if (Flag->isStr("must_be_null")) |
| 1224 | MustBeNull = true; |
| 1225 | else { |
| 1226 | Diag(Tok, diag::err_type_safety_unknown_flag) << Flag; |
| 1227 | T.skipToEnd(); |
| 1228 | return; |
| 1229 | } |
| 1230 | ConsumeToken(); // consume flag |
| 1231 | } |
| 1232 | |
| 1233 | if (!T.consumeClose()) { |
| 1234 | Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 1235 | ArgumentKind, ArgumentKindLoc, |
| 1236 | MatchingCType.release(), LayoutCompatible, |
| 1237 | MustBeNull, AttributeList::AS_GNU); |
| 1238 | } |
| 1239 | |
| 1240 | if (EndLoc) |
| 1241 | *EndLoc = T.getCloseLocation(); |
| 1242 | } |
| 1243 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1244 | /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets |
| 1245 | /// of a C++11 attribute-specifier in a location where an attribute is not |
| 1246 | /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this |
| 1247 | /// situation. |
| 1248 | /// |
| 1249 | /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if |
| 1250 | /// this doesn't appear to actually be an attribute-specifier, and the caller |
| 1251 | /// should try to parse it. |
| 1252 | bool Parser::DiagnoseProhibitedCXX11Attribute() { |
| 1253 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)); |
| 1254 | |
| 1255 | switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) { |
| 1256 | case CAK_NotAttributeSpecifier: |
| 1257 | // No diagnostic: we're in Obj-C++11 and this is not actually an attribute. |
| 1258 | return false; |
| 1259 | |
| 1260 | case CAK_InvalidAttributeSpecifier: |
| 1261 | Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute); |
| 1262 | return false; |
| 1263 | |
| 1264 | case CAK_AttributeSpecifier: |
| 1265 | // Parse and discard the attributes. |
| 1266 | SourceLocation BeginLoc = ConsumeBracket(); |
| 1267 | ConsumeBracket(); |
| 1268 | SkipUntil(tok::r_square, /*StopAtSemi*/ false); |
| 1269 | assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied"); |
| 1270 | SourceLocation EndLoc = ConsumeBracket(); |
| 1271 | Diag(BeginLoc, diag::err_attributes_not_allowed) |
| 1272 | << SourceRange(BeginLoc, EndLoc); |
| 1273 | return true; |
| 1274 | } |
Chandler Carruth | 2c6dbd7 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 1275 | llvm_unreachable("All cases handled above."); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
Richard Smith | 975d52c | 2013-02-20 01:17:14 +0000 | [diff] [blame] | 1278 | /// \brief We have found the opening square brackets of a C++11 |
| 1279 | /// attribute-specifier in a location where an attribute is not permitted, but |
| 1280 | /// we know where the attributes ought to be written. Parse them anyway, and |
| 1281 | /// provide a fixit moving them to the right place. |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1282 | void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs, |
| 1283 | SourceLocation CorrectLocation) { |
| 1284 | assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) || |
| 1285 | Tok.is(tok::kw_alignas)); |
| 1286 | |
| 1287 | // Consume the attributes. |
| 1288 | SourceLocation Loc = Tok.getLocation(); |
| 1289 | ParseCXX11Attributes(Attrs); |
| 1290 | CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true); |
| 1291 | |
| 1292 | Diag(Loc, diag::err_attributes_not_allowed) |
| 1293 | << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange) |
| 1294 | << FixItHint::CreateRemoval(AttrRange); |
| 1295 | } |
| 1296 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1297 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 1298 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 1299 | << attrs.Range; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1302 | void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) { |
| 1303 | AttributeList *AttrList = attrs.getList(); |
| 1304 | while (AttrList) { |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1305 | if (AttrList->isCXX11Attribute()) { |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 1306 | Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr) |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1307 | << AttrList->getName(); |
| 1308 | AttrList->setInvalid(); |
| 1309 | } |
| 1310 | AttrList = AttrList->getNext(); |
| 1311 | } |
| 1312 | } |
| 1313 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1314 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 1315 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1316 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 1317 | /// location of the semicolon in DeclEnd. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1318 | /// |
| 1319 | /// declaration: [C99 6.7] |
| 1320 | /// block-declaration -> |
| 1321 | /// simple-declaration |
| 1322 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1323 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1324 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1325 | /// [C++] using-directive |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1326 | /// [C++] using-declaration |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1327 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1328 | /// others... [FIXME] |
| 1329 | /// |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1330 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 1331 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1332 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1333 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 1334 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | e8cff36 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 1335 | // Must temporarily exit the objective-c container scope for |
| 1336 | // parsing c none objective-c decls. |
| 1337 | ObjCDeclContextSwitch ObjCDC(*this); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1338 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1339 | Decl *SingleDecl = 0; |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1340 | Decl *OwnedType = 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1341 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1342 | case tok::kw_template: |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1343 | case tok::kw_export: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1344 | ProhibitAttributes(attrs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1345 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1346 | break; |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1347 | case tok::kw_inline: |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 1348 | // 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] | 1349 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1350 | ProhibitAttributes(attrs); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1351 | SourceLocation InlineLoc = ConsumeToken(); |
| 1352 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 1353 | break; |
| 1354 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1355 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1356 | true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1357 | case tok::kw_namespace: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1358 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1359 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1360 | break; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1361 | case tok::kw_using: |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1362 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1363 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1364 | break; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1365 | case tok::kw_static_assert: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1366 | case tok::kw__Static_assert: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1367 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1368 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1369 | break; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1370 | default: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1371 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1372 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1373 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1374 | // 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] | 1375 | // single decl, convert it now. Alias declarations can also declare a type; |
| 1376 | // include that too if it is present. |
| 1377 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1381 | /// declaration-specifiers init-declarator-list[opt] ';' |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1382 | /// [C++11] attribute-specifier-seq decl-specifier-seq[opt] |
| 1383 | /// init-declarator-list ';' |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1384 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 1385 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1386 | /// |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1387 | /// for-range-declaration: [C++11 6.5p1: stmt.ranged] |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1388 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1389 | /// |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1390 | /// 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] | 1391 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1392 | /// |
| 1393 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 1394 | /// of a simple-declaration. If we find that we are, we also parse the |
| 1395 | /// for-range-initializer, and place it here. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1396 | Parser::DeclGroupPtrTy |
| 1397 | Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context, |
| 1398 | SourceLocation &DeclEnd, |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1399 | ParsedAttributesWithRange &Attrs, |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1400 | bool RequireSemi, ForRangeInit *FRI) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1401 | // Parse the common declaration-specifiers piece. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1402 | ParsingDeclSpec DS(*this); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1403 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1404 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1405 | getDeclSpecContextFromDeclaratorContext(Context)); |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1406 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1407 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1408 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1409 | if (Tok.is(tok::semi)) { |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1410 | ProhibitAttributes(Attrs); |
Argyrios Kyrtzidis | 5641b0d | 2012-05-16 23:49:15 +0000 | [diff] [blame] | 1411 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1412 | if (RequireSemi) ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1413 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1414 | DS); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1415 | DS.complete(TheDecl); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1416 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1417 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1418 | |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1419 | DS.takeAttributesFrom(Attrs); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1420 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1421 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1422 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1423 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1424 | /// for a declarator. |
| 1425 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1426 | switch (Tok.getKind()) { |
| 1427 | case tok::annot_cxxscope: |
| 1428 | case tok::annot_template_id: |
| 1429 | case tok::caret: |
| 1430 | case tok::code_completion: |
| 1431 | case tok::coloncolon: |
| 1432 | case tok::ellipsis: |
| 1433 | case tok::kw___attribute: |
| 1434 | case tok::kw_operator: |
| 1435 | case tok::l_paren: |
| 1436 | case tok::star: |
| 1437 | return true; |
| 1438 | |
| 1439 | case tok::amp: |
| 1440 | case tok::ampamp: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1441 | return getLangOpts().CPlusPlus; |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1442 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1443 | 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] | 1444 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 && |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1445 | NextToken().is(tok::l_square); |
| 1446 | |
| 1447 | 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] | 1448 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1449 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1450 | case tok::identifier: |
| 1451 | switch (NextToken().getKind()) { |
| 1452 | case tok::code_completion: |
| 1453 | case tok::coloncolon: |
| 1454 | case tok::comma: |
| 1455 | case tok::equal: |
| 1456 | case tok::equalequal: // Might be a typo for '='. |
| 1457 | case tok::kw_alignas: |
| 1458 | case tok::kw_asm: |
| 1459 | case tok::kw___attribute: |
| 1460 | case tok::l_brace: |
| 1461 | case tok::l_paren: |
| 1462 | case tok::l_square: |
| 1463 | case tok::less: |
| 1464 | case tok::r_brace: |
| 1465 | case tok::r_paren: |
| 1466 | case tok::r_square: |
| 1467 | case tok::semi: |
| 1468 | return true; |
| 1469 | |
| 1470 | case tok::colon: |
| 1471 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1472 | // and in block scope it's probably a label. Inside a class definition, |
| 1473 | // this is a bit-field. |
| 1474 | return Context == Declarator::MemberContext || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1475 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1476 | |
| 1477 | case tok::identifier: // Possible virt-specifier. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1478 | return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken()); |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1479 | |
| 1480 | default: |
| 1481 | return false; |
| 1482 | } |
| 1483 | |
| 1484 | default: |
| 1485 | return false; |
| 1486 | } |
| 1487 | } |
| 1488 | |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1489 | /// Skip until we reach something which seems like a sensible place to pick |
| 1490 | /// up parsing after a malformed declaration. This will sometimes stop sooner |
| 1491 | /// than SkipUntil(tok::r_brace) would, but will never stop later. |
| 1492 | void Parser::SkipMalformedDecl() { |
| 1493 | while (true) { |
| 1494 | switch (Tok.getKind()) { |
| 1495 | case tok::l_brace: |
| 1496 | // Skip until matching }, then stop. We've probably skipped over |
| 1497 | // a malformed class or function definition or similar. |
| 1498 | ConsumeBrace(); |
| 1499 | SkipUntil(tok::r_brace, /*StopAtSemi*/false); |
| 1500 | if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) { |
| 1501 | // This declaration isn't over yet. Keep skipping. |
| 1502 | continue; |
| 1503 | } |
| 1504 | if (Tok.is(tok::semi)) |
| 1505 | ConsumeToken(); |
| 1506 | return; |
| 1507 | |
| 1508 | case tok::l_square: |
| 1509 | ConsumeBracket(); |
| 1510 | SkipUntil(tok::r_square, /*StopAtSemi*/false); |
| 1511 | continue; |
| 1512 | |
| 1513 | case tok::l_paren: |
| 1514 | ConsumeParen(); |
| 1515 | SkipUntil(tok::r_paren, /*StopAtSemi*/false); |
| 1516 | continue; |
| 1517 | |
| 1518 | case tok::r_brace: |
| 1519 | return; |
| 1520 | |
| 1521 | case tok::semi: |
| 1522 | ConsumeToken(); |
| 1523 | return; |
| 1524 | |
| 1525 | case tok::kw_inline: |
| 1526 | // 'inline namespace' at the start of a line is almost certainly |
Jordan Rose | 94f29f4 | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1527 | // a good place to pick back up parsing, except in an Objective-C |
| 1528 | // @interface context. |
| 1529 | if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) && |
| 1530 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1531 | return; |
| 1532 | break; |
| 1533 | |
| 1534 | case tok::kw_namespace: |
| 1535 | // 'namespace' at the start of a line is almost certainly a good |
Jordan Rose | 94f29f4 | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1536 | // place to pick back up parsing, except in an Objective-C |
| 1537 | // @interface context. |
| 1538 | if (Tok.isAtStartOfLine() && |
| 1539 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
| 1540 | return; |
| 1541 | break; |
| 1542 | |
| 1543 | case tok::at: |
| 1544 | // @end is very much like } in Objective-C contexts. |
| 1545 | if (NextToken().isObjCAtKeyword(tok::objc_end) && |
| 1546 | ParsingInObjCContainer) |
| 1547 | return; |
| 1548 | break; |
| 1549 | |
| 1550 | case tok::minus: |
| 1551 | case tok::plus: |
| 1552 | // - and + probably start new method declarations in Objective-C contexts. |
| 1553 | if (Tok.isAtStartOfLine() && ParsingInObjCContainer) |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1554 | return; |
| 1555 | break; |
| 1556 | |
| 1557 | case tok::eof: |
| 1558 | return; |
| 1559 | |
| 1560 | default: |
| 1561 | break; |
| 1562 | } |
| 1563 | |
| 1564 | ConsumeAnyToken(); |
| 1565 | } |
| 1566 | } |
| 1567 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1568 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1569 | /// definition or a group of object declarations, actually parse the |
| 1570 | /// result. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1571 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1572 | unsigned Context, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1573 | bool AllowFunctionDefinitions, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1574 | SourceLocation *DeclEnd, |
| 1575 | ForRangeInit *FRI) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1576 | // Parse the first declarator. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1577 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1578 | ParseDeclarator(D); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1579 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1580 | // Bail out if the first declarator didn't seem well-formed. |
| 1581 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1582 | SkipMalformedDecl(); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1583 | return DeclGroupPtrTy(); |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1584 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1585 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1586 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1587 | // appropriate function scope after the function Decl has been constructed. |
DeLesley Hutchins | 161db02 | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1588 | // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList. |
| 1589 | LateParsedAttrList LateParsedAttrs(true); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1590 | if (D.isFunctionDeclarator()) |
| 1591 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1592 | |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1593 | // 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^] | 1594 | if (D.isFunctionDeclarator() && |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1595 | // Look at the next token to make sure that this isn't a function |
| 1596 | // declaration. We have to check this because __attribute__ might be the |
| 1597 | // start of a function definition in GCC-extended K&R C. |
Fariborz Jahanian | be1d4ec | 2012-08-10 15:54:40 +0000 | [diff] [blame] | 1598 | !isDeclarationAfterDeclarator()) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1599 | |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame^] | 1600 | if (AllowFunctionDefinitions) { |
| 1601 | if (isStartOfFunctionDefinition(D)) { |
| 1602 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1603 | Diag(Tok, diag::err_function_declared_typedef); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1604 | |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame^] | 1605 | // Recover by treating the 'typedef' as spurious. |
| 1606 | DS.ClearStorageClassSpecs(); |
| 1607 | } |
| 1608 | |
| 1609 | Decl *TheDecl = |
| 1610 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
| 1611 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1612 | } |
| 1613 | |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame^] | 1614 | if (isDeclarationSpecifier()) { |
| 1615 | // If there is an invalid declaration specifier right after the function |
| 1616 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1617 | // actually a body. Just fall through into the code that handles it as a |
| 1618 | // prototype, and let the top-level code handle the erroneous declspec |
| 1619 | // where it would otherwise expect a comma or semicolon. |
| 1620 | } else { |
| 1621 | Diag(Tok, diag::err_expected_fn_body); |
| 1622 | SkipUntil(tok::semi); |
| 1623 | return DeclGroupPtrTy(); |
| 1624 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1625 | } else { |
Douglas Gregor | b004a8e | 2013-04-16 16:01:32 +0000 | [diff] [blame^] | 1626 | if (Tok.is(tok::l_brace)) { |
| 1627 | Diag(Tok, diag::err_function_definition_not_allowed); |
| 1628 | SkipUntil(tok::r_brace, true, true); |
| 1629 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1630 | } |
| 1631 | } |
| 1632 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1633 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1634 | return DeclGroupPtrTy(); |
| 1635 | |
| 1636 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1637 | // must parse and analyze the for-range-initializer before the declaration is |
| 1638 | // analyzed. |
Douglas Gregor | 12849d0 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1639 | // |
| 1640 | // Handle the Objective-C for-in loop variable similarly, although we |
| 1641 | // don't need to parse the container in advance. |
| 1642 | if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) { |
| 1643 | bool IsForRangeLoop = false; |
| 1644 | if (Tok.is(tok::colon)) { |
| 1645 | IsForRangeLoop = true; |
| 1646 | FRI->ColonLoc = ConsumeToken(); |
| 1647 | if (Tok.is(tok::l_brace)) |
| 1648 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1649 | else |
| 1650 | FRI->RangeExpr = ParseExpression(); |
| 1651 | } |
| 1652 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1653 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | 12849d0 | 2013-04-08 20:52:24 +0000 | [diff] [blame] | 1654 | if (IsForRangeLoop) |
| 1655 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1656 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | 6895a64 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 1657 | D.complete(ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1658 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1); |
| 1659 | } |
| 1660 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1661 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1662 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1663 | if (LateParsedAttrs.size() > 0) |
| 1664 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1665 | D.complete(FirstDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1666 | if (FirstDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1667 | DeclsInGroup.push_back(FirstDecl); |
| 1668 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1669 | bool ExpectSemi = Context != Declarator::ForContext; |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 1670 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1671 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1672 | // error, bail out. |
| 1673 | while (Tok.is(tok::comma)) { |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1674 | SourceLocation CommaLoc = ConsumeToken(); |
| 1675 | |
| 1676 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1677 | // This comma was followed by a line-break and something which can't be |
| 1678 | // the start of a declarator. The comma was probably a typo for a |
| 1679 | // semicolon. |
| 1680 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1681 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1682 | ExpectSemi = false; |
| 1683 | break; |
| 1684 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1685 | |
| 1686 | // Parse the next declarator. |
| 1687 | D.clear(); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1688 | D.setCommaLoc(CommaLoc); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1689 | |
| 1690 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1691 | // declaration, these would be part of the declspec. In subsequent |
| 1692 | // declarators, they become part of the declarator itself, so that they |
| 1693 | // don't apply to declarators after *this* one. Examples: |
| 1694 | // short __attribute__((common)) var; -> declspec |
| 1695 | // short var __attribute__((common)); -> declarator |
| 1696 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1697 | MaybeParseGNUAttributes(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1698 | |
| 1699 | ParseDeclarator(D); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1700 | if (!D.isInvalidType()) { |
| 1701 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1702 | D.complete(ThisDecl); |
| 1703 | if (ThisDecl) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1704 | DeclsInGroup.push_back(ThisDecl); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1705 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | if (DeclEnd) |
| 1709 | *DeclEnd = Tok.getLocation(); |
| 1710 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1711 | if (ExpectSemi && |
Chris Lattner | 8bb21d3 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 1712 | ExpectAndConsumeSemi(Context == Declarator::FileContext |
| 1713 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1714 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1715 | // Okay, there was no semicolon and one was expected. If we see a |
| 1716 | // declaration specifier, just assume it was missing and continue parsing. |
| 1717 | // Otherwise things are very confused and we skip to recover. |
| 1718 | if (!isDeclarationSpecifier()) { |
| 1719 | SkipUntil(tok::r_brace, true, true); |
| 1720 | if (Tok.is(tok::semi)) |
| 1721 | ConsumeToken(); |
| 1722 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1725 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1726 | DeclsInGroup.data(), |
| 1727 | DeclsInGroup.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1730 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1731 | /// declarator. Returns true on an error. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1732 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1733 | // If a simple-asm-expr is present, parse it. |
| 1734 | if (Tok.is(tok::kw_asm)) { |
| 1735 | SourceLocation Loc; |
| 1736 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1737 | if (AsmLabel.isInvalid()) { |
| 1738 | SkipUntil(tok::semi, true, true); |
| 1739 | return true; |
| 1740 | } |
| 1741 | |
| 1742 | D.setAsmLabel(AsmLabel.release()); |
| 1743 | D.SetRangeEnd(Loc); |
| 1744 | } |
| 1745 | |
| 1746 | MaybeParseGNUAttributes(D); |
| 1747 | return false; |
| 1748 | } |
| 1749 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1750 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1751 | /// declarator'. This method parses the remainder of the declaration |
| 1752 | /// (including any attributes or initializer, among other things) and |
| 1753 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1754 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1755 | /// init-declarator: [C99 6.7] |
| 1756 | /// declarator |
| 1757 | /// declarator '=' initializer |
| 1758 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1759 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1760 | /// [C++] declarator initializer[opt] |
| 1761 | /// |
| 1762 | /// [C++] initializer: |
| 1763 | /// [C++] '=' initializer-clause |
| 1764 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1765 | /// [C++0x] '=' 'default' [TODO] |
| 1766 | /// [C++0x] '=' 'delete' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1767 | /// [C++0x] braced-init-list |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1768 | /// |
| 1769 | /// According to the standard grammar, =default and =delete are function |
| 1770 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1771 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1772 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1773 | const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1774 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1775 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1776 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1777 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1778 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1779 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1780 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1781 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1782 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1783 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1784 | switch (TemplateInfo.Kind) { |
| 1785 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1786 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1787 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1788 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1789 | case ParsedTemplateInfo::Template: |
| 1790 | case ParsedTemplateInfo::ExplicitSpecialization: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1791 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1792 | *TemplateInfo.TemplateParams, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1793 | D); |
| 1794 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1795 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1796 | case ParsedTemplateInfo::ExplicitInstantiation: { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1797 | DeclResult ThisRes |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1798 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1799 | TemplateInfo.ExternLoc, |
| 1800 | TemplateInfo.TemplateLoc, |
| 1801 | D); |
| 1802 | if (ThisRes.isInvalid()) { |
| 1803 | SkipUntil(tok::semi, true, true); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1804 | return 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1805 | } |
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 | ThisDecl = ThisRes.get(); |
| 1808 | break; |
| 1809 | } |
| 1810 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1811 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1812 | bool TypeContainsAuto = |
| 1813 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
| 1814 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1815 | // Parse declarator '=' initializer. |
Richard Trieu | d6c7c67 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1816 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | fcaf27e | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1817 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1818 | ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1819 | if (Tok.is(tok::kw_delete)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1820 | if (D.isFunctionDeclarator()) |
| 1821 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1822 | << 1 /* delete */; |
| 1823 | else |
| 1824 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Sean Hunt | fe2695e | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1825 | } else if (Tok.is(tok::kw_default)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1826 | if (D.isFunctionDeclarator()) |
Sebastian Redl | ecfcd56 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1827 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1828 | << 0 /* default */; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1829 | else |
| 1830 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1831 | } else { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1832 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1833 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1834 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1835 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1836 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1837 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1838 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Peter Collingbourne | ec98f2f | 2012-07-27 12:56:09 +0000 | [diff] [blame] | 1839 | Actions.FinalizeDeclaration(ThisDecl); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1840 | cutOffParsing(); |
| 1841 | return 0; |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1842 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1843 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1844 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1845 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1846 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1847 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1848 | ExitScope(); |
| 1849 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1850 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1851 | if (Init.isInvalid()) { |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1852 | SkipUntil(tok::comma, true, true); |
| 1853 | Actions.ActOnInitializerError(ThisDecl); |
| 1854 | } else |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1855 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1856 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1857 | } |
| 1858 | } else if (Tok.is(tok::l_paren)) { |
| 1859 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1860 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1861 | T.consumeOpen(); |
| 1862 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1863 | ExprVector Exprs; |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1864 | CommaLocsTy CommaLocs; |
| 1865 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1866 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1867 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1868 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1871 | if (ParseExpressionList(Exprs, CommaLocs)) { |
David Blaikie | 3ea19c8 | 2012-10-10 23:15:05 +0000 | [diff] [blame] | 1872 | Actions.ActOnInitializerError(ThisDecl); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1873 | SkipUntil(tok::r_paren); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1874 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1875 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1876 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1877 | ExitScope(); |
| 1878 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1879 | } else { |
| 1880 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1881 | T.consumeClose(); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1882 | |
| 1883 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1884 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1885 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1886 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1887 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1888 | ExitScope(); |
| 1889 | } |
| 1890 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1891 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 1892 | T.getCloseLocation(), |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1893 | Exprs); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1894 | Actions.AddInitializerToDecl(ThisDecl, Initializer.take(), |
| 1895 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1896 | } |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1897 | } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) && |
Fariborz Jahanian | b0ed95c | 2012-07-03 23:22:13 +0000 | [diff] [blame] | 1898 | (!CurParsedObjCImpl || !D.isFunctionDeclarator())) { |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1899 | // Parse C++0x braced-init-list. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1900 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1901 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1902 | if (D.getCXXScopeSpec().isSet()) { |
| 1903 | EnterScope(0); |
| 1904 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1905 | } |
| 1906 | |
| 1907 | ExprResult Init(ParseBraceInitializer()); |
| 1908 | |
| 1909 | if (D.getCXXScopeSpec().isSet()) { |
| 1910 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1911 | ExitScope(); |
| 1912 | } |
| 1913 | |
| 1914 | if (Init.isInvalid()) { |
| 1915 | Actions.ActOnInitializerError(ThisDecl); |
| 1916 | } else |
| 1917 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1918 | /*DirectInit=*/true, TypeContainsAuto); |
| 1919 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1920 | } else { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1921 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1922 | } |
| 1923 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1924 | Actions.FinalizeDeclaration(ThisDecl); |
| 1925 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1926 | return ThisDecl; |
| 1927 | } |
| 1928 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1929 | /// ParseSpecifierQualifierList |
| 1930 | /// specifier-qualifier-list: |
| 1931 | /// type-specifier specifier-qualifier-list[opt] |
| 1932 | /// type-qualifier specifier-qualifier-list[opt] |
| 1933 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 1934 | /// |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1935 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 1936 | DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1937 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1938 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1939 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1940 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1941 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1942 | // Validate declspec for type-name. |
| 1943 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 1944 | if ((DSC == DSC_type_specifier || DSC == DSC_trailing) && |
| 1945 | !DS.hasTypeSpecifier()) { |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1946 | Diag(Tok, diag::err_expected_type); |
| 1947 | DS.SetTypeSpecError(); |
| 1948 | } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 1949 | !DS.hasAttributes()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1950 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1951 | if (!DS.hasTypeSpecifier()) |
| 1952 | DS.SetTypeSpecError(); |
| 1953 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1954 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1955 | // Issue diagnostic and remove storage class if present. |
| 1956 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 1957 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1958 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 1959 | else |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 1960 | Diag(DS.getThreadStorageClassSpecLoc(), |
| 1961 | diag::err_typename_invalid_storageclass); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1962 | DS.ClearStorageClassSpecs(); |
| 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 function specfier if present. |
| 1966 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1967 | if (DS.isInlineSpecified()) |
| 1968 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1969 | if (DS.isVirtualSpecified()) |
| 1970 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1971 | if (DS.isExplicitSpecified()) |
| 1972 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1973 | DS.ClearFunctionSpecs(); |
| 1974 | } |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1975 | |
| 1976 | // Issue diagnostic and remove constexpr specfier if present. |
| 1977 | if (DS.isConstexprSpecified()) { |
| 1978 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 1979 | DS.ClearConstexprSpec(); |
| 1980 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1983 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 1984 | /// specified token is valid after the identifier in a declarator which |
| 1985 | /// immediately follows the declspec. For example, these things are valid: |
| 1986 | /// |
| 1987 | /// int x [ 4]; // direct-declarator |
| 1988 | /// int x ( int y); // direct-declarator |
| 1989 | /// int(int x ) // direct-declarator |
| 1990 | /// int x ; // simple-declaration |
| 1991 | /// int x = 17; // init-declarator-list |
| 1992 | /// int x , y; // init-declarator-list |
| 1993 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1994 | /// int x : 4; // struct-declarator |
Chris Lattner | c83c27a | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 1995 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1996 | /// |
| 1997 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 1998 | /// ')' happens to be valid anyway). |
| 1999 | /// int (x) |
| 2000 | /// |
| 2001 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 2002 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 2003 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 2004 | 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] | 2005 | } |
| 2006 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2007 | |
| 2008 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 2009 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 2010 | /// the declspec has no type specifier. In this case, the declspec is either |
| 2011 | /// malformed or is "implicit int" (in K&R and C89). |
| 2012 | /// |
| 2013 | /// This method handles diagnosing this prettily and returns false if the |
| 2014 | /// declspec is done being processed. If it recovers and thinks there may be |
| 2015 | /// other pieces of declspec after it, it returns true. |
| 2016 | /// |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2017 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2018 | const ParsedTemplateInfo &TemplateInfo, |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2019 | AccessSpecifier AS, DeclSpecContext DSC, |
| 2020 | ParsedAttributesWithRange &Attrs) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2021 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2022 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2023 | SourceLocation Loc = Tok.getLocation(); |
| 2024 | // If we see an identifier that is not a type name, we normally would |
| 2025 | // parse it as the identifer being declared. However, when a typename |
| 2026 | // is typo'd or the definition is not included, this will incorrectly |
| 2027 | // parse the typename as the identifier name and fall over misparsing |
| 2028 | // later parts of the diagnostic. |
| 2029 | // |
| 2030 | // As such, we try to do some look-ahead in cases where this would |
| 2031 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 2032 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 2033 | // an identifier with implicit int, we'd get a parse error because the |
| 2034 | // next token is obviously invalid for a type. Parse these as a case |
| 2035 | // with an invalid type specifier. |
| 2036 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2037 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2038 | // 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] | 2039 | // error, do lookahead to try to do better recovery. This never applies |
| 2040 | // within a type specifier. Outside of C++, we allow this even if the |
| 2041 | // language doesn't "officially" support implicit int -- we support |
| 2042 | // implicit int as an extension in C99 and C11. Allegedly, MS also |
| 2043 | // supports implicit int in C++ mode. |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 2044 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2045 | (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) && |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2046 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2047 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 2048 | // we just avoid eating the identifier, so it will be parsed as the |
| 2049 | // identifier in the declarator. |
| 2050 | return false; |
| 2051 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2052 | |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2053 | if (getLangOpts().CPlusPlus && |
| 2054 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 2055 | // Don't require a type specifier if we have the 'auto' storage class |
| 2056 | // specifier in C++98 -- we'll promote it to a type specifier. |
| 2057 | return false; |
| 2058 | } |
| 2059 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2060 | // Otherwise, if we don't consume this token, we are going to emit an |
| 2061 | // error anyway. Try to recover from various common problems. Check |
| 2062 | // to see if this was a reference to a tag name without a tag specified. |
| 2063 | // 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] | 2064 | // |
| 2065 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 2066 | if (SS == 0) { |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2067 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2068 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2069 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2070 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2071 | default: break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2072 | case DeclSpec::TST_enum: |
| 2073 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 2074 | case DeclSpec::TST_union: |
| 2075 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 2076 | case DeclSpec::TST_struct: |
| 2077 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2078 | case DeclSpec::TST_interface: |
| 2079 | TagName="__interface"; FixitTagName = "__interface "; |
| 2080 | TagKind=tok::kw___interface;break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 2081 | case DeclSpec::TST_class: |
| 2082 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2083 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2084 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2085 | if (TagName) { |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2086 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 2087 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 2088 | Sema::LookupOrdinaryName); |
| 2089 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2090 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2091 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 2092 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 2093 | |
| 2094 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 2095 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 2096 | I != IEnd; ++I) |
Kaelyn Uhrain | 392b3f5 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 2097 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 2098 | << TokenName << TagName; |
| 2099 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2100 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2101 | // Parse this as a tag as if the missing tag were present. |
| 2102 | if (TagKind == tok::kw_enum) |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2103 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2104 | else |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2105 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2106 | /*EnteringContext*/ false, DSC_normal, Attrs); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2107 | return true; |
| 2108 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2109 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2110 | |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2111 | // Determine whether this identifier could plausibly be the name of something |
Richard Smith | 7514db2 | 2012-05-15 21:42:17 +0000 | [diff] [blame] | 2112 | // being declared (with a missing type). |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 2113 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
| 2114 | (!SS || DSC == DSC_top_level || DSC == DSC_class)) { |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 2115 | // Look ahead to the next token to try to figure out what this declaration |
| 2116 | // was supposed to be. |
| 2117 | switch (NextToken().getKind()) { |
| 2118 | case tok::comma: |
| 2119 | case tok::equal: |
| 2120 | case tok::kw_asm: |
| 2121 | case tok::l_brace: |
| 2122 | case tok::l_square: |
| 2123 | case tok::semi: |
| 2124 | // This looks like a variable declaration. The type is probably missing. |
| 2125 | // We're done parsing decl-specifiers. |
| 2126 | return false; |
| 2127 | |
| 2128 | case tok::l_paren: { |
| 2129 | // static x(4); // 'x' is not a type |
| 2130 | // x(int n); // 'x' is not a type |
| 2131 | // x (*p)[]; // 'x' is a type |
| 2132 | // |
| 2133 | // Since we're in an error case (or the rare 'implicit int in C++' MS |
| 2134 | // extension), we can afford to perform a tentative parse to determine |
| 2135 | // which case we're in. |
| 2136 | TentativeParsingAction PA(*this); |
| 2137 | ConsumeToken(); |
| 2138 | TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false); |
| 2139 | PA.Revert(); |
| 2140 | if (TPR == TPResult::False()) |
| 2141 | return false; |
| 2142 | // The identifier is followed by a parenthesized declarator. |
| 2143 | // It's supposed to be a type. |
| 2144 | break; |
| 2145 | } |
| 2146 | |
| 2147 | default: |
| 2148 | // This is probably supposed to be a type. This includes cases like: |
| 2149 | // int f(itn); |
| 2150 | // struct S { unsinged : 4; }; |
| 2151 | break; |
| 2152 | } |
| 2153 | } |
| 2154 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2155 | // 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] | 2156 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2157 | ParsedType T; |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2158 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2159 | if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2160 | // The action emitted a diagnostic, so we don't have to. |
| 2161 | if (T) { |
| 2162 | // The action has suggested that the type T could be used. Set that as |
| 2163 | // the type in the declaration specifiers, consume the would-be type |
| 2164 | // name token, and we're done. |
| 2165 | const char *PrevSpec; |
| 2166 | unsigned DiagID; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2167 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2168 | DS.SetRangeEnd(Tok.getLocation()); |
| 2169 | ConsumeToken(); |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2170 | // There may be other declaration specifiers after this. |
| 2171 | return true; |
| 2172 | } else if (II != Tok.getIdentifierInfo()) { |
| 2173 | // If no type was suggested, the correction is to a keyword |
| 2174 | Tok.setKind(II->getTokenID()); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2175 | // There may be other declaration specifiers after this. |
| 2176 | return true; |
| 2177 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2178 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2179 | // Fall through; the action had no suggestion for us. |
| 2180 | } else { |
| 2181 | // The action did not emit a diagnostic, so emit one now. |
| 2182 | SourceRange R; |
| 2183 | if (SS) R = SS->getRange(); |
| 2184 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 2185 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2186 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2187 | // Mark this as an error. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2188 | DS.SetTypeSpecError(); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2189 | DS.SetRangeEnd(Tok.getLocation()); |
| 2190 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2191 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2192 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 2193 | // avoid rippling error messages on subsequent uses of the same type, |
| 2194 | // could be useful if #include was forgotten. |
| 2195 | return false; |
| 2196 | } |
| 2197 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2198 | /// \brief Determine the declaration specifier context from the declarator |
| 2199 | /// context. |
| 2200 | /// |
| 2201 | /// \param Context the declarator context, which is one of the |
| 2202 | /// Declarator::TheContext enumerator values. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2203 | Parser::DeclSpecContext |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2204 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 2205 | if (Context == Declarator::MemberContext) |
| 2206 | return DSC_class; |
| 2207 | if (Context == Declarator::FileContext) |
| 2208 | return DSC_top_level; |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 2209 | if (Context == Declarator::TrailingReturnContext) |
| 2210 | return DSC_trailing; |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2211 | return DSC_normal; |
| 2212 | } |
| 2213 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2214 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 2215 | /// |
| 2216 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 2217 | /// type. Ideally, the type should be propagated directly into Sema. |
| 2218 | /// |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2219 | /// [C11] type-id |
| 2220 | /// [C11] constant-expression |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2221 | /// [C++0x] type-id ...[opt] |
| 2222 | /// [C++0x] assignment-expression ...[opt] |
| 2223 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 2224 | SourceLocation &EllipsisLoc) { |
| 2225 | ExprResult ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2226 | if (isTypeIdInParens()) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2227 | SourceLocation TypeLoc = Tok.getLocation(); |
| 2228 | ParsedType Ty = ParseTypeName().get(); |
| 2229 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2230 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 2231 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2232 | } else |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2233 | ER = ParseConstantExpression(); |
| 2234 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2235 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis)) |
Peter Collingbourne | fe9b2a8 | 2011-10-24 17:56:00 +0000 | [diff] [blame] | 2236 | EllipsisLoc = ConsumeToken(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2237 | |
| 2238 | return ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2239 | } |
| 2240 | |
| 2241 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 2242 | /// attribute to Attrs. |
| 2243 | /// |
| 2244 | /// alignment-specifier: |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2245 | /// [C11] '_Alignas' '(' type-id ')' |
| 2246 | /// [C11] '_Alignas' '(' constant-expression ')' |
Richard Smith | 33f04a2 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2247 | /// [C++11] 'alignas' '(' type-id ...[opt] ')' |
| 2248 | /// [C++11] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2249 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2250 | SourceLocation *EndLoc) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2251 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 2252 | "Not an alignment-specifier!"); |
| 2253 | |
Richard Smith | 33f04a2 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2254 | IdentifierInfo *KWName = Tok.getIdentifierInfo(); |
| 2255 | SourceLocation KWLoc = ConsumeToken(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2256 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2257 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2258 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2259 | return; |
| 2260 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2261 | SourceLocation EllipsisLoc; |
| 2262 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2263 | if (ArgExpr.isInvalid()) { |
| 2264 | SkipUntil(tok::r_paren); |
| 2265 | return; |
| 2266 | } |
| 2267 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2268 | T.consumeClose(); |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2269 | if (EndLoc) |
| 2270 | *EndLoc = T.getCloseLocation(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2271 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2272 | ExprVector ArgExprs; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2273 | ArgExprs.push_back(ArgExpr.release()); |
Richard Smith | 33f04a2 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2274 | Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(), |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2275 | ArgExprs.data(), 1, AttributeList::AS_Keyword, EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2278 | /// ParseDeclarationSpecifiers |
| 2279 | /// declaration-specifiers: [C99 6.7] |
| 2280 | /// storage-class-specifier declaration-specifiers[opt] |
| 2281 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2282 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2283 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2284 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2285 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2286 | /// |
| 2287 | /// storage-class-specifier: [C99 6.7.1] |
| 2288 | /// 'typedef' |
| 2289 | /// 'extern' |
| 2290 | /// 'static' |
| 2291 | /// 'auto' |
| 2292 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2293 | /// [C++] 'mutable' |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2294 | /// [C++11] 'thread_local' |
| 2295 | /// [C11] '_Thread_local' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2296 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2297 | /// function-specifier: [C99 6.7.4] |
| 2298 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2299 | /// [C++] 'virtual' |
| 2300 | /// [C++] 'explicit' |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2301 | /// [OpenCL] '__kernel' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2302 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2303 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2304 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2305 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 2306 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2307 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2308 | AccessSpecifier AS, |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2309 | DeclSpecContext DSContext, |
| 2310 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2311 | if (DS.getSourceRange().isInvalid()) { |
| 2312 | DS.SetRangeStart(Tok.getLocation()); |
| 2313 | DS.SetRangeEnd(Tok.getLocation()); |
| 2314 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2315 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2316 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2317 | bool AttrsLastTime = false; |
| 2318 | ParsedAttributesWithRange attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2319 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2320 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2321 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2322 | unsigned DiagID = 0; |
| 2323 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2324 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2325 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2326 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2327 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2328 | DoneWithDeclSpec: |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2329 | if (!AttrsLastTime) |
| 2330 | ProhibitAttributes(attrs); |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2331 | else { |
| 2332 | // Reject C++11 attributes that appertain to decl specifiers as |
| 2333 | // we don't support any C++11 attributes that appertain to decl |
| 2334 | // specifiers. This also conforms to what g++ 4.8 is doing. |
| 2335 | ProhibitCXX11Attributes(attrs); |
| 2336 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2337 | DS.takeAttributesFrom(attrs); |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2338 | } |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 2339 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2340 | // If this is not a declaration specifier token, we're done reading decl |
| 2341 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2342 | DS.Finish(Diags, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2343 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2344 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2345 | case tok::l_square: |
| 2346 | case tok::kw_alignas: |
Richard Smith | 672edb0 | 2013-02-22 09:15:49 +0000 | [diff] [blame] | 2347 | if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier()) |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2348 | goto DoneWithDeclSpec; |
| 2349 | |
| 2350 | ProhibitAttributes(attrs); |
| 2351 | // FIXME: It would be good to recover by accepting the attributes, |
| 2352 | // but attempting to do that now would cause serious |
| 2353 | // madness in terms of diagnostics. |
| 2354 | attrs.clear(); |
| 2355 | attrs.Range = SourceRange(); |
| 2356 | |
| 2357 | ParseCXX11Attributes(attrs); |
| 2358 | AttrsLastTime = true; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2359 | continue; |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2360 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2361 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2362 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2363 | if (DS.hasTypeSpecifier()) { |
| 2364 | bool AllowNonIdentifiers |
| 2365 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 2366 | Scope::BlockScope | |
| 2367 | Scope::TemplateParamScope | |
| 2368 | Scope::FunctionPrototypeScope | |
| 2369 | Scope::AtCatchScope)) == 0; |
| 2370 | bool AllowNestedNameSpecifiers |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2371 | = DSContext == DSC_top_level || |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2372 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 2373 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2374 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2375 | AllowNonIdentifiers, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2376 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2377 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2378 | } |
| 2379 | |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2380 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 2381 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 2382 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2383 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2384 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2385 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2386 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 2387 | else if (CurParsedObjCImpl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2388 | CCC = Sema::PCC_ObjCImplementation; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2389 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2390 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2391 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2392 | } |
| 2393 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2394 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2395 | // C++ scope specifier. Annotate and loop, or bail out on error. |
| 2396 | if (TryAnnotateCXXScopeToken(true)) { |
| 2397 | if (!DS.hasTypeSpecifier()) |
| 2398 | DS.SetTypeSpecError(); |
| 2399 | goto DoneWithDeclSpec; |
| 2400 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 2401 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 2402 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2403 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2404 | |
| 2405 | case tok::annot_cxxscope: { |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2406 | if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector()) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2407 | goto DoneWithDeclSpec; |
| 2408 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2409 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 2410 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 2411 | Tok.getAnnotationRange(), |
| 2412 | SS); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2413 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2414 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2415 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2416 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2417 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2418 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2419 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2420 | |
| 2421 | // C++ [class.qual]p2: |
| 2422 | // In a lookup in which the constructor is an acceptable lookup |
| 2423 | // result and the nested-name-specifier nominates a class C: |
| 2424 | // |
| 2425 | // - if the name specified after the |
| 2426 | // nested-name-specifier, when looked up in C, is the |
| 2427 | // injected-class-name of C (Clause 9), or |
| 2428 | // |
| 2429 | // - if the name specified after the nested-name-specifier |
| 2430 | // is the same as the identifier or the |
| 2431 | // simple-template-id's template-name in the last |
| 2432 | // component of the nested-name-specifier, |
| 2433 | // |
| 2434 | // the name is instead considered to name the constructor of |
| 2435 | // class C. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2436 | // |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2437 | // Thus, if the template-name is actually the constructor |
| 2438 | // name, then the code is ill-formed; this interpretation is |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2439 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2440 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2441 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2442 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2443 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2444 | if (isConstructorDeclarator()) { |
| 2445 | // The user meant this to be an out-of-line constructor |
| 2446 | // definition, but template arguments are not allowed |
| 2447 | // there. Just allow this as a constructor; we'll |
| 2448 | // complain about it later. |
| 2449 | goto DoneWithDeclSpec; |
| 2450 | } |
| 2451 | |
| 2452 | // The user meant this to name a type, but it actually names |
| 2453 | // a constructor with some extraneous template |
| 2454 | // arguments. Complain, then parse it as a type as the user |
| 2455 | // intended. |
| 2456 | Diag(TemplateId->TemplateNameLoc, |
| 2457 | diag::err_out_of_line_template_id_names_constructor) |
| 2458 | << TemplateId->Name; |
| 2459 | } |
| 2460 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2461 | DS.getTypeSpecScope() = SS; |
| 2462 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2463 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2464 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2465 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2466 | continue; |
| 2467 | } |
| 2468 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2469 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2470 | DS.getTypeSpecScope() = SS; |
| 2471 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2472 | if (Tok.getAnnotationValue()) { |
| 2473 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2474 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2475 | Tok.getAnnotationEndLoc(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2476 | PrevSpec, DiagID, T); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2477 | if (isInvalid) |
| 2478 | break; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2479 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2480 | else |
| 2481 | DS.SetTypeSpecError(); |
| 2482 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2483 | ConsumeToken(); // The typename |
| 2484 | } |
| 2485 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2486 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2487 | goto DoneWithDeclSpec; |
| 2488 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2489 | // If we're in a context where the identifier could be a class name, |
| 2490 | // check whether this is a constructor declaration. |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2491 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2492 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2493 | &SS)) { |
| 2494 | if (isConstructorDeclarator()) |
| 2495 | goto DoneWithDeclSpec; |
| 2496 | |
| 2497 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 2498 | // of the class is qualified in a context where it could name |
| 2499 | // a constructor, its a constructor name. However, we've |
| 2500 | // looked at the declarator, and the user probably meant this |
| 2501 | // to be a type. Complain that it isn't supposed to be treated |
| 2502 | // as a type, then proceed to parse it as a type. |
| 2503 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 2504 | << Next.getIdentifierInfo(); |
| 2505 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2506 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2507 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 2508 | Next.getLocation(), |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2509 | getCurScope(), &SS, |
| 2510 | false, false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2511 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2512 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2513 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2514 | // If the referenced identifier is not a type, then this declspec is |
| 2515 | // erroneous: We already checked about that it has no type specifier, and |
| 2516 | // 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] | 2517 | // typename. |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2518 | if (TypeRep == 0) { |
| 2519 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2520 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2521 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) { |
| 2522 | if (!Attrs.empty()) { |
| 2523 | AttrsLastTime = true; |
| 2524 | attrs.takeAllFrom(Attrs); |
| 2525 | } |
| 2526 | continue; |
| 2527 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2528 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2529 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2530 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2531 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2532 | ConsumeToken(); // The C++ scope. |
| 2533 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2534 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2535 | DiagID, TypeRep); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2536 | if (isInvalid) |
| 2537 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2538 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2539 | DS.SetRangeEnd(Tok.getLocation()); |
| 2540 | ConsumeToken(); // The typename. |
| 2541 | |
| 2542 | continue; |
| 2543 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2544 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2545 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2546 | if (Tok.getAnnotationValue()) { |
| 2547 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | c43271e | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 2548 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2549 | DiagID, T); |
| 2550 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2551 | DS.SetTypeSpecError(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2552 | |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 2553 | if (isInvalid) |
| 2554 | break; |
| 2555 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2556 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2557 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2558 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2559 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2560 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2561 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2562 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2563 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2564 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2565 | continue; |
| 2566 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2567 | |
Douglas Gregor | bfad915 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2568 | case tok::kw___is_signed: |
| 2569 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 2570 | // typically treats it as a trait. If we see __is_signed as it appears |
| 2571 | // in libstdc++, e.g., |
| 2572 | // |
| 2573 | // static const bool __is_signed; |
| 2574 | // |
| 2575 | // then treat __is_signed as an identifier rather than as a keyword. |
| 2576 | if (DS.getTypeSpecType() == TST_bool && |
| 2577 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
| 2578 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 2579 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
| 2580 | Tok.setKind(tok::identifier); |
| 2581 | } |
| 2582 | |
| 2583 | // We're done with the declaration-specifiers. |
| 2584 | goto DoneWithDeclSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2585 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2586 | // typedef-name |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2587 | case tok::kw_decltype: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2588 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2589 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 2590 | // so handle it as such. This is important for ctor parsing. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2591 | if (getLangOpts().CPlusPlus) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2592 | if (TryAnnotateCXXScopeToken(true)) { |
| 2593 | if (!DS.hasTypeSpecifier()) |
| 2594 | DS.SetTypeSpecError(); |
| 2595 | goto DoneWithDeclSpec; |
| 2596 | } |
| 2597 | if (!Tok.is(tok::identifier)) |
| 2598 | continue; |
| 2599 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2600 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2601 | // This identifier can only be a typedef name if we haven't already seen |
| 2602 | // a type-specifier. Without this check we misparse: |
| 2603 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 2604 | if (DS.hasTypeSpecifier()) |
| 2605 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2606 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2607 | // Check for need to substitute AltiVec keyword tokens. |
| 2608 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2609 | break; |
| 2610 | |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2611 | // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not |
| 2612 | // allow the use of a typedef name as a type specifier. |
| 2613 | if (DS.isTypeAltiVecVector()) |
| 2614 | goto DoneWithDeclSpec; |
| 2615 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2616 | ParsedType TypeRep = |
| 2617 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 2618 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2619 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2620 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 2621 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2622 | if (!TypeRep) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2623 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2624 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) { |
| 2625 | if (!Attrs.empty()) { |
| 2626 | AttrsLastTime = true; |
| 2627 | attrs.takeAllFrom(Attrs); |
| 2628 | } |
| 2629 | continue; |
| 2630 | } |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2631 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2632 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2633 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2634 | // If we're in a context where the identifier could be a class name, |
| 2635 | // check whether this is a constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2636 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2637 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2638 | isConstructorDeclarator()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2639 | goto DoneWithDeclSpec; |
| 2640 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2641 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2642 | DiagID, TypeRep); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2643 | if (isInvalid) |
| 2644 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2645 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2646 | DS.SetRangeEnd(Tok.getLocation()); |
| 2647 | ConsumeToken(); // The identifier |
| 2648 | |
| 2649 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2650 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2651 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2652 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2653 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2654 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2655 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2656 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2657 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2658 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2659 | |
| 2660 | // type-name |
| 2661 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2662 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2663 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2664 | // This template-id does not refer to a type name, so we're |
| 2665 | // done with the type-specifiers. |
| 2666 | goto DoneWithDeclSpec; |
| 2667 | } |
| 2668 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2669 | // If we're in a context where the template-id could be a |
| 2670 | // constructor name or specialization, check whether this is a |
| 2671 | // constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2672 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2673 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2674 | isConstructorDeclarator()) |
| 2675 | goto DoneWithDeclSpec; |
| 2676 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2677 | // Turn the template-id annotation token into a type annotation |
| 2678 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2679 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2680 | continue; |
| 2681 | } |
| 2682 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2683 | // GNU attributes support. |
| 2684 | case tok::kw___attribute: |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2685 | ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2686 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2687 | |
| 2688 | // Microsoft declspec support. |
| 2689 | case tok::kw___declspec: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2690 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2691 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2692 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2693 | // Microsoft single token adornments. |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2694 | case tok::kw___forceinline: { |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2695 | isInvalid = DS.setFunctionSpecInline(Loc); |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2696 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2697 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 2698 | // FIXME: This does not work correctly if it is set to be a declspec |
| 2699 | // attribute, and a GNU attribute is simply incorrect. |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2700 | DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 2701 | SourceLocation(), 0, 0, AttributeList::AS_GNU); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2702 | break; |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2703 | } |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2704 | |
| 2705 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2706 | case tok::kw___ptr32: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2707 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2708 | case tok::kw___cdecl: |
| 2709 | case tok::kw___stdcall: |
| 2710 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2711 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2712 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2713 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2714 | continue; |
| 2715 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2716 | // Borland single token adornments. |
| 2717 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2718 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2719 | continue; |
| 2720 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2721 | // OpenCL single token adornments. |
| 2722 | case tok::kw___kernel: |
| 2723 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2724 | continue; |
| 2725 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2726 | // storage-class-specifier |
| 2727 | case tok::kw_typedef: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2728 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
| 2729 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2730 | break; |
| 2731 | case tok::kw_extern: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2732 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2733 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2734 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
| 2735 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2736 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2737 | case tok::kw___private_extern__: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2738 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
| 2739 | Loc, PrevSpec, DiagID); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2740 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2741 | case tok::kw_static: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2742 | if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2743 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2744 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
| 2745 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2746 | break; |
| 2747 | case tok::kw_auto: |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2748 | if (getLangOpts().CPlusPlus11) { |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2749 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2750 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2751 | PrevSpec, DiagID); |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2752 | if (!isInvalid) |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2753 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2754 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2755 | } else |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2756 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 2757 | DiagID); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2758 | } else |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2759 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2760 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2761 | break; |
| 2762 | case tok::kw_register: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2763 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
| 2764 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2765 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2766 | case tok::kw_mutable: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2767 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
| 2768 | PrevSpec, DiagID); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2769 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2770 | case tok::kw___thread: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 2771 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc, |
| 2772 | PrevSpec, DiagID); |
| 2773 | break; |
| 2774 | case tok::kw_thread_local: |
| 2775 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc, |
| 2776 | PrevSpec, DiagID); |
| 2777 | break; |
| 2778 | case tok::kw__Thread_local: |
| 2779 | isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local, |
| 2780 | Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2781 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2782 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2783 | // function-specifier |
| 2784 | case tok::kw_inline: |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2785 | isInvalid = DS.setFunctionSpecInline(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2786 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2787 | case tok::kw_virtual: |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2788 | isInvalid = DS.setFunctionSpecVirtual(Loc); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2789 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2790 | case tok::kw_explicit: |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2791 | isInvalid = DS.setFunctionSpecExplicit(Loc); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2792 | break; |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 2793 | case tok::kw__Noreturn: |
| 2794 | if (!getLangOpts().C11) |
| 2795 | Diag(Loc, diag::ext_c11_noreturn); |
| 2796 | isInvalid = DS.setFunctionSpecNoreturn(Loc); |
| 2797 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2798 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2799 | // alignment-specifier |
| 2800 | case tok::kw__Alignas: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2801 | if (!getLangOpts().C11) |
Jordan Rose | f70a886 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2802 | Diag(Tok, diag::ext_c11_alignment) << Tok.getName(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2803 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2804 | continue; |
| 2805 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2806 | // friend |
| 2807 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2808 | if (DSContext == DSC_class) |
| 2809 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 2810 | else { |
| 2811 | PrevSpec = ""; // not actually used by the diagnostic |
| 2812 | DiagID = diag::err_friend_invalid_in_context; |
| 2813 | isInvalid = true; |
| 2814 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2815 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2816 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2817 | // Modules |
| 2818 | case tok::kw___module_private__: |
| 2819 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 2820 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2821 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2822 | // constexpr |
| 2823 | case tok::kw_constexpr: |
| 2824 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 2825 | break; |
| 2826 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2827 | // type-specifier |
| 2828 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2829 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 2830 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2831 | break; |
| 2832 | case tok::kw_long: |
| 2833 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2834 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2835 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2836 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2837 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2838 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2839 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2840 | case tok::kw___int64: |
| 2841 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2842 | DiagID); |
| 2843 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2844 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2845 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 2846 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2847 | break; |
| 2848 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2849 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2850 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2851 | break; |
| 2852 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2853 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2854 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2855 | break; |
| 2856 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2857 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2858 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2859 | break; |
| 2860 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2861 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 2862 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2863 | break; |
| 2864 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2865 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 2866 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2867 | break; |
| 2868 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2869 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 2870 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2871 | break; |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 2872 | case tok::kw___int128: |
| 2873 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
| 2874 | DiagID); |
| 2875 | break; |
| 2876 | case tok::kw_half: |
| 2877 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
| 2878 | DiagID); |
| 2879 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2880 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2881 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 2882 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2883 | break; |
| 2884 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2885 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 2886 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2887 | break; |
| 2888 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2889 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 2890 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2891 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2892 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2893 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 2894 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2895 | break; |
| 2896 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2897 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 2898 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2899 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2900 | case tok::kw_bool: |
| 2901 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2902 | if (Tok.is(tok::kw_bool) && |
| 2903 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 2904 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 2905 | PrevSpec = ""; // Not used by the diagnostic. |
| 2906 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2907 | // For better error recovery. |
| 2908 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2909 | isInvalid = true; |
| 2910 | } else { |
| 2911 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 2912 | DiagID); |
| 2913 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2914 | break; |
| 2915 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2916 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2917 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2918 | break; |
| 2919 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2920 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2921 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2922 | break; |
| 2923 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2924 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2925 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2926 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2927 | case tok::kw___vector: |
| 2928 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2929 | break; |
| 2930 | case tok::kw___pixel: |
| 2931 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2932 | break; |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 2933 | case tok::kw_image1d_t: |
| 2934 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc, |
| 2935 | PrevSpec, DiagID); |
| 2936 | break; |
| 2937 | case tok::kw_image1d_array_t: |
| 2938 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc, |
| 2939 | PrevSpec, DiagID); |
| 2940 | break; |
| 2941 | case tok::kw_image1d_buffer_t: |
| 2942 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc, |
| 2943 | PrevSpec, DiagID); |
| 2944 | break; |
| 2945 | case tok::kw_image2d_t: |
| 2946 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc, |
| 2947 | PrevSpec, DiagID); |
| 2948 | break; |
| 2949 | case tok::kw_image2d_array_t: |
| 2950 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc, |
| 2951 | PrevSpec, DiagID); |
| 2952 | break; |
| 2953 | case tok::kw_image3d_t: |
| 2954 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc, |
| 2955 | PrevSpec, DiagID); |
| 2956 | break; |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2957 | case tok::kw_sampler_t: |
| 2958 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc, |
| 2959 | PrevSpec, DiagID); |
| 2960 | break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2961 | case tok::kw_event_t: |
| 2962 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc, |
| 2963 | PrevSpec, DiagID); |
| 2964 | break; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 2965 | case tok::kw___unknown_anytype: |
| 2966 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 2967 | PrevSpec, DiagID); |
| 2968 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2969 | |
| 2970 | // class-specifier: |
| 2971 | case tok::kw_class: |
| 2972 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2973 | case tok::kw___interface: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2974 | case tok::kw_union: { |
| 2975 | tok::TokenKind Kind = Tok.getKind(); |
| 2976 | ConsumeToken(); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2977 | |
| 2978 | // These are attributes following class specifiers. |
| 2979 | // To produce better diagnostic, we parse them when |
| 2980 | // parsing class specifier. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2981 | ParsedAttributesWithRange Attributes(AttrFactory); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2982 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2983 | EnteringContext, DSContext, Attributes); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2984 | |
| 2985 | // If there are attributes following class specifier, |
| 2986 | // take them over and handle them here. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2987 | if (!Attributes.empty()) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2988 | AttrsLastTime = true; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2989 | attrs.takeAllFrom(Attributes); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2990 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2991 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2992 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2993 | |
| 2994 | // enum-specifier: |
| 2995 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2996 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2997 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2998 | continue; |
| 2999 | |
| 3000 | // cv-qualifier: |
| 3001 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3002 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3003 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3004 | break; |
| 3005 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3006 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3007 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3008 | break; |
| 3009 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3010 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 3011 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3012 | break; |
| 3013 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3014 | // C++ typename-specifier: |
| 3015 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3016 | if (TryAnnotateTypeOrScopeToken()) { |
| 3017 | DS.SetTypeSpecError(); |
| 3018 | goto DoneWithDeclSpec; |
| 3019 | } |
| 3020 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3021 | continue; |
| 3022 | break; |
| 3023 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 3024 | // GNU typeof support. |
| 3025 | case tok::kw_typeof: |
| 3026 | ParseTypeofSpecifier(DS); |
| 3027 | continue; |
| 3028 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3029 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 3030 | ParseDecltypeSpecifier(DS); |
| 3031 | continue; |
| 3032 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3033 | case tok::kw___underlying_type: |
| 3034 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3035 | continue; |
| 3036 | |
| 3037 | case tok::kw__Atomic: |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3038 | // C11 6.7.2.4/4: |
| 3039 | // If the _Atomic keyword is immediately followed by a left parenthesis, |
| 3040 | // it is interpreted as a type specifier (with a type name), not as a |
| 3041 | // type qualifier. |
| 3042 | if (NextToken().is(tok::l_paren)) { |
| 3043 | ParseAtomicSpecifier(DS); |
| 3044 | continue; |
| 3045 | } |
| 3046 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 3047 | getLangOpts()); |
| 3048 | break; |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 3049 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3050 | // OpenCL qualifiers: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3051 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3052 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3053 | goto DoneWithDeclSpec; |
| 3054 | case tok::kw___private: |
| 3055 | case tok::kw___global: |
| 3056 | case tok::kw___local: |
| 3057 | case tok::kw___constant: |
| 3058 | case tok::kw___read_only: |
| 3059 | case tok::kw___write_only: |
| 3060 | case tok::kw___read_write: |
| 3061 | ParseOpenCLQualifiers(DS); |
| 3062 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3063 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 3064 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 3065 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3066 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 3067 | // but we support it. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3068 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 3069 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3070 | |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 3071 | if (!ParseObjCProtocolQualifiers(DS)) |
| 3072 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 3073 | << FixItHint::CreateInsertion(Loc, "id") |
| 3074 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3075 | |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 3076 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 3077 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 3078 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3079 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3080 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3081 | if (isInvalid) { |
| 3082 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3083 | assert(DiagID); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3084 | |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 3085 | if (DiagID == diag::ext_duplicate_declspec) |
| 3086 | Diag(Tok, DiagID) |
| 3087 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 3088 | else |
| 3089 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3090 | } |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 3091 | |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 3092 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 3093 | if (DiagID != diag::err_bool_redeclaration) |
| 3094 | ConsumeToken(); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3095 | |
| 3096 | AttrsLastTime = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3097 | } |
| 3098 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 3099 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3100 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 3101 | /// semicolon. |
| 3102 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3103 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3104 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3105 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3106 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3107 | /// struct-declarator-list: |
| 3108 | /// struct-declarator |
| 3109 | /// struct-declarator-list ',' struct-declarator |
| 3110 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 3111 | /// struct-declarator: |
| 3112 | /// declarator |
| 3113 | /// [GNU] declarator attributes[opt] |
| 3114 | /// declarator[opt] ':' constant-expression |
| 3115 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 3116 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3117 | void Parser:: |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3118 | ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3119 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3120 | if (Tok.is(tok::kw___extension__)) { |
| 3121 | // __extension__ silences extension warnings in the subexpression. |
| 3122 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3123 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 3124 | return ParseStructDeclaration(DS, Fields); |
| 3125 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3126 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3127 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3128 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3129 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 3130 | // If there are no declarators, this is a free-standing declaration |
| 3131 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3132 | if (Tok.is(tok::semi)) { |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3133 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
| 3134 | DS); |
| 3135 | DS.complete(TheDecl); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3136 | return; |
| 3137 | } |
| 3138 | |
| 3139 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3140 | bool FirstDeclarator = true; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3141 | SourceLocation CommaLoc; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3142 | while (1) { |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3143 | ParsingFieldDeclarator DeclaratorInfo(*this, DS); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3144 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3145 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3146 | // Attributes are only allowed here on successive declarators. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3147 | if (!FirstDeclarator) |
| 3148 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3149 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3150 | /// struct-declarator: declarator |
| 3151 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3152 | if (Tok.isNot(tok::colon)) { |
| 3153 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 3154 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3155 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3156 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3157 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3158 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3159 | ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3160 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3161 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3162 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 3163 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 3164 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3165 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3166 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3167 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3168 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3169 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3170 | // We're done with this declarator; invoke the callback. |
Eli Friedman | 817a886 | 2012-08-08 23:35:12 +0000 | [diff] [blame] | 3171 | Fields.invoke(DeclaratorInfo); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3172 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3173 | // If we don't have a comma, it is either the end of the list (a ';') |
| 3174 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3175 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3176 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3177 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3178 | // Consume the comma. |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3179 | CommaLoc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3180 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3181 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3182 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3183 | } |
| 3184 | |
| 3185 | /// ParseStructUnionBody |
| 3186 | /// struct-contents: |
| 3187 | /// struct-declaration-list |
| 3188 | /// [EXT] empty |
| 3189 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 3190 | /// struct-declaration-list: |
| 3191 | /// struct-declaration |
| 3192 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3193 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3194 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3195 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3196 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3197 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 3198 | "parsing struct/union body"); |
Andy Gibbs | f50f3f7 | 2013-04-03 09:31:19 +0000 | [diff] [blame] | 3199 | assert(!getLangOpts().CPlusPlus && "C++ declarations not supported"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3200 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3201 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3202 | if (T.consumeOpen()) |
| 3203 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3204 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3205 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3206 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3207 | |
Andy Gibbs | f50f3f7 | 2013-04-03 09:31:19 +0000 | [diff] [blame] | 3208 | // Empty structs are an extension in C (C99 6.7.2.1p7). |
| 3209 | if (Tok.is(tok::r_brace)) { |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 3210 | Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union); |
| 3211 | Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union); |
| 3212 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3213 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3214 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3215 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3216 | // 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] | 3217 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3218 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3219 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3220 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3221 | if (Tok.is(tok::semi)) { |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3222 | ConsumeExtraSemi(InsideStruct, TagType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3223 | continue; |
| 3224 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3225 | |
Andy Gibbs | 74b9fa1 | 2013-04-03 09:46:04 +0000 | [diff] [blame] | 3226 | // Parse _Static_assert declaration. |
| 3227 | if (Tok.is(tok::kw__Static_assert)) { |
| 3228 | SourceLocation DeclEnd; |
| 3229 | ParseStaticAssertDeclaration(DeclEnd); |
| 3230 | continue; |
| 3231 | } |
| 3232 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3233 | if (!Tok.is(tok::at)) { |
| 3234 | struct CFieldCallback : FieldCallback { |
| 3235 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3236 | Decl *TagDecl; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3237 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3238 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3239 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3240 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3241 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 3242 | |
Eli Friedman | dcdff46 | 2012-08-08 23:53:27 +0000 | [diff] [blame] | 3243 | void invoke(ParsingFieldDeclarator &FD) { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3244 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3245 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 3246 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 3247 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3248 | FieldDecls.push_back(Field); |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3249 | FD.complete(Field); |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3250 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3251 | } Callback(*this, TagDecl, FieldDecls); |
| 3252 | |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3253 | // Parse all the comma separated declarators. |
| 3254 | ParsingDeclSpec DS(*this); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3255 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3256 | } else { // Handle @defs |
| 3257 | ConsumeToken(); |
| 3258 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 3259 | Diag(Tok, diag::err_unexpected_at); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3260 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3261 | continue; |
| 3262 | } |
| 3263 | ConsumeToken(); |
| 3264 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 3265 | if (!Tok.is(tok::identifier)) { |
| 3266 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3267 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3268 | continue; |
| 3269 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3270 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3271 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3272 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3273 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 3274 | ConsumeToken(); |
| 3275 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3276 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3277 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3278 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3279 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3280 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3281 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3282 | break; |
| 3283 | } else { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3284 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 3285 | // 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] | 3286 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3287 | // If we stopped at a ';', eat it. |
| 3288 | if (Tok.is(tok::semi)) ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3289 | } |
| 3290 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3291 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3292 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3293 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3294 | ParsedAttributes attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3295 | // If attributes exist after struct contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3296 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 3297 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3298 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 3299 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3300 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3301 | attrs.getList()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3302 | StructScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3303 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 3304 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3305 | } |
| 3306 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3307 | /// ParseEnumSpecifier |
| 3308 | /// enum-specifier: [C99 6.7.2.2] |
| 3309 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3310 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3311 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3312 | /// '}' attributes[opt] |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 3313 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3314 | /// '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3315 | /// 'enum' identifier |
| 3316 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3317 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3318 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 3319 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3320 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3321 | /// enum-head: [C++11] |
| 3322 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 3323 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 3324 | /// identifier enum-base[opt] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3325 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3326 | /// enum-key: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3327 | /// 'enum' |
| 3328 | /// 'enum' 'class' |
| 3329 | /// 'enum' 'struct' |
| 3330 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3331 | /// enum-base: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3332 | /// ':' type-specifier-seq |
| 3333 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3334 | /// [C++] elaborated-type-specifier: |
| 3335 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 3336 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3337 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 3338 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3339 | AccessSpecifier AS, DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3340 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3341 | if (Tok.is(tok::code_completion)) { |
| 3342 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3343 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3344 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3345 | } |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3346 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3347 | // If attributes exist after tag, parse them. |
| 3348 | ParsedAttributesWithRange attrs(AttrFactory); |
| 3349 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3350 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3351 | |
| 3352 | // If declspecs exist after tag, parse them. |
| 3353 | while (Tok.is(tok::kw___declspec)) |
| 3354 | ParseMicrosoftDeclSpec(attrs); |
| 3355 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3356 | SourceLocation ScopedEnumKWLoc; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3357 | bool IsScopedUsingClassTag = false; |
| 3358 | |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3359 | // In C++11, recognize 'enum class' and 'enum struct'. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3360 | if (getLangOpts().CPlusPlus11 && |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3361 | (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3362 | Diag(Tok, diag::warn_cxx98_compat_scoped_enum); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3363 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3364 | ScopedEnumKWLoc = ConsumeToken(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3365 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3366 | // Attributes are not allowed between these keywords. Diagnose, |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3367 | // but then just treat them like they appeared in the right place. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3368 | ProhibitAttributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3369 | |
| 3370 | // They are allowed afterwards, though. |
| 3371 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3372 | MaybeParseCXX11Attributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3373 | while (Tok.is(tok::kw___declspec)) |
| 3374 | ParseMicrosoftDeclSpec(attrs); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3375 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3376 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3377 | // C++11 [temp.explicit]p12: |
| 3378 | // The usual access controls do not apply to names used to specify |
| 3379 | // explicit instantiations. |
| 3380 | // We extend this to also cover explicit specializations. Note that |
| 3381 | // we don't suppress if this turns out to be an elaborated type |
| 3382 | // specifier. |
| 3383 | bool shouldDelayDiagsInTag = |
| 3384 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 3385 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 3386 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3387 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3388 | // Enum definitions should not be parsed in a trailing-return-type. |
| 3389 | bool AllowDeclaration = DSC != DSC_trailing; |
| 3390 | |
| 3391 | bool AllowFixedUnderlyingType = AllowDeclaration && |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3392 | (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt || |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3393 | getLangOpts().ObjC2); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3394 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3395 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3396 | if (getLangOpts().CPlusPlus) { |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3397 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 3398 | // if a fixed underlying type is allowed. |
| 3399 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3400 | |
| 3401 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Richard Smith | 725fe0e | 2013-04-01 21:43:41 +0000 | [diff] [blame] | 3402 | /*EnteringContext=*/true)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3403 | return; |
| 3404 | |
| 3405 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3406 | Diag(Tok, diag::err_expected_ident); |
| 3407 | if (Tok.isNot(tok::l_brace)) { |
| 3408 | // Has no name and is not a definition. |
| 3409 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3410 | SkipUntil(tok::comma, true); |
| 3411 | return; |
| 3412 | } |
| 3413 | } |
| 3414 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3415 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3416 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3417 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3418 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3419 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3420 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3421 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3422 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3423 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3424 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3425 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3426 | // If an identifier is present, consume and remember it. |
| 3427 | IdentifierInfo *Name = 0; |
| 3428 | SourceLocation NameLoc; |
| 3429 | if (Tok.is(tok::identifier)) { |
| 3430 | Name = Tok.getIdentifierInfo(); |
| 3431 | NameLoc = ConsumeToken(); |
| 3432 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3433 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3434 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3435 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 3436 | // declaration of a scoped enumeration. |
| 3437 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3438 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3439 | IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3440 | } |
| 3441 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3442 | // Okay, end the suppression area. We'll decide whether to emit the |
| 3443 | // diagnostics in a second. |
| 3444 | if (shouldDelayDiagsInTag) |
| 3445 | diagsFromTag.done(); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3446 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3447 | TypeResult BaseType; |
| 3448 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3449 | // Parse the fixed underlying type. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3450 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3451 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3452 | bool PossibleBitfield = false; |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3453 | if (CanBeBitfield) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3454 | // If we're in class scope, this can either be an enum declaration with |
| 3455 | // an underlying type, or a declaration of a bitfield member. We try to |
| 3456 | // use a simple disambiguation scheme first to catch the common cases |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3457 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 3458 | // anything that's a simple-type-specifier followed by '(' as an |
| 3459 | // expression. This suffices because function types are not valid |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3460 | // underlying types anyway. |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 3461 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 3462 | Sema::ConstantEvaluated); |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3463 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3464 | // 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] | 3465 | // bit-field. This is the common case. |
| 3466 | if (TPR == TPResult::True()) |
| 3467 | PossibleBitfield = true; |
| 3468 | // If the next token starts a type-specifier-seq, it may be either a |
| 3469 | // 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] | 3470 | // 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] | 3471 | // fixed underlying type. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3472 | else if (TPR == TPResult::False() && |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3473 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 3474 | // Consume the ':'. |
| 3475 | ConsumeToken(); |
| 3476 | } else { |
| 3477 | // We have the start of a type-specifier-seq, so we have to perform |
| 3478 | // tentative parsing to determine whether we have an expression or a |
| 3479 | // type. |
| 3480 | TentativeParsingAction TPA(*this); |
| 3481 | |
| 3482 | // Consume the ':'. |
| 3483 | ConsumeToken(); |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3484 | |
| 3485 | // If we see a type specifier followed by an open-brace, we have an |
| 3486 | // ambiguity between an underlying type and a C++11 braced |
| 3487 | // function-style cast. Resolve this by always treating it as an |
| 3488 | // underlying type. |
| 3489 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 3490 | // this case. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3491 | if ((getLangOpts().CPlusPlus && |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3492 | isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3493 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3494 | // We'll parse this as a bitfield later. |
| 3495 | PossibleBitfield = true; |
| 3496 | TPA.Revert(); |
| 3497 | } else { |
| 3498 | // We have a type-specifier-seq. |
| 3499 | TPA.Commit(); |
| 3500 | } |
| 3501 | } |
| 3502 | } else { |
| 3503 | // Consume the ':'. |
| 3504 | ConsumeToken(); |
| 3505 | } |
| 3506 | |
| 3507 | if (!PossibleBitfield) { |
| 3508 | SourceRange Range; |
| 3509 | BaseType = ParseTypeName(&Range); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3510 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3511 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3512 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Eli Friedman | cef3a7b | 2012-11-02 01:34:28 +0000 | [diff] [blame] | 3513 | } else if (!getLangOpts().ObjC2) { |
| 3514 | if (getLangOpts().CPlusPlus) |
| 3515 | Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range; |
| 3516 | else |
| 3517 | Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range; |
| 3518 | } |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3519 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3520 | } |
| 3521 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3522 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 3523 | // friend declaration, and cannot have an accompanying definition. If we have |
| 3524 | // 'enum foo;', then this is a forward declaration. If we have |
| 3525 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 3526 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3527 | // |
| 3528 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 3529 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 3530 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 3531 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3532 | Sema::TagUseKind TUK; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3533 | if (!AllowDeclaration) { |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3534 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3535 | } else if (Tok.is(tok::l_brace)) { |
| 3536 | if (DS.isFriendSpecified()) { |
| 3537 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
| 3538 | << SourceRange(DS.getFriendSpecLoc()); |
| 3539 | ConsumeBrace(); |
| 3540 | SkipUntil(tok::r_brace); |
| 3541 | TUK = Sema::TUK_Friend; |
| 3542 | } else { |
| 3543 | TUK = Sema::TUK_Definition; |
| 3544 | } |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3545 | } else if (DSC != DSC_type_specifier && |
| 3546 | (Tok.is(tok::semi) || |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3547 | (Tok.isAtStartOfLine() && |
| 3548 | !isValidAfterTypeSpecifier(CanBeBitfield)))) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3549 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
| 3550 | if (Tok.isNot(tok::semi)) { |
| 3551 | // A semicolon was missing after this declaration. Diagnose and recover. |
| 3552 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, |
| 3553 | "enum"); |
| 3554 | PP.EnterToken(Tok); |
| 3555 | Tok.setKind(tok::semi); |
| 3556 | } |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3557 | } else { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3558 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3559 | } |
| 3560 | |
| 3561 | // If this is an elaborated type specifier, and we delayed |
| 3562 | // diagnostics before, just merge them into the current pool. |
| 3563 | if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) { |
| 3564 | diagsFromTag.redelay(); |
| 3565 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3566 | |
| 3567 | MultiTemplateParamsArg TParams; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3568 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3569 | TUK != Sema::TUK_Reference) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3570 | if (!getLangOpts().CPlusPlus11 || !SS.isSet()) { |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3571 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3572 | Diag(Tok, diag::err_enum_template); |
| 3573 | SkipUntil(tok::comma, true); |
| 3574 | return; |
| 3575 | } |
| 3576 | |
| 3577 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 3578 | // Enumerations can't be explicitly instantiated. |
| 3579 | DS.SetTypeSpecError(); |
| 3580 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 3581 | return; |
| 3582 | } |
| 3583 | |
| 3584 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 3585 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 3586 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3587 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3588 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3589 | if (TUK == Sema::TUK_Reference) |
| 3590 | ProhibitAttributes(attrs); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3591 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3592 | if (!Name && TUK != Sema::TUK_Definition) { |
| 3593 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3594 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3595 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3596 | SkipUntil(tok::comma, true); |
| 3597 | return; |
| 3598 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3599 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3600 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3601 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3602 | const char *PrevSpec = 0; |
| 3603 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3604 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3605 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3606 | AS, DS.getModulePrivateSpecLoc(), TParams, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3607 | Owned, IsDependent, ScopedEnumKWLoc, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3608 | IsScopedUsingClassTag, BaseType); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3609 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3610 | if (IsDependent) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3611 | // This enum has a dependent nested-name-specifier. Handle it as a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3612 | // dependent tag. |
| 3613 | if (!Name) { |
| 3614 | DS.SetTypeSpecError(); |
| 3615 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 3616 | return; |
| 3617 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3618 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3619 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3620 | TUK, SS, Name, StartLoc, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3621 | NameLoc); |
| 3622 | if (Type.isInvalid()) { |
| 3623 | DS.SetTypeSpecError(); |
| 3624 | return; |
| 3625 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3626 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3627 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 3628 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3629 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3630 | Diag(StartLoc, DiagID) << PrevSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3631 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3632 | return; |
| 3633 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3634 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3635 | if (!TagDecl) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3636 | // The action failed to produce an enumeration tag. If this is a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3637 | // definition, consume the entire definition. |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3638 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3639 | ConsumeBrace(); |
| 3640 | SkipUntil(tok::r_brace); |
| 3641 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3642 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3643 | DS.SetTypeSpecError(); |
| 3644 | return; |
| 3645 | } |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3646 | |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3647 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3648 | ParseEnumBody(StartLoc, TagDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3649 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3650 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3651 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3652 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3653 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3654 | } |
| 3655 | |
| 3656 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3657 | /// enumerator-list: |
| 3658 | /// enumerator |
| 3659 | /// enumerator-list ',' enumerator |
| 3660 | /// enumerator: |
| 3661 | /// enumeration-constant |
| 3662 | /// enumeration-constant '=' constant-expression |
| 3663 | /// enumeration-constant: |
| 3664 | /// identifier |
| 3665 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3666 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3667 | // Enter the scope of the enum body and start the definition. |
| 3668 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3669 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3670 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3671 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3672 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3673 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3674 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3675 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3676 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3677 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3678 | SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3679 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3680 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3681 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3682 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3683 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3684 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3685 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3686 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3687 | // If attributes exist after the enumerator, parse them. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3688 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3689 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3690 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3691 | ProhibitAttributes(attrs); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3692 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3693 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3694 | ExprResult AssignedVal; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 3695 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3696 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3697 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3698 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3699 | AssignedVal = ParseConstantExpression(); |
| 3700 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3701 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3702 | } |
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 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3705 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3706 | LastEnumConstDecl, |
| 3707 | IdentLoc, Ident, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3708 | attrs.getList(), EqualLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3709 | AssignedVal.release()); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3710 | PD.complete(EnumConstDecl); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3711 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3712 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3713 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3714 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3715 | if (Tok.is(tok::identifier)) { |
| 3716 | // We're missing a comma between enumerators. |
| 3717 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3718 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3719 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3720 | continue; |
| 3721 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3722 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3723 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3724 | break; |
| 3725 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3726 | |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3727 | if (Tok.isNot(tok::identifier)) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3728 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11) |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3729 | Diag(CommaLoc, getLangOpts().CPlusPlus ? |
| 3730 | diag::ext_enumerator_list_comma_cxx : |
| 3731 | diag::ext_enumerator_list_comma_c) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3732 | << FixItHint::CreateRemoval(CommaLoc); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3733 | else if (getLangOpts().CPlusPlus11) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3734 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3735 | << FixItHint::CreateRemoval(CommaLoc); |
| 3736 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3737 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3738 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3739 | // Eat the }. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3740 | T.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3741 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3742 | // If attributes exist after the identifier list, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3743 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3744 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3745 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3746 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
| 3747 | EnumDecl, EnumConstantDecls.data(), |
| 3748 | EnumConstantDecls.size(), getCurScope(), |
| 3749 | attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3750 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3751 | EnumScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3752 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3753 | T.getCloseLocation()); |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3754 | |
| 3755 | // The next token must be valid after an enum definition. If not, a ';' |
| 3756 | // was probably forgotten. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3757 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
| 3758 | if (!isValidAfterTypeSpecifier(CanBeBitfield)) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3759 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum"); |
| 3760 | // Push this token back into the preprocessor and change our current token |
| 3761 | // to ';' so that the rest of the code recovers as though there were an |
| 3762 | // ';' after the definition. |
| 3763 | PP.EnterToken(Tok); |
| 3764 | Tok.setKind(tok::semi); |
| 3765 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3766 | } |
| 3767 | |
| 3768 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3769 | /// start of a type-qualifier-list. |
| 3770 | bool Parser::isTypeQualifier() const { |
| 3771 | switch (Tok.getKind()) { |
| 3772 | default: return false; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3773 | |
| 3774 | // type-qualifier only in OpenCL |
| 3775 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3776 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3777 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3778 | // type-qualifier |
| 3779 | case tok::kw_const: |
| 3780 | case tok::kw_volatile: |
| 3781 | case tok::kw_restrict: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3782 | case tok::kw___private: |
| 3783 | case tok::kw___local: |
| 3784 | case tok::kw___global: |
| 3785 | case tok::kw___constant: |
| 3786 | case tok::kw___read_only: |
| 3787 | case tok::kw___read_write: |
| 3788 | case tok::kw___write_only: |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3789 | return true; |
| 3790 | } |
| 3791 | } |
| 3792 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3793 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3794 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3795 | /// specifier or if we're not sure. |
| 3796 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3797 | switch (Tok.getKind()) { |
| 3798 | default: return false; |
| 3799 | // type-specifiers |
| 3800 | case tok::kw_short: |
| 3801 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3802 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3803 | case tok::kw___int128: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3804 | case tok::kw_signed: |
| 3805 | case tok::kw_unsigned: |
| 3806 | case tok::kw__Complex: |
| 3807 | case tok::kw__Imaginary: |
| 3808 | case tok::kw_void: |
| 3809 | case tok::kw_char: |
| 3810 | case tok::kw_wchar_t: |
| 3811 | case tok::kw_char16_t: |
| 3812 | case tok::kw_char32_t: |
| 3813 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3814 | case tok::kw_half: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3815 | case tok::kw_float: |
| 3816 | case tok::kw_double: |
| 3817 | case tok::kw_bool: |
| 3818 | case tok::kw__Bool: |
| 3819 | case tok::kw__Decimal32: |
| 3820 | case tok::kw__Decimal64: |
| 3821 | case tok::kw__Decimal128: |
| 3822 | case tok::kw___vector: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3823 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3824 | // OpenCL specific types: |
| 3825 | case tok::kw_image1d_t: |
| 3826 | case tok::kw_image1d_array_t: |
| 3827 | case tok::kw_image1d_buffer_t: |
| 3828 | case tok::kw_image2d_t: |
| 3829 | case tok::kw_image2d_array_t: |
| 3830 | case tok::kw_image3d_t: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3831 | case tok::kw_sampler_t: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3832 | case tok::kw_event_t: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3833 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3834 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3835 | case tok::kw_class: |
| 3836 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3837 | case tok::kw___interface: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3838 | case tok::kw_union: |
| 3839 | // enum-specifier |
| 3840 | case tok::kw_enum: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3841 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3842 | // typedef-name |
| 3843 | case tok::annot_typename: |
| 3844 | return true; |
| 3845 | } |
| 3846 | } |
| 3847 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3848 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3849 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3850 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3851 | switch (Tok.getKind()) { |
| 3852 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3853 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3854 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3855 | if (TryAltiVecVectorToken()) |
| 3856 | return true; |
| 3857 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3858 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3859 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3860 | // recurse to handle whatever we get. |
| 3861 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3862 | return true; |
| 3863 | if (Tok.is(tok::identifier)) |
| 3864 | return false; |
| 3865 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3866 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3867 | case tok::coloncolon: // ::foo::bar |
| 3868 | if (NextToken().is(tok::kw_new) || // ::new |
| 3869 | NextToken().is(tok::kw_delete)) // ::delete |
| 3870 | return false; |
| 3871 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3872 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3873 | return true; |
| 3874 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3875 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3876 | // GNU attributes support. |
| 3877 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3878 | // GNU typeof support. |
| 3879 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3880 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3881 | // type-specifiers |
| 3882 | case tok::kw_short: |
| 3883 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3884 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3885 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3886 | case tok::kw_signed: |
| 3887 | case tok::kw_unsigned: |
| 3888 | case tok::kw__Complex: |
| 3889 | case tok::kw__Imaginary: |
| 3890 | case tok::kw_void: |
| 3891 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3892 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3893 | case tok::kw_char16_t: |
| 3894 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3895 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3896 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3897 | case tok::kw_float: |
| 3898 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3899 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3900 | case tok::kw__Bool: |
| 3901 | case tok::kw__Decimal32: |
| 3902 | case tok::kw__Decimal64: |
| 3903 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3904 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3905 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3906 | // OpenCL specific types: |
| 3907 | case tok::kw_image1d_t: |
| 3908 | case tok::kw_image1d_array_t: |
| 3909 | case tok::kw_image1d_buffer_t: |
| 3910 | case tok::kw_image2d_t: |
| 3911 | case tok::kw_image2d_array_t: |
| 3912 | case tok::kw_image3d_t: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3913 | case tok::kw_sampler_t: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3914 | case tok::kw_event_t: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3915 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3916 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3917 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3918 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3919 | case tok::kw___interface: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3920 | case tok::kw_union: |
| 3921 | // enum-specifier |
| 3922 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3923 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3924 | // type-qualifier |
| 3925 | case tok::kw_const: |
| 3926 | case tok::kw_volatile: |
| 3927 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3928 | |
John McCall | b8a8de3 | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 3929 | // Debugger support. |
| 3930 | case tok::kw___unknown_anytype: |
| 3931 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3932 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 3933 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3934 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3935 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 3936 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3937 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3938 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3939 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3940 | case tok::kw___cdecl: |
| 3941 | case tok::kw___stdcall: |
| 3942 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3943 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3944 | case tok::kw___w64: |
| 3945 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3946 | case tok::kw___ptr32: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3947 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3948 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3949 | |
| 3950 | case tok::kw___private: |
| 3951 | case tok::kw___local: |
| 3952 | case tok::kw___global: |
| 3953 | case tok::kw___constant: |
| 3954 | case tok::kw___read_only: |
| 3955 | case tok::kw___read_write: |
| 3956 | case tok::kw___write_only: |
| 3957 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3958 | return true; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3959 | |
| 3960 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3961 | return getLangOpts().OpenCL; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3962 | |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 3963 | // C11 _Atomic |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3964 | case tok::kw__Atomic: |
| 3965 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3966 | } |
| 3967 | } |
| 3968 | |
| 3969 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 3970 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3971 | /// |
| 3972 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 3973 | /// this check is to disambiguate between an expression and a declaration. |
| 3974 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3975 | switch (Tok.getKind()) { |
| 3976 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3977 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3978 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3979 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3980 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3981 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3982 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3983 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3984 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3985 | if (TryAltiVecVectorToken()) |
| 3986 | return true; |
| 3987 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3988 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3989 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3990 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3991 | // recurse to handle whatever we get. |
| 3992 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3993 | return true; |
| 3994 | if (Tok.is(tok::identifier)) |
| 3995 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3996 | |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3997 | // 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] | 3998 | // by an identifier and then either ':' or ']', in a place where an |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3999 | // expression is permitted, then this is probably a class message send |
| 4000 | // missing the initial '['. In this case, we won't consider this to be |
| 4001 | // the start of a declaration. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4002 | if (DisambiguatingWithExpression && |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 4003 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 4004 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4005 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4006 | return isDeclarationSpecifier(); |
| 4007 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4008 | case tok::coloncolon: // ::foo::bar |
| 4009 | if (NextToken().is(tok::kw_new) || // ::new |
| 4010 | NextToken().is(tok::kw_delete)) // ::delete |
| 4011 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4012 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 4013 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 4014 | // recurse to handle whatever we get. |
| 4015 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4016 | return true; |
| 4017 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4018 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4019 | // storage-class-specifier |
| 4020 | case tok::kw_typedef: |
| 4021 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 4022 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4023 | case tok::kw_static: |
| 4024 | case tok::kw_auto: |
| 4025 | case tok::kw_register: |
| 4026 | case tok::kw___thread: |
Richard Smith | ec64244 | 2013-04-12 22:46:28 +0000 | [diff] [blame] | 4027 | case tok::kw_thread_local: |
| 4028 | case tok::kw__Thread_local: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4029 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 4030 | // Modules |
| 4031 | case tok::kw___module_private__: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4032 | |
John McCall | b8a8de3 | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 4033 | // Debugger support |
| 4034 | case tok::kw___unknown_anytype: |
| 4035 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4036 | // type-specifiers |
| 4037 | case tok::kw_short: |
| 4038 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 4039 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 4040 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4041 | case tok::kw_signed: |
| 4042 | case tok::kw_unsigned: |
| 4043 | case tok::kw__Complex: |
| 4044 | case tok::kw__Imaginary: |
| 4045 | case tok::kw_void: |
| 4046 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 4047 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 4048 | case tok::kw_char16_t: |
| 4049 | case tok::kw_char32_t: |
| 4050 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4051 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 4052 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4053 | case tok::kw_float: |
| 4054 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 4055 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4056 | case tok::kw__Bool: |
| 4057 | case tok::kw__Decimal32: |
| 4058 | case tok::kw__Decimal64: |
| 4059 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 4060 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4061 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 4062 | // OpenCL specific types: |
| 4063 | case tok::kw_image1d_t: |
| 4064 | case tok::kw_image1d_array_t: |
| 4065 | case tok::kw_image1d_buffer_t: |
| 4066 | case tok::kw_image2d_t: |
| 4067 | case tok::kw_image2d_array_t: |
| 4068 | case tok::kw_image3d_t: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 4069 | case tok::kw_sampler_t: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 4070 | case tok::kw_event_t: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 4071 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 4072 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 4073 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4074 | case tok::kw_struct: |
| 4075 | case tok::kw_union: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 4076 | case tok::kw___interface: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4077 | // enum-specifier |
| 4078 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4079 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4080 | // type-qualifier |
| 4081 | case tok::kw_const: |
| 4082 | case tok::kw_volatile: |
| 4083 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4084 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4085 | // function-specifier |
| 4086 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4087 | case tok::kw_virtual: |
| 4088 | case tok::kw_explicit: |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 4089 | case tok::kw__Noreturn: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4090 | |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 4091 | // alignment-specifier |
| 4092 | case tok::kw__Alignas: |
| 4093 | |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4094 | // friend keyword. |
| 4095 | case tok::kw_friend: |
| 4096 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 4097 | // static_assert-declaration |
| 4098 | case tok::kw__Static_assert: |
| 4099 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4100 | // GNU typeof support. |
| 4101 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4102 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 4103 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 4104 | case tok::kw___attribute: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4105 | |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4106 | // C++11 decltype and constexpr. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4107 | case tok::annot_decltype: |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 4108 | case tok::kw_constexpr: |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 4109 | |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4110 | // C11 _Atomic |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4111 | case tok::kw__Atomic: |
| 4112 | return true; |
| 4113 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 4114 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 4115 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4116 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4117 | |
Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 4118 | // typedef-name |
| 4119 | case tok::annot_typename: |
| 4120 | return !DisambiguatingWithExpression || |
| 4121 | !isStartOfObjCClassMessageMissingOpenBracket(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4122 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 4123 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4124 | case tok::kw___cdecl: |
| 4125 | case tok::kw___stdcall: |
| 4126 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4127 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4128 | case tok::kw___w64: |
| 4129 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4130 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4131 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4132 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4133 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4134 | |
| 4135 | case tok::kw___private: |
| 4136 | case tok::kw___local: |
| 4137 | case tok::kw___global: |
| 4138 | case tok::kw___constant: |
| 4139 | case tok::kw___read_only: |
| 4140 | case tok::kw___read_write: |
| 4141 | case tok::kw___write_only: |
| 4142 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4143 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4144 | } |
| 4145 | } |
| 4146 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4147 | bool Parser::isConstructorDeclarator() { |
| 4148 | TentativeParsingAction TPA(*this); |
| 4149 | |
| 4150 | // Parse the C++ scope specifier. |
| 4151 | CXXScopeSpec SS; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4152 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4153 | /*EnteringContext=*/true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4154 | TPA.Revert(); |
| 4155 | return false; |
| 4156 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4157 | |
| 4158 | // Parse the constructor name. |
| 4159 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 4160 | // We already know that we have a constructor name; just consume |
| 4161 | // the token. |
| 4162 | ConsumeToken(); |
| 4163 | } else { |
| 4164 | TPA.Revert(); |
| 4165 | return false; |
| 4166 | } |
| 4167 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4168 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4169 | if (Tok.isNot(tok::l_paren)) { |
| 4170 | TPA.Revert(); |
| 4171 | return false; |
| 4172 | } |
| 4173 | ConsumeParen(); |
| 4174 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4175 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 4176 | // that we have a constructor. |
| 4177 | if (Tok.is(tok::r_paren) || |
| 4178 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4179 | TPA.Revert(); |
| 4180 | return true; |
| 4181 | } |
| 4182 | |
| 4183 | // If we need to, enter the specified scope. |
| 4184 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4185 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4186 | DeclScopeObj.EnterDeclaratorScope(); |
| 4187 | |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4188 | // Optionally skip Microsoft attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4189 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4190 | MaybeParseMicrosoftAttributes(Attrs); |
| 4191 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4192 | // Check whether the next token(s) are part of a declaration |
| 4193 | // specifier, in which case we have the start of a parameter and, |
| 4194 | // therefore, we know that this is a constructor. |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4195 | bool IsConstructor = false; |
| 4196 | if (isDeclarationSpecifier()) |
| 4197 | IsConstructor = true; |
| 4198 | else if (Tok.is(tok::identifier) || |
| 4199 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 4200 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 4201 | // This might be a parenthesized member name, but is more likely to |
| 4202 | // be a constructor declaration with an invalid argument type. Keep |
| 4203 | // looking. |
| 4204 | if (Tok.is(tok::annot_cxxscope)) |
| 4205 | ConsumeToken(); |
| 4206 | ConsumeToken(); |
| 4207 | |
| 4208 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4209 | // which must have one of the following syntactic forms (see the |
| 4210 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4211 | switch (Tok.getKind()) { |
| 4212 | case tok::l_paren: |
| 4213 | // C(X ( int)); |
| 4214 | case tok::l_square: |
| 4215 | // C(X [ 5]); |
| 4216 | // C(X [ [attribute]]); |
| 4217 | case tok::coloncolon: |
| 4218 | // C(X :: Y); |
| 4219 | // C(X :: *p); |
| 4220 | case tok::r_paren: |
| 4221 | // C(X ) |
| 4222 | // Assume this isn't a constructor, rather than assuming it's a |
| 4223 | // constructor with an unnamed parameter of an ill-formed type. |
| 4224 | break; |
| 4225 | |
| 4226 | default: |
| 4227 | IsConstructor = true; |
| 4228 | break; |
| 4229 | } |
| 4230 | } |
| 4231 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4232 | TPA.Revert(); |
| 4233 | return IsConstructor; |
| 4234 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4235 | |
| 4236 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4237 | /// type-qualifier-list: [C99 6.7.5] |
| 4238 | /// type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4239 | /// [vendor] attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4240 | /// [ only if VendorAttributesAllowed=true ] |
| 4241 | /// type-qualifier-list type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4242 | /// [vendor] type-qualifier-list attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4243 | /// [ only if VendorAttributesAllowed=true ] |
| 4244 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4245 | /// [ only if CXX11AttributesAllowed=true ] |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4246 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4247 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4248 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 4249 | bool VendorAttributesAllowed, |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4250 | bool CXX11AttributesAllowed, |
| 4251 | bool AtomicAllowed) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4252 | if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed && |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4253 | isCXX11AttributeSpecifier()) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4254 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4255 | ParseCXX11Attributes(attrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4256 | DS.takeAttributesFrom(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4257 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4258 | |
| 4259 | SourceLocation EndLoc; |
| 4260 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4261 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4262 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4263 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4264 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4265 | SourceLocation Loc = Tok.getLocation(); |
| 4266 | |
| 4267 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4268 | case tok::code_completion: |
| 4269 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 4270 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4271 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4272 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4273 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4274 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4275 | break; |
| 4276 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4277 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4278 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4279 | break; |
| 4280 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4281 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4282 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4283 | break; |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4284 | case tok::kw__Atomic: |
| 4285 | if (!AtomicAllowed) |
| 4286 | goto DoneWithTypeQuals; |
| 4287 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID, |
| 4288 | getLangOpts()); |
| 4289 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4290 | |
| 4291 | // OpenCL qualifiers: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4292 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4293 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4294 | goto DoneWithTypeQuals; |
| 4295 | case tok::kw___private: |
| 4296 | case tok::kw___global: |
| 4297 | case tok::kw___local: |
| 4298 | case tok::kw___constant: |
| 4299 | case tok::kw___read_only: |
| 4300 | case tok::kw___write_only: |
| 4301 | case tok::kw___read_write: |
| 4302 | ParseOpenCLQualifiers(DS); |
| 4303 | break; |
| 4304 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4305 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 4306 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4307 | case tok::kw___ptr32: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4308 | case tok::kw___cdecl: |
| 4309 | case tok::kw___stdcall: |
| 4310 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4311 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4312 | case tok::kw___unaligned: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4313 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4314 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4315 | continue; |
| 4316 | } |
| 4317 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4318 | case tok::kw___pascal: |
| 4319 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4320 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4321 | continue; |
| 4322 | } |
| 4323 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4324 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4325 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4326 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4327 | continue; // do *not* consume the next token! |
| 4328 | } |
| 4329 | // otherwise, FALL THROUGH! |
| 4330 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4331 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4332 | // If this is not a type-qualifier token, we're done reading type |
| 4333 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 4334 | DS.Finish(Diags, PP); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4335 | if (EndLoc.isValid()) |
| 4336 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4337 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4338 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4339 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4340 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 4341 | if (isInvalid) { |
| 4342 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4343 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4344 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4345 | EndLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4346 | } |
| 4347 | } |
| 4348 | |
| 4349 | |
| 4350 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 4351 | /// |
| 4352 | void Parser::ParseDeclarator(Declarator &D) { |
| 4353 | /// This implements the 'declarator' production in the C grammar, then checks |
| 4354 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4355 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4356 | } |
| 4357 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4358 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) { |
| 4359 | if (Kind == tok::star || Kind == tok::caret) |
| 4360 | return true; |
| 4361 | |
| 4362 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 4363 | if (!Lang.CPlusPlus) |
| 4364 | return false; |
| 4365 | |
| 4366 | return Kind == tok::amp || Kind == tok::ampamp; |
| 4367 | } |
| 4368 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4369 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 4370 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 4371 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4372 | /// ptr-operator production. |
| 4373 | /// |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4374 | /// 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] | 4375 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 4376 | /// isConstructorDeclarator. |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4377 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4378 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 4379 | /// [C] pointer[opt] direct-declarator |
| 4380 | /// [C++] direct-declarator |
| 4381 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4382 | /// |
| 4383 | /// pointer: [C99 6.7.5] |
| 4384 | /// '*' type-qualifier-list[opt] |
| 4385 | /// '*' type-qualifier-list[opt] pointer |
| 4386 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4387 | /// ptr-operator: |
| 4388 | /// '*' cv-qualifier-seq[opt] |
| 4389 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4390 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4391 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4392 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4393 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4394 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 4395 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 4396 | if (Diags.hasAllExtensionsSilenced()) |
| 4397 | D.setExtension(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4398 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4399 | // C++ member pointers start with a '::' or a nested-name. |
| 4400 | // Member pointers get special handling, since there's no place for the |
| 4401 | // scope spec in the generic path below. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4402 | if (getLangOpts().CPlusPlus && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 4403 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 4404 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4405 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4406 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4407 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4408 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4409 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 4410 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4411 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4412 | // The scope spec really belongs to the direct-declarator. |
Richard Smith | 6a502c4 | 2013-01-08 22:43:49 +0000 | [diff] [blame] | 4413 | if (D.mayHaveIdentifier()) |
| 4414 | D.getCXXScopeSpec() = SS; |
| 4415 | else |
| 4416 | AnnotateScopeToken(SS, true); |
| 4417 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4418 | if (DirectDeclParser) |
| 4419 | (this->*DirectDeclParser)(D); |
| 4420 | return; |
| 4421 | } |
| 4422 | |
| 4423 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4424 | D.SetRangeEnd(Loc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4425 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4426 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4427 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4428 | |
| 4429 | // Recurse to parse whatever is left. |
| 4430 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 4431 | |
| 4432 | // Sema will have to catch (syntactically invalid) pointers into global |
| 4433 | // scope. It has to catch pointers into namespace scope anyway. |
| 4434 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4435 | Loc), |
| 4436 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4437 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4438 | return; |
| 4439 | } |
| 4440 | } |
| 4441 | |
| 4442 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4443 | // Not a pointer, C++ reference, or block. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4444 | if (!isPtrOperatorToken(Kind, getLangOpts())) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4445 | if (DirectDeclParser) |
| 4446 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4447 | return; |
| 4448 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4449 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4450 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 4451 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4452 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4453 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4454 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 4455 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4456 | // Is a pointer. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4457 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4458 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4459 | // FIXME: GNU attributes are not allowed here in a new-type-id. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4460 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4461 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4462 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4463 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4464 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4465 | if (Kind == tok::star) |
| 4466 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 4467 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 4468 | DS.getConstSpecLoc(), |
| 4469 | DS.getVolatileSpecLoc(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4470 | DS.getRestrictSpecLoc()), |
| 4471 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4472 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4473 | else |
| 4474 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4475 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4476 | Loc), |
| 4477 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4478 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4479 | } else { |
| 4480 | // Is a reference |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4481 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4482 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4483 | // Complain about rvalue references in C++03, but then go on and build |
| 4484 | // the declarator. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4485 | if (Kind == tok::ampamp) |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4486 | Diag(Loc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4487 | diag::warn_cxx98_compat_rvalue_reference : |
| 4488 | diag::ext_rvalue_reference); |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4489 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4490 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 4491 | ParseTypeQualifierListOpt(DS); |
| 4492 | D.ExtendWithDeclSpec(DS); |
| 4493 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4494 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 4495 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 4496 | // template type argument, in which case the cv-qualifiers are ignored. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4497 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 4498 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 4499 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4500 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4501 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 4502 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4503 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4504 | // 'restrict' is permitted as an extension. |
| 4505 | if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic) |
| 4506 | Diag(DS.getAtomicSpecLoc(), |
| 4507 | diag::err_invalid_reference_qualifier_application) << "_Atomic"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4508 | } |
| 4509 | |
| 4510 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4511 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4512 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4513 | if (D.getNumTypeObjects() > 0) { |
| 4514 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 4515 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 4516 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 4517 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 4518 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4519 | << II; |
| 4520 | else |
| 4521 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4522 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4523 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4524 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4525 | // can go ahead and build the (technically ill-formed) |
| 4526 | // declarator: reference collapsing will take care of it. |
| 4527 | } |
| 4528 | } |
| 4529 | |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4530 | // Remember that we parsed a reference type. |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4531 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4532 | Kind == tok::amp), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4533 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4534 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4535 | } |
| 4536 | } |
| 4537 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4538 | static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D, |
| 4539 | SourceLocation EllipsisLoc) { |
| 4540 | if (EllipsisLoc.isValid()) { |
| 4541 | FixItHint Insertion; |
| 4542 | if (!D.getEllipsisLoc().isValid()) { |
| 4543 | Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "..."); |
| 4544 | D.setEllipsisLoc(EllipsisLoc); |
| 4545 | } |
| 4546 | P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) |
| 4547 | << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName(); |
| 4548 | } |
| 4549 | } |
| 4550 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4551 | /// ParseDirectDeclarator |
| 4552 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4553 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4554 | /// '(' declarator ')' |
| 4555 | /// [GNU] '(' attributes declarator ')' |
| 4556 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4557 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4558 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4559 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4560 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4561 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 4562 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4563 | /// direct-declarator '(' parameter-type-list ')' |
| 4564 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4565 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4566 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 4567 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 4568 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4569 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 4570 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 4571 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4572 | /// [C++] declarator-id |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4573 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4574 | /// |
| 4575 | /// declarator-id: [C++ 8] |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4576 | /// '...'[opt] id-expression |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4577 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 4578 | /// |
| 4579 | /// id-expression: [C++ 5.1] |
| 4580 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4581 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4582 | /// |
| 4583 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4584 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4585 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4586 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4587 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 4588 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 4589 | /// |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4590 | /// Note, any additional constructs added here may need corresponding changes |
| 4591 | /// in isConstructorDeclarator. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4592 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4593 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4594 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4595 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4596 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4597 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4598 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4599 | D.getContext() == Declarator::MemberContext; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4600 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4601 | EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4602 | } |
| 4603 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4604 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4605 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 4606 | // Change the declaration context for name lookup, until this function |
| 4607 | // is exited (and the declarator has been parsed). |
| 4608 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4611 | // C++0x [dcl.fct]p14: |
| 4612 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4613 | // of a parameter-declaration-clause without a preceding comma. In |
| 4614 | // this case, the ellipsis is parsed as part of the |
| 4615 | // abstract-declarator if the type of the parameter names a template |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4616 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 4617 | // as part of the parameter-declaration-clause. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4618 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4619 | !((D.getContext() == Declarator::PrototypeContext || |
| 4620 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4621 | NextToken().is(tok::r_paren) && |
Richard Smith | 30f2a74 | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4622 | !D.hasGroupingParens() && |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4623 | !Actions.containsUnexpandedParameterPacks(D))) { |
| 4624 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 4625 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) { |
| 4626 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 4627 | // the user what they should have done. |
| 4628 | ParseDeclarator(D); |
| 4629 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4630 | return; |
| 4631 | } else |
| 4632 | D.setEllipsisLoc(EllipsisLoc); |
| 4633 | |
| 4634 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 4635 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 4636 | // the l_paren token. |
| 4637 | } |
| 4638 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4639 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 4640 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 4641 | // We found something that indicates the start of an unqualified-id. |
| 4642 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4643 | bool AllowConstructorName; |
| 4644 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 4645 | AllowConstructorName = false; |
| 4646 | else if (D.getCXXScopeSpec().isSet()) |
| 4647 | AllowConstructorName = |
| 4648 | (D.getContext() == Declarator::FileContext || |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 4649 | D.getContext() == Declarator::MemberContext); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4650 | else |
| 4651 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 4652 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4653 | SourceLocation TemplateKWLoc; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4654 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 4655 | /*EnteringContext=*/true, |
| 4656 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4657 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4658 | ParsedType(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4659 | TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4660 | D.getName()) || |
| 4661 | // Once we're past the identifier, if the scope was bad, mark the |
| 4662 | // whole declarator bad. |
| 4663 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4664 | D.SetIdentifier(0, Tok.getLocation()); |
| 4665 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4666 | } else { |
| 4667 | // Parsed the unqualified-id; update range information and move along. |
| 4668 | if (D.getSourceRange().getBegin().isInvalid()) |
| 4669 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 4670 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4671 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4672 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4673 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4674 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4675 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4676 | "There's a C++-specific check for tok::identifier above"); |
| 4677 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 4678 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 4679 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4680 | goto PastIdentifier; |
| 4681 | } |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4682 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4683 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4684 | // direct-declarator: '(' declarator ')' |
| 4685 | // direct-declarator: '(' attributes declarator ')' |
| 4686 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 4687 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4688 | |
| 4689 | // If the declarator was parenthesized, we entered the declarator |
| 4690 | // scope when parsing the parenthesized declarator, then exited |
| 4691 | // the scope already. Re-enter the scope, if we need to. |
| 4692 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4693 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4694 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4695 | if (!D.isInvalidType() && |
| 4696 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4697 | // Change the declaration context for name lookup, until this function |
| 4698 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4699 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4700 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4701 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4702 | // This could be something simple like "int" (in which case the declarator |
| 4703 | // portion is empty), if an abstract-declarator is allowed. |
| 4704 | D.SetIdentifier(0, Tok.getLocation()); |
Richard Smith | 30f2a74 | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4705 | |
| 4706 | // The grammar for abstract-pack-declarator does not allow grouping parens. |
| 4707 | // FIXME: Revisit this once core issue 1488 is resolved. |
| 4708 | if (D.hasEllipsis() && D.hasGroupingParens()) |
| 4709 | Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()), |
| 4710 | diag::ext_abstract_pack_declarator_parens); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4711 | } else { |
David Blaikie | e75d9cf | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 4712 | if (Tok.getKind() == tok::annot_pragma_parser_crash) |
David Blaikie | 377da4c | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 4713 | LLVM_BUILTIN_TRAP; |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 4714 | if (D.getContext() == Declarator::MemberContext) |
| 4715 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 4716 | << D.getDeclSpec().getSourceRange(); |
Richard Trieu | db55c04c | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 4717 | else if (getLangOpts().CPlusPlus) { |
| 4718 | if (Tok.is(tok::period) || Tok.is(tok::arrow)) |
| 4719 | Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow); |
| 4720 | else |
| 4721 | Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus; |
| 4722 | } else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4723 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4724 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 4725 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4726 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4727 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4728 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4729 | assert(D.isPastIdentifier() && |
| 4730 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4731 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4732 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 4733 | if (D.hasName() && !D.getNumTypeObjects()) |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4734 | MaybeParseCXX11Attributes(D); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4735 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4736 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4737 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4738 | // Enter function-declaration scope, limiting any declarators to the |
| 4739 | // function prototype scope, including parameter declarators. |
| 4740 | ParseScope PrototypeScope(this, |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4741 | Scope::FunctionPrototypeScope|Scope::DeclScope| |
| 4742 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 4743 | ? Scope::FunctionDeclarationScope : 0)); |
| 4744 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4745 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 4746 | // In such a case, check if we actually have a function declarator; if it |
| 4747 | // is not, the declarator has been fully parsed. |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4748 | bool IsAmbiguous = false; |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 4749 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 4750 | // The name of the declarator, if any, is tentatively declared within |
| 4751 | // a possible direct initializer. |
| 4752 | TentativelyDeclaredIdentifiers.push_back(D.getIdentifier()); |
| 4753 | bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous); |
| 4754 | TentativelyDeclaredIdentifiers.pop_back(); |
| 4755 | if (!IsFunctionDecl) |
| 4756 | break; |
| 4757 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4758 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4759 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4760 | T.consumeOpen(); |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4761 | ParseFunctionDeclarator(D, attrs, T, IsAmbiguous); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4762 | PrototypeScope.Exit(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4763 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4764 | ParseBracketDeclarator(D); |
| 4765 | } else { |
| 4766 | break; |
| 4767 | } |
| 4768 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4769 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4770 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4771 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 4772 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4773 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4774 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 4775 | /// |
| 4776 | /// direct-declarator: |
| 4777 | /// '(' declarator ')' |
| 4778 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4779 | /// direct-declarator '(' parameter-type-list ')' |
| 4780 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4781 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4782 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4783 | /// |
| 4784 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4785 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4786 | T.consumeOpen(); |
| 4787 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4788 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4789 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4790 | // Eat any attributes before we look at whether this is a grouping or function |
| 4791 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 4792 | // the type being built up, for example: |
| 4793 | // int (__attribute__(()) *x)(long y) |
| 4794 | // If this ends up not being a grouping paren, the attribute applies to the |
| 4795 | // first argument, for example: |
| 4796 | // int (__attribute__(()) int x) |
| 4797 | // In either case, we need to eat any attributes to be able to determine what |
| 4798 | // sort of paren this is. |
| 4799 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4800 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4801 | bool RequiresArg = false; |
| 4802 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4803 | ParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4804 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4805 | // We require that the argument list (if this is a non-grouping paren) be |
| 4806 | // present even if the attribute list was empty. |
| 4807 | RequiresArg = true; |
| 4808 | } |
Chad Rosier | 9cab1c9 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 4809 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4810 | // Eat any Microsoft extensions. |
Chad Rosier | 9cab1c9 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 4811 | ParseMicrosoftTypeAttributes(attrs); |
| 4812 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4813 | // Eat any Borland extensions. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 4814 | if (Tok.is(tok::kw___pascal)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4815 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4816 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4817 | // If we haven't past the identifier yet (or where the identifier would be |
| 4818 | // stored, if this is an abstract declarator), then this is probably just |
| 4819 | // grouping parens. However, if this could be an abstract-declarator, then |
| 4820 | // this could also be the start of function arguments (consider 'void()'). |
| 4821 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4822 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4823 | if (!D.mayOmitIdentifier()) { |
| 4824 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 4825 | // paren, because we haven't seen the identifier yet. |
| 4826 | isGrouping = true; |
| 4827 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4828 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 4829 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4830 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 4831 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4832 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 4833 | // considered to be a type, not a K&R identifier-list. |
| 4834 | isGrouping = false; |
| 4835 | } else { |
| 4836 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 4837 | isGrouping = true; |
| 4838 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4839 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4840 | // If this is a grouping paren, handle: |
| 4841 | // direct-declarator: '(' declarator ')' |
| 4842 | // direct-declarator: '(' attributes declarator ')' |
| 4843 | if (isGrouping) { |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4844 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 4845 | D.setEllipsisLoc(SourceLocation()); |
| 4846 | |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4847 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4848 | D.setGroupingParens(true); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4849 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4850 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4851 | T.consumeClose(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4852 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4853 | T.getCloseLocation()), |
| 4854 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4855 | |
| 4856 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4857 | |
| 4858 | // An ellipsis cannot be placed outside parentheses. |
| 4859 | if (EllipsisLoc.isValid()) |
| 4860 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4861 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4862 | return; |
| 4863 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4864 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4865 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 4866 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4867 | // identifier (and remember where it would have been), then call into |
| 4868 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4869 | D.SetIdentifier(0, Tok.getLocation()); |
| 4870 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4871 | // Enter function-declaration scope, limiting any declarators to the |
| 4872 | // function prototype scope, including parameter declarators. |
| 4873 | ParseScope PrototypeScope(this, |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4874 | Scope::FunctionPrototypeScope | Scope::DeclScope | |
| 4875 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 4876 | ? Scope::FunctionDeclarationScope : 0)); |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4877 | ParseFunctionDeclarator(D, attrs, T, false, RequiresArg); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4878 | PrototypeScope.Exit(); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4879 | } |
| 4880 | |
| 4881 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 4882 | /// declarator D up to a paren, which indicates that we are parsing function |
| 4883 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4884 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4885 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 4886 | /// immediately after the open paren - they should be considered to be the |
| 4887 | /// first argument of a parameter. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4888 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4889 | /// If RequiresArg is true, then the first argument of the function is required |
| 4890 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4891 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4892 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 4893 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 4894 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 4895 | /// |
| 4896 | /// [C++11] exception-specification: |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4897 | /// dynamic-exception-specification |
| 4898 | /// noexcept-specification |
| 4899 | /// |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4900 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4901 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4902 | BalancedDelimiterTracker &Tracker, |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4903 | bool IsAmbiguous, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4904 | bool RequiresArg) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4905 | assert(getCurScope()->isFunctionPrototypeScope() && |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4906 | "Should call from a Function scope"); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4907 | // lparen is already consumed! |
| 4908 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 4909 | |
| 4910 | // This should be true when the function has typed arguments. |
| 4911 | // Otherwise, it is treated as a K&R-style function. |
| 4912 | bool HasProto = false; |
| 4913 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4914 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4915 | // Remember where we see an ellipsis, if any. |
| 4916 | SourceLocation EllipsisLoc; |
| 4917 | |
| 4918 | DeclSpec DS(AttrFactory); |
| 4919 | bool RefQualifierIsLValueRef = true; |
| 4920 | SourceLocation RefQualifierLoc; |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4921 | SourceLocation ConstQualifierLoc; |
| 4922 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4923 | ExceptionSpecificationType ESpecType = EST_None; |
| 4924 | SourceRange ESpecRange; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4925 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 4926 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4927 | ExprResult NoexceptExpr; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4928 | ParsedAttributes FnAttrs(AttrFactory); |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 4929 | TypeResult TrailingReturnType; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4930 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4931 | Actions.ActOnStartFunctionDeclarator(); |
| 4932 | |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4933 | /* LocalEndLoc is the end location for the local FunctionTypeLoc. |
| 4934 | EndLoc is the end location for the function declarator. |
| 4935 | They differ for trailing return types. */ |
| 4936 | SourceLocation StartLoc, LocalEndLoc, EndLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4937 | SourceLocation LParenLoc, RParenLoc; |
| 4938 | LParenLoc = Tracker.getOpenLocation(); |
| 4939 | StartLoc = LParenLoc; |
| 4940 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4941 | if (isFunctionDeclaratorIdentifierList()) { |
| 4942 | if (RequiresArg) |
| 4943 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4944 | |
| 4945 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 4946 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4947 | Tracker.consumeClose(); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4948 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4949 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4950 | EndLoc = RParenLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4951 | } else { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4952 | if (Tok.isNot(tok::r_paren)) |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4953 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4954 | else if (RequiresArg) |
| 4955 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4956 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4957 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4958 | |
| 4959 | // If we have the closing ')', eat it. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4960 | Tracker.consumeClose(); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4961 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4962 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4963 | EndLoc = RParenLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4964 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4965 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4966 | // FIXME: Accept these components in any order, and produce fixits to |
| 4967 | // correct the order if the user gets it wrong. Ideally we should deal |
| 4968 | // with the virt-specifier-seq and pure-specifier in the same way. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4969 | |
| 4970 | // Parse cv-qualifier-seq[opt]. |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 4971 | ParseTypeQualifierListOpt(DS, /*VendorAttributesAllowed*/ false, |
| 4972 | /*CXX11AttributesAllowed*/ false, |
| 4973 | /*AtomicAllowed*/ false); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4974 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 4975 | EndLoc = DS.getSourceRange().getEnd(); |
| 4976 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 4977 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 4978 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4979 | |
| 4980 | // Parse ref-qualifier[opt]. |
| 4981 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4982 | Diag(Tok, getLangOpts().CPlusPlus11 ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4983 | diag::warn_cxx98_compat_ref_qualifier : |
| 4984 | diag::ext_ref_qualifier); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4985 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4986 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 4987 | RefQualifierLoc = ConsumeToken(); |
| 4988 | EndLoc = RefQualifierLoc; |
| 4989 | } |
| 4990 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4991 | // C++11 [expr.prim.general]p3: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4992 | // If a declaration declares a member function or member function |
| 4993 | // 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] | 4994 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4995 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4996 | // declarator. |
Richard Smith | d922779 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 4997 | // FIXME: currently, "static" case isn't handled correctly. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4998 | bool IsCXX11MemberFunction = |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4999 | getLangOpts().CPlusPlus11 && |
Richard Smith | d922779 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 5000 | (D.getContext() == Declarator::MemberContext |
| 5001 | ? !D.getDeclSpec().isFriendSpecified() |
| 5002 | : D.getContext() == Declarator::FileContext && |
| 5003 | D.getCXXScopeSpec().isValid() && |
| 5004 | Actions.CurContext->isRecord()); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5005 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 5006 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
Richard Smith | 7b19cb1 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 5007 | DS.getTypeQualifiers() | |
| 5008 | (D.getDeclSpec().isConstexprSpecified() |
| 5009 | ? Qualifiers::Const : 0), |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 5010 | IsCXX11MemberFunction); |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5011 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5012 | // Parse exception-specification[opt]. |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5013 | ESpecType = tryParseExceptionSpecification(ESpecRange, |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 5014 | DynamicExceptions, |
| 5015 | DynamicExceptionRanges, |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 5016 | NoexceptExpr); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5017 | if (ESpecType != EST_None) |
| 5018 | EndLoc = ESpecRange.getEnd(); |
| 5019 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5020 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 5021 | // after the exception-specification. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5022 | MaybeParseCXX11Attributes(FnAttrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5023 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5024 | // Parse trailing-return-type[opt]. |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5025 | LocalEndLoc = EndLoc; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5026 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 5027 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5028 | if (D.getDeclSpec().getTypeSpecType() == TST_auto) |
| 5029 | StartLoc = D.getDeclSpec().getTypeSpecTypeLoc(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5030 | LocalEndLoc = Tok.getLocation(); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 5031 | SourceRange Range; |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 5032 | TrailingReturnType = ParseTrailingReturnType(Range); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5033 | EndLoc = Range.getEnd(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5034 | } |
| 5035 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5036 | } |
| 5037 | |
| 5038 | // Remember that we parsed a function type, and remember the attributes. |
| 5039 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5040 | IsAmbiguous, |
| 5041 | LParenLoc, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5042 | ParamInfo.data(), ParamInfo.size(), |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 5043 | EllipsisLoc, RParenLoc, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5044 | DS.getTypeQualifiers(), |
| 5045 | RefQualifierIsLValueRef, |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 5046 | RefQualifierLoc, ConstQualifierLoc, |
| 5047 | VolatileQualifierLoc, |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 5048 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5049 | ESpecType, ESpecRange.getBegin(), |
| 5050 | DynamicExceptions.data(), |
| 5051 | DynamicExceptionRanges.data(), |
| 5052 | DynamicExceptions.size(), |
| 5053 | NoexceptExpr.isUsable() ? |
| 5054 | NoexceptExpr.get() : 0, |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 5055 | StartLoc, LocalEndLoc, D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5056 | TrailingReturnType), |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5057 | FnAttrs, EndLoc); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 5058 | |
| 5059 | Actions.ActOnEndFunctionDeclarator(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5060 | } |
| 5061 | |
| 5062 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 5063 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 5064 | /// |
| 5065 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 5066 | /// abstract-declarators. |
| 5067 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5068 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5069 | && Tok.is(tok::identifier) |
| 5070 | && !TryAltiVecVectorToken() |
| 5071 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 5072 | // 6.7.5.3p11. |
| 5073 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 5074 | // Identifier lists follow a really simple grammar: the identifiers can |
| 5075 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 5076 | // identifier lists are really rare in the brave new modern world, and |
| 5077 | // it is very common for someone to typo a type in a non-K&R style |
| 5078 | // list. If we are presented with something like: "void foo(intptr x, |
| 5079 | // float y)", we don't want to start parsing the function declarator as |
| 5080 | // though it is a K&R style declarator just because intptr is an |
| 5081 | // invalid type. |
| 5082 | // |
| 5083 | // To handle this, we check to see if the token after the first |
| 5084 | // identifier is a "," or ")". Only then do we parse it as an |
| 5085 | // identifier list. |
| 5086 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 5087 | } |
| 5088 | |
| 5089 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 5090 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 5091 | /// |
| 5092 | /// After returning, ParamInfo will hold the parsed parameters. |
| 5093 | /// |
| 5094 | /// identifier-list: [C99 6.7.5] |
| 5095 | /// identifier |
| 5096 | /// identifier-list ',' identifier |
| 5097 | /// |
| 5098 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 5099 | Declarator &D, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5100 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5101 | // If there was no identifier specified for the declarator, either we are in |
| 5102 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 5103 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 5104 | // diagnose this. |
| 5105 | if (!D.getIdentifier()) |
| 5106 | Diag(Tok, diag::ext_ident_list_in_param); |
| 5107 | |
| 5108 | // Maintain an efficient lookup of params we have seen so far. |
| 5109 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 5110 | |
| 5111 | while (1) { |
| 5112 | // If this isn't an identifier, report the error and skip until ')'. |
| 5113 | if (Tok.isNot(tok::identifier)) { |
| 5114 | Diag(Tok, diag::err_expected_ident); |
| 5115 | SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 5116 | // Forget we parsed anything. |
| 5117 | ParamInfo.clear(); |
| 5118 | return; |
| 5119 | } |
| 5120 | |
| 5121 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 5122 | |
| 5123 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 5124 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 5125 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 5126 | |
| 5127 | // Verify that the argument identifier has not already been mentioned. |
| 5128 | if (!ParamsSoFar.insert(ParmII)) { |
| 5129 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 5130 | } else { |
| 5131 | // Remember this identifier in ParamInfo. |
| 5132 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 5133 | Tok.getLocation(), |
| 5134 | 0)); |
| 5135 | } |
| 5136 | |
| 5137 | // Eat the identifier. |
| 5138 | ConsumeToken(); |
| 5139 | |
| 5140 | // The list continues if we see a comma. |
| 5141 | if (Tok.isNot(tok::comma)) |
| 5142 | break; |
| 5143 | ConsumeToken(); |
| 5144 | } |
| 5145 | } |
| 5146 | |
| 5147 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 5148 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 5149 | /// identifier list. |
| 5150 | /// |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5151 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 5152 | /// caller parsed those arguments immediately after the open paren - they should |
| 5153 | /// be considered to be part of the first parameter. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5154 | /// |
| 5155 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 5156 | /// be the location of the ellipsis, if any was parsed. |
| 5157 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5158 | /// parameter-type-list: [C99 6.7.5] |
| 5159 | /// parameter-list |
| 5160 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5161 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5162 | /// |
| 5163 | /// parameter-list: [C99 6.7.5] |
| 5164 | /// parameter-declaration |
| 5165 | /// parameter-list ',' parameter-declaration |
| 5166 | /// |
| 5167 | /// parameter-declaration: [C99 6.7.5] |
| 5168 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5169 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5170 | /// [C++11] initializer-clause |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5171 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 5172 | /// declaration-specifiers abstract-declarator[opt] |
| 5173 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 5174 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5175 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5176 | /// [C++11] attribute-specifier-seq parameter-declaration |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5177 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5178 | void Parser::ParseParameterDeclarationClause( |
| 5179 | Declarator &D, |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5180 | ParsedAttributes &FirstArgAttrs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5181 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5182 | SourceLocation &EllipsisLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5183 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5184 | while (1) { |
| 5185 | if (Tok.is(tok::ellipsis)) { |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5186 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 5187 | // before deciding this was a parameter-declaration-clause. |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 5188 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5189 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5190 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5191 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5192 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5193 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5194 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5195 | |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5196 | // Parse any C++11 attributes. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5197 | MaybeParseCXX11Attributes(DS.getAttributes()); |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5198 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5199 | // Skip any Microsoft attributes before a param. |
Chad Rosier | 16f90bf | 2012-12-20 20:37:53 +0000 | [diff] [blame] | 5200 | MaybeParseMicrosoftAttributes(DS.getAttributes()); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5201 | |
| 5202 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5203 | |
| 5204 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5205 | // 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] | 5206 | // 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] | 5207 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 5208 | // too much hassle. |
| 5209 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5210 | |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 5211 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5212 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5213 | // Parse the declarator. This is "PrototypeContext", because we must |
| 5214 | // accept either 'declarator' or 'abstract-declarator' here. |
| 5215 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 5216 | ParseDeclarator(ParmDecl); |
| 5217 | |
| 5218 | // Parse GNU attributes, if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5219 | MaybeParseGNUAttributes(ParmDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5220 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5221 | // Remember this parsed parameter in ParamInfo. |
| 5222 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5223 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5224 | // DefArgToks is used when the parsing of default arguments needs |
| 5225 | // to be delayed. |
| 5226 | CachedTokens *DefArgToks = 0; |
| 5227 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5228 | // If no parameter was specified, verify that *something* was specified, |
| 5229 | // otherwise we have a missing type and identifier. |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 5230 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 5231 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5232 | // Completely missing, emit error. |
| 5233 | Diag(DSStart, diag::err_missing_param); |
| 5234 | } else { |
| 5235 | // Otherwise, we have something. Add it and let semantic analysis try |
| 5236 | // 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] | 5237 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5238 | // Inform the actions module about the parameter declarator, so it gets |
| 5239 | // added to the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5240 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5241 | |
| 5242 | // Parse the default argument, if any. We parse the default |
| 5243 | // arguments in all dialects; the semantic analysis in |
| 5244 | // ActOnParamDefaultArgument will reject the default argument in |
| 5245 | // C. |
| 5246 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5247 | SourceLocation EqualLoc = Tok.getLocation(); |
| 5248 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5249 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5250 | if (D.getContext() == Declarator::MemberContext) { |
| 5251 | // If we're inside a class definition, cache the tokens |
| 5252 | // corresponding to the default argument. We'll actually parse |
| 5253 | // them when we see the end of the class definition. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5254 | // FIXME: Can we use a smart pointer for Toks? |
| 5255 | DefArgToks = new CachedTokens; |
| 5256 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5257 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 5258 | /*StopAtSemi=*/true, |
| 5259 | /*ConsumeFinalToken=*/false)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5260 | delete DefArgToks; |
| 5261 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5262 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5263 | } else { |
| 5264 | // Mark the end of the default argument so that we know when to |
| 5265 | // stop when we parse it later on. |
| 5266 | Token DefArgEnd; |
| 5267 | DefArgEnd.startToken(); |
| 5268 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 5269 | DefArgEnd.setLocation(Tok.getLocation()); |
| 5270 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5271 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 5272 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5273 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5274 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5275 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5276 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5277 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5278 | // The argument isn't actually potentially evaluated unless it is |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5279 | // used. |
| 5280 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 5281 | Sema::PotentiallyEvaluatedIfUsed, |
| 5282 | Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5283 | |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5284 | ExprResult DefArgResult; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5285 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5286 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5287 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5288 | } else |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5289 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5290 | if (DefArgResult.isInvalid()) { |
| 5291 | Actions.ActOnParamDefaultArgumentError(Param); |
| 5292 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 5293 | } else { |
| 5294 | // Inform the actions module about the default argument |
| 5295 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5296 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5297 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5298 | } |
| 5299 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5300 | |
| 5301 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 5302 | ParmDecl.getIdentifierLoc(), Param, |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5303 | DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5304 | } |
| 5305 | |
| 5306 | // 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] | 5307 | if (Tok.isNot(tok::comma)) { |
| 5308 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5309 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5310 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5311 | if (!getLangOpts().CPlusPlus) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5312 | // We have ellipsis without a preceding ',', which is ill-formed |
| 5313 | // in C. Complain and provide the fix. |
| 5314 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5315 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5316 | } |
| 5317 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5318 | |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5319 | break; |
| 5320 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5321 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5322 | // Consume the comma. |
| 5323 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5324 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5325 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 5326 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5327 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5328 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 5329 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 5330 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 5331 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 5332 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5333 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 5334 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5335 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5336 | if (CheckProhibitedCXX11Attribute()) |
| 5337 | return; |
| 5338 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5339 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 5340 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5341 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5342 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 5343 | // This code does a fast path to handle some of the most obvious cases. |
| 5344 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5345 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5346 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5347 | MaybeParseCXX11Attributes(attrs); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5348 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5349 | // Remember that we parsed the empty array type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5350 | ExprResult NumElements; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5351 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5352 | T.getOpenLocation(), |
| 5353 | T.getCloseLocation()), |
| 5354 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5355 | return; |
| 5356 | } else if (Tok.getKind() == tok::numeric_constant && |
| 5357 | GetLookAheadToken(1).is(tok::r_square)) { |
| 5358 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5359 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5360 | ConsumeToken(); |
| 5361 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5362 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5363 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5364 | MaybeParseCXX11Attributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5365 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5366 | // Remember that we parsed a array type, and remember its features. |
Nikola Smiljanic | ebf0fa8 | 2013-01-11 08:33:05 +0000 | [diff] [blame] | 5367 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5368 | ExprRes.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5369 | T.getOpenLocation(), |
| 5370 | T.getCloseLocation()), |
| 5371 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5372 | return; |
| 5373 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5374 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5375 | // If valid, this location is the position where we read the 'static' keyword. |
| 5376 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5377 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5378 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5379 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5380 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5381 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5382 | DeclSpec DS(AttrFactory); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5383 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5384 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5385 | // If we haven't already read 'static', check to see if there is one after the |
| 5386 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5387 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5388 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5389 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5390 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 5391 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5392 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5393 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5394 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 5395 | // 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] | 5396 | // the token after the star is a ']'. Since stars in arrays are |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5397 | // infrequent, use of lookahead is not costly here. |
| 5398 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 5399 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5400 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5401 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5402 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5403 | StaticLoc = SourceLocation(); // Drop the static. |
| 5404 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5405 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5406 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5407 | // Note, in C89, this production uses the constant-expr production instead |
| 5408 | // of assignment-expr. The only difference is that assignment-expr allows |
| 5409 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 5410 | // 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] | 5411 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5412 | // Parse the constant-expression or assignment-expression now (depending |
| 5413 | // on dialect). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5414 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5415 | NumElements = ParseConstantExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5416 | } else { |
| 5417 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 5418 | Sema::ConstantEvaluated); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5419 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5420 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5421 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5422 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5423 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 5424 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 5425 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5426 | // If the expression was invalid, skip it. |
| 5427 | SkipUntil(tok::r_square); |
| 5428 | return; |
| 5429 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5430 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5431 | T.consumeClose(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5432 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5433 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5434 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5435 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5436 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5437 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5438 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5439 | NumElements.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5440 | T.getOpenLocation(), |
| 5441 | T.getCloseLocation()), |
| 5442 | attrs, T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5443 | } |
| 5444 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5445 | /// [GNU] typeof-specifier: |
| 5446 | /// typeof ( expressions ) |
| 5447 | /// typeof ( type-name ) |
| 5448 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5449 | /// |
| 5450 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5451 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5452 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5453 | SourceLocation StartLoc = ConsumeToken(); |
| 5454 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5455 | const bool hasParens = Tok.is(tok::l_paren); |
| 5456 | |
Eli Friedman | 80bfa3d | 2012-09-26 04:34:21 +0000 | [diff] [blame] | 5457 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated, |
| 5458 | Sema::ReuseLambdaContextDecl); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5459 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5460 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5461 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5462 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5463 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 5464 | CastTy, CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5465 | if (hasParens) |
| 5466 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5467 | |
| 5468 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5469 | // FIXME: Not accurate, the range gets one token more than it should. |
| 5470 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5471 | else |
| 5472 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5473 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5474 | if (isCastExpr) { |
| 5475 | if (!CastTy) { |
| 5476 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5477 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 5478 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5479 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5480 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5481 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5482 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5483 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5484 | DiagID, CastTy)) |
| 5485 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5486 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5487 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5488 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5489 | // If we get here, the operand to the typeof was an expresion. |
| 5490 | if (Operand.isInvalid()) { |
| 5491 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 5492 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5493 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5494 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5495 | // We might need to transform the operand if it is potentially evaluated. |
| 5496 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 5497 | if (Operand.isInvalid()) { |
| 5498 | DS.SetTypeSpecError(); |
| 5499 | return; |
| 5500 | } |
| 5501 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5502 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5503 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5504 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5505 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5506 | DiagID, Operand.get())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5507 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5508 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5509 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 5510 | /// [C11] atomic-specifier: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5511 | /// _Atomic ( type-name ) |
| 5512 | /// |
| 5513 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5514 | assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) && |
| 5515 | "Not an atomic specifier"); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5516 | |
| 5517 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5518 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Richard Smith | 4cf4a5e | 2013-03-28 01:55:44 +0000 | [diff] [blame] | 5519 | if (T.consumeOpen()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5520 | return; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5521 | |
| 5522 | TypeResult Result = ParseTypeName(); |
| 5523 | if (Result.isInvalid()) { |
| 5524 | SkipUntil(tok::r_paren); |
| 5525 | return; |
| 5526 | } |
| 5527 | |
| 5528 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5529 | T.consumeClose(); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5530 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5531 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5532 | return; |
| 5533 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5534 | DS.setTypeofParensRange(T.getRange()); |
| 5535 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5536 | |
| 5537 | const char *PrevSpec = 0; |
| 5538 | unsigned DiagID; |
| 5539 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
| 5540 | DiagID, Result.release())) |
| 5541 | Diag(StartLoc, DiagID) << PrevSpec; |
| 5542 | } |
| 5543 | |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5544 | |
| 5545 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 5546 | /// from TryAltiVecVectorToken. |
| 5547 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 5548 | Token Next = NextToken(); |
| 5549 | switch (Next.getKind()) { |
| 5550 | default: return false; |
| 5551 | case tok::kw_short: |
| 5552 | case tok::kw_long: |
| 5553 | case tok::kw_signed: |
| 5554 | case tok::kw_unsigned: |
| 5555 | case tok::kw_void: |
| 5556 | case tok::kw_char: |
| 5557 | case tok::kw_int: |
| 5558 | case tok::kw_float: |
| 5559 | case tok::kw_double: |
| 5560 | case tok::kw_bool: |
| 5561 | case tok::kw___pixel: |
| 5562 | Tok.setKind(tok::kw___vector); |
| 5563 | return true; |
| 5564 | case tok::identifier: |
| 5565 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5566 | Tok.setKind(tok::kw___vector); |
| 5567 | return true; |
| 5568 | } |
| 5569 | return false; |
| 5570 | } |
| 5571 | } |
| 5572 | |
| 5573 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 5574 | const char *&PrevSpec, unsigned &DiagID, |
| 5575 | bool &isInvalid) { |
| 5576 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 5577 | Token Next = NextToken(); |
| 5578 | switch (Next.getKind()) { |
| 5579 | case tok::kw_short: |
| 5580 | case tok::kw_long: |
| 5581 | case tok::kw_signed: |
| 5582 | case tok::kw_unsigned: |
| 5583 | case tok::kw_void: |
| 5584 | case tok::kw_char: |
| 5585 | case tok::kw_int: |
| 5586 | case tok::kw_float: |
| 5587 | case tok::kw_double: |
| 5588 | case tok::kw_bool: |
| 5589 | case tok::kw___pixel: |
| 5590 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5591 | return true; |
| 5592 | case tok::identifier: |
| 5593 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5594 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5595 | return true; |
| 5596 | } |
| 5597 | break; |
| 5598 | default: |
| 5599 | break; |
| 5600 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 5601 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5602 | DS.isTypeAltiVecVector()) { |
| 5603 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 5604 | return true; |
| 5605 | } |
| 5606 | return false; |
| 5607 | } |