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. |
| 397 | // |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 398 | // For right now, we will just skip to the closing right paren of the |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 399 | // property expression. |
| 400 | // |
| 401 | // FIXME: we should deal with __declspec(property) at some point because it |
| 402 | // is used in the platform SDK headers for the Parallel Patterns Library |
| 403 | // and ATL. |
| 404 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 405 | if (T.expectAndConsume(diag::err_expected_lparen_after, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 406 | Ident->getNameStart(), tok::r_paren)) |
| 407 | return; |
| 408 | T.skipToEnd(); |
| 409 | } else { |
| 410 | // We don't recognize this as a valid declspec, but instead of creating the |
| 411 | // attribute and allowing sema to warn about it, we will warn here instead. |
| 412 | // This is because some attributes have multiple spellings, but we need to |
| 413 | // 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] | 414 | // attribute, we'd have to split the valid declspec spelling logic into |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 415 | // both locations. |
| 416 | Diag(Loc, diag::warn_ms_declspec_unknown) << Ident; |
| 417 | |
| 418 | // If there's an open paren, we should eat the open and close parens under |
| 419 | // the assumption that this unknown declspec has parameters. |
| 420 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 421 | if (!T.consumeOpen()) |
| 422 | T.skipToEnd(); |
| 423 | } |
| 424 | } |
| 425 | |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 426 | /// [MS] decl-specifier: |
| 427 | /// __declspec ( extended-decl-modifier-seq ) |
| 428 | /// |
| 429 | /// [MS] extended-decl-modifier-seq: |
| 430 | /// extended-decl-modifier[opt] |
| 431 | /// extended-decl-modifier extended-decl-modifier-seq |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 432 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) { |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 433 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 434 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 435 | ConsumeToken(); |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 436 | BalancedDelimiterTracker T(*this, tok::l_paren); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 437 | if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec", |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 438 | tok::r_paren)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 439 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 440 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 441 | // An empty declspec is perfectly legal and should not warn. Additionally, |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 442 | // you can specify multiple attributes per declspec. |
| 443 | while (Tok.getKind() != tok::r_paren) { |
| 444 | // We expect either a well-known identifier or a generic string. Anything |
| 445 | // else is a malformed declspec. |
| 446 | bool IsString = Tok.getKind() == tok::string_literal ? true : false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 447 | if (!IsString && Tok.getKind() != tok::identifier && |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 448 | Tok.getKind() != tok::kw_restrict) { |
| 449 | Diag(Tok, diag::err_ms_declspec_type); |
| 450 | T.skipToEnd(); |
| 451 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 452 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 453 | |
| 454 | IdentifierInfo *AttrName; |
| 455 | SourceLocation AttrNameLoc; |
| 456 | if (IsString) { |
| 457 | SmallString<8> StrBuffer; |
| 458 | bool Invalid = false; |
| 459 | StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid); |
| 460 | if (Invalid) { |
| 461 | T.skipToEnd(); |
| 462 | return; |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 463 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 464 | AttrName = PP.getIdentifierInfo(Str); |
| 465 | AttrNameLoc = ConsumeStringToken(); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 466 | } else { |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 467 | AttrName = Tok.getIdentifierInfo(); |
| 468 | AttrNameLoc = ConsumeToken(); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 469 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 470 | |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 471 | if (IsString || IsSimpleMicrosoftDeclSpec(AttrName)) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 472 | // If we have a generic string, we will allow it because there is no |
| 473 | // documented list of allowable string declspecs, but we know they exist |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 474 | // (for instance, SAL declspecs in older versions of MSVC). |
| 475 | // |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 476 | // Alternatively, if the identifier is a simple one, then it requires no |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 477 | // arguments and can be turned into an attribute directly. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 478 | Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 479 | 0, 0, AttributeList::AS_Declspec); |
| 480 | else |
| 481 | ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs); |
Jakob Stoklund Olesen | 3532936 | 2012-06-19 21:48:43 +0000 | [diff] [blame] | 482 | } |
Aaron Ballman | fc685ac | 2012-06-19 22:09:27 +0000 | [diff] [blame] | 483 | T.consumeClose(); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 484 | } |
| 485 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 486 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 487 | // Treat these like attributes |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 488 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 489 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 490 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
Chad Rosier | ccbb402 | 2012-12-21 21:27:13 +0000 | [diff] [blame] | 491 | Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 492 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 493 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 494 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 495 | SourceLocation(), 0, 0, AttributeList::AS_Keyword); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 496 | } |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 497 | } |
| 498 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 499 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 500 | // Treat these like attributes |
| 501 | while (Tok.is(tok::kw___pascal)) { |
| 502 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 503 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 504 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 505 | SourceLocation(), 0, 0, AttributeList::AS_Keyword); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 506 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 509 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 510 | // Treat these like attributes |
| 511 | while (Tok.is(tok::kw___kernel)) { |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 512 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 513 | SourceLocation AttrNameLoc = ConsumeToken(); |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 514 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 515 | SourceLocation(), 0, 0, AttributeList::AS_Keyword); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 519 | void Parser::ParseOpenCLQualifiers(DeclSpec &DS) { |
Richard Smith | 5cd532c | 2013-01-29 01:24:26 +0000 | [diff] [blame] | 520 | // FIXME: The mapping from attribute spelling to semantics should be |
| 521 | // performed in Sema, not here. |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 522 | SourceLocation Loc = Tok.getLocation(); |
| 523 | switch(Tok.getKind()) { |
| 524 | // OpenCL qualifiers: |
| 525 | case tok::kw___private: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 526 | case tok::kw_private: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 527 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 528 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 529 | PP.getIdentifierInfo("address_space"), Loc, 0); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 530 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 531 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 532 | case tok::kw___global: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 533 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 534 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 535 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 536 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 537 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 538 | case tok::kw___local: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 539 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 540 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 541 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 542 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 543 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 544 | case tok::kw___constant: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 545 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 546 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 547 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 548 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 549 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 550 | case tok::kw___read_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 551 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 552 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 553 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 554 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 555 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 556 | case tok::kw___write_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 557 | DS.getAttributes().addNewInteger( |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 558 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 559 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 560 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 561 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 562 | case tok::kw___read_write: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 563 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 564 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 565 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 566 | break; |
| 567 | default: break; |
| 568 | } |
| 569 | } |
| 570 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 571 | /// \brief Parse a version number. |
| 572 | /// |
| 573 | /// version: |
| 574 | /// simple-integer |
| 575 | /// simple-integer ',' simple-integer |
| 576 | /// simple-integer ',' simple-integer ',' simple-integer |
| 577 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 578 | Range = Tok.getLocation(); |
| 579 | |
| 580 | if (!Tok.is(tok::numeric_constant)) { |
| 581 | Diag(Tok, diag::err_expected_version); |
| 582 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 583 | return VersionTuple(); |
| 584 | } |
| 585 | |
| 586 | // Parse the major (and possibly minor and subminor) versions, which |
| 587 | // are stored in the numeric constant. We utilize a quirk of the |
| 588 | // lexer, which is that it handles something like 1.2.3 as a single |
| 589 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 590 | SmallString<512> Buffer; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 591 | Buffer.resize(Tok.getLength()+1); |
| 592 | const char *ThisTokBegin = &Buffer[0]; |
| 593 | |
| 594 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 595 | bool Invalid = false; |
| 596 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 597 | if (Invalid) |
| 598 | return VersionTuple(); |
| 599 | |
| 600 | // Parse the major version. |
| 601 | unsigned AfterMajor = 0; |
| 602 | unsigned Major = 0; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 603 | while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 604 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 605 | ++AfterMajor; |
| 606 | } |
| 607 | |
| 608 | if (AfterMajor == 0) { |
| 609 | Diag(Tok, diag::err_expected_version); |
| 610 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 611 | return VersionTuple(); |
| 612 | } |
| 613 | |
| 614 | if (AfterMajor == ActualLength) { |
| 615 | ConsumeToken(); |
| 616 | |
| 617 | // We only had a single version component. |
| 618 | if (Major == 0) { |
| 619 | Diag(Tok, diag::err_zero_version); |
| 620 | return VersionTuple(); |
| 621 | } |
| 622 | |
| 623 | return VersionTuple(Major); |
| 624 | } |
| 625 | |
| 626 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 627 | Diag(Tok, diag::err_expected_version); |
| 628 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 629 | return VersionTuple(); |
| 630 | } |
| 631 | |
| 632 | // Parse the minor version. |
| 633 | unsigned AfterMinor = AfterMajor + 1; |
| 634 | unsigned Minor = 0; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 635 | while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 636 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 637 | ++AfterMinor; |
| 638 | } |
| 639 | |
| 640 | if (AfterMinor == ActualLength) { |
| 641 | ConsumeToken(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 642 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 643 | // We had major.minor. |
| 644 | if (Major == 0 && Minor == 0) { |
| 645 | Diag(Tok, diag::err_zero_version); |
| 646 | return VersionTuple(); |
| 647 | } |
| 648 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 649 | return VersionTuple(Major, Minor); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 650 | } |
| 651 | |
| 652 | // If what follows is not a '.', we have a problem. |
| 653 | if (ThisTokBegin[AfterMinor] != '.') { |
| 654 | Diag(Tok, diag::err_expected_version); |
| 655 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 656 | return VersionTuple(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | // Parse the subminor version. |
| 660 | unsigned AfterSubminor = AfterMinor + 1; |
| 661 | unsigned Subminor = 0; |
Jordan Rose | 3f6f51e | 2013-02-08 22:30:41 +0000 | [diff] [blame] | 662 | while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 663 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 664 | ++AfterSubminor; |
| 665 | } |
| 666 | |
| 667 | if (AfterSubminor != ActualLength) { |
| 668 | Diag(Tok, diag::err_expected_version); |
| 669 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 670 | return VersionTuple(); |
| 671 | } |
| 672 | ConsumeToken(); |
| 673 | return VersionTuple(Major, Minor, Subminor); |
| 674 | } |
| 675 | |
| 676 | /// \brief Parse the contents of the "availability" attribute. |
| 677 | /// |
| 678 | /// availability-attribute: |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 679 | /// 'availability' '(' platform ',' version-arg-list, opt-message')' |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 680 | /// |
| 681 | /// platform: |
| 682 | /// identifier |
| 683 | /// |
| 684 | /// version-arg-list: |
| 685 | /// version-arg |
| 686 | /// version-arg ',' version-arg-list |
| 687 | /// |
| 688 | /// version-arg: |
| 689 | /// 'introduced' '=' version |
| 690 | /// 'deprecated' '=' version |
Douglas Gregor | 93a7067 | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 691 | /// 'obsoleted' = version |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 692 | /// 'unavailable' |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 693 | /// opt-message: |
| 694 | /// 'message' '=' <string> |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 695 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 696 | SourceLocation AvailabilityLoc, |
| 697 | ParsedAttributes &attrs, |
| 698 | SourceLocation *endLoc) { |
| 699 | SourceLocation PlatformLoc; |
| 700 | IdentifierInfo *Platform = 0; |
| 701 | |
| 702 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 703 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 704 | ExprResult MessageExpr; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 705 | |
| 706 | // Opening '('. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 707 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 708 | if (T.consumeOpen()) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 709 | Diag(Tok, diag::err_expected_lparen); |
| 710 | return; |
| 711 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 712 | |
| 713 | // Parse the platform name, |
| 714 | if (Tok.isNot(tok::identifier)) { |
| 715 | Diag(Tok, diag::err_availability_expected_platform); |
| 716 | SkipUntil(tok::r_paren); |
| 717 | return; |
| 718 | } |
| 719 | Platform = Tok.getIdentifierInfo(); |
| 720 | PlatformLoc = ConsumeToken(); |
| 721 | |
| 722 | // Parse the ',' following the platform name. |
| 723 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren)) |
| 724 | return; |
| 725 | |
| 726 | // If we haven't grabbed the pointers for the identifiers |
| 727 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 728 | if (!Ident_introduced) { |
| 729 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 730 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 731 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 732 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 733 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 737 | SourceLocation UnavailableLoc; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 738 | do { |
| 739 | if (Tok.isNot(tok::identifier)) { |
| 740 | Diag(Tok, diag::err_availability_expected_change); |
| 741 | SkipUntil(tok::r_paren); |
| 742 | return; |
| 743 | } |
| 744 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 745 | SourceLocation KeywordLoc = ConsumeToken(); |
| 746 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 747 | if (Keyword == Ident_unavailable) { |
| 748 | if (UnavailableLoc.isValid()) { |
| 749 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 750 | << Keyword << SourceRange(UnavailableLoc); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 751 | } |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 752 | UnavailableLoc = KeywordLoc; |
| 753 | |
| 754 | if (Tok.isNot(tok::comma)) |
| 755 | break; |
| 756 | |
| 757 | ConsumeToken(); |
| 758 | continue; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 761 | if (Tok.isNot(tok::equal)) { |
| 762 | Diag(Tok, diag::err_expected_equal_after) |
| 763 | << Keyword; |
| 764 | SkipUntil(tok::r_paren); |
| 765 | return; |
| 766 | } |
| 767 | ConsumeToken(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 768 | if (Keyword == Ident_message) { |
| 769 | if (!isTokenStringLiteral()) { |
Andy Gibbs | 97f8461 | 2012-11-17 19:16:52 +0000 | [diff] [blame] | 770 | Diag(Tok, diag::err_expected_string_literal) |
| 771 | << /*Source='availability attribute'*/2; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 772 | SkipUntil(tok::r_paren); |
| 773 | return; |
| 774 | } |
| 775 | MessageExpr = ParseStringLiteralExpression(); |
| 776 | break; |
| 777 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 778 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 779 | SourceRange VersionRange; |
| 780 | VersionTuple Version = ParseVersionTuple(VersionRange); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 781 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 782 | if (Version.empty()) { |
| 783 | SkipUntil(tok::r_paren); |
| 784 | return; |
| 785 | } |
| 786 | |
| 787 | unsigned Index; |
| 788 | if (Keyword == Ident_introduced) |
| 789 | Index = Introduced; |
| 790 | else if (Keyword == Ident_deprecated) |
| 791 | Index = Deprecated; |
| 792 | else if (Keyword == Ident_obsoleted) |
| 793 | Index = Obsoleted; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 794 | else |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 795 | Index = Unknown; |
| 796 | |
| 797 | if (Index < Unknown) { |
| 798 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 799 | Diag(KeywordLoc, diag::err_availability_redundant) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 800 | << Keyword |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 801 | << SourceRange(Changes[Index].KeywordLoc, |
| 802 | Changes[Index].VersionRange.getEnd()); |
| 803 | } |
| 804 | |
| 805 | Changes[Index].KeywordLoc = KeywordLoc; |
| 806 | Changes[Index].Version = Version; |
| 807 | Changes[Index].VersionRange = VersionRange; |
| 808 | } else { |
| 809 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 810 | << Keyword << VersionRange; |
| 811 | } |
| 812 | |
| 813 | if (Tok.isNot(tok::comma)) |
| 814 | break; |
| 815 | |
| 816 | ConsumeToken(); |
| 817 | } while (true); |
| 818 | |
| 819 | // Closing ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 820 | if (T.consumeClose()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 821 | return; |
| 822 | |
| 823 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 824 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 825 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 826 | // The 'unavailable' availability cannot be combined with any other |
| 827 | // availability changes. Make sure that hasn't happened. |
| 828 | if (UnavailableLoc.isValid()) { |
| 829 | bool Complained = false; |
| 830 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 831 | if (Changes[Index].KeywordLoc.isValid()) { |
| 832 | if (!Complained) { |
| 833 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 834 | << SourceRange(Changes[Index].KeywordLoc, |
| 835 | Changes[Index].VersionRange.getEnd()); |
| 836 | Complained = true; |
| 837 | } |
| 838 | |
| 839 | // Clear out the availability. |
| 840 | Changes[Index] = AvailabilityChange(); |
| 841 | } |
| 842 | } |
| 843 | } |
| 844 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 845 | // Record this attribute |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 846 | attrs.addNew(&Availability, |
| 847 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Fariborz Jahanian | f96708d | 2012-01-23 23:38:32 +0000 | [diff] [blame] | 848 | 0, AvailabilityLoc, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 849 | Platform, PlatformLoc, |
| 850 | Changes[Introduced], |
| 851 | Changes[Deprecated], |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 852 | Changes[Obsoleted], |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 853 | UnavailableLoc, MessageExpr.take(), |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 854 | AttributeList::AS_GNU); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 855 | } |
| 856 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 857 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 858 | // Late Parsed Attributes: |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 859 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 860 | |
| 861 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 862 | |
| 863 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 864 | Self->ParseLexedAttributes(*Class); |
| 865 | } |
| 866 | |
| 867 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 868 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 872 | /// scope appropriately. |
| 873 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 874 | // Deal with templates |
| 875 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 876 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 877 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 878 | HasTemplateScope); |
| 879 | if (HasTemplateScope) |
| 880 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 881 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 882 | // Set or update the scope flags. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 883 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 884 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 885 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 886 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 887 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 888 | // Enter the scope of nested classes |
| 889 | if (!AlreadyHasClassScope) |
| 890 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 891 | Class.TagOrTemplate); |
Benjamin Kramer | 268efba | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 892 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 893 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 894 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 895 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 896 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 897 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 898 | if (!AlreadyHasClassScope) |
| 899 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 900 | Class.TagOrTemplate); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 901 | } |
| 902 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 903 | |
| 904 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 905 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 906 | bool EnterScope, bool OnDefinition) { |
DeLesley Hutchins | 161db02 | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 907 | assert(LAs.parseSoon() && |
| 908 | "Attribute list should be marked for immediate parsing."); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 909 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 95526a4 | 2012-08-15 22:41:04 +0000 | [diff] [blame] | 910 | if (D) |
| 911 | LAs[i]->addDecl(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 912 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | d306cf7 | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 913 | delete LAs[i]; |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 914 | } |
| 915 | LAs.clear(); |
| 916 | } |
| 917 | |
| 918 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 919 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 920 | /// This will be called at the end of parsing a class declaration |
| 921 | /// for each LateParsedAttribute. We consume the saved tokens and |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 922 | /// create an attribute with the arguments filled in. We add this |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 923 | /// to the Attribute list for the decl. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 924 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 925 | bool EnterScope, bool OnDefinition) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 926 | // Save the current token position. |
| 927 | SourceLocation OrigLoc = Tok.getLocation(); |
| 928 | |
| 929 | // Append the current token at the end of the new token stream so that it |
| 930 | // doesn't get lost. |
| 931 | LA.Toks.push_back(Tok); |
| 932 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 933 | // Consume the previously pushed token. |
Argyrios Kyrtzidis | ab2d09b | 2013-03-27 23:58:17 +0000 | [diff] [blame^] | 934 | ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 935 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 936 | if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) { |
Richard Smith | cd8ab51 | 2013-01-17 01:30:42 +0000 | [diff] [blame] | 937 | // FIXME: Do not warn on C++11 attributes, once we start supporting |
| 938 | // them here. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 939 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 940 | << LA.AttrName.getName(); |
| 941 | } |
| 942 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 943 | ParsedAttributes Attrs(AttrFactory); |
| 944 | SourceLocation endLoc; |
| 945 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 946 | if (LA.Decls.size() > 0) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 947 | Decl *D = LA.Decls[0]; |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 948 | NamedDecl *ND = dyn_cast<NamedDecl>(D); |
| 949 | RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext()); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 950 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 951 | // Allow 'this' within late-parsed attributes. |
| 952 | Sema::CXXThisScopeRAII ThisScope(Actions, RD, |
| 953 | /*TypeQuals=*/0, |
| 954 | ND && RD && ND->isCXXInstanceMember()); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 955 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 956 | if (LA.Decls.size() == 1) { |
| 957 | // If the Decl is templatized, add template parameters to scope. |
| 958 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 959 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 960 | if (HasTemplateScope) |
| 961 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 962 | |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 963 | // If the Decl is on a function, add function parameters to the scope. |
| 964 | bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
| 965 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope); |
| 966 | if (HasFunScope) |
| 967 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 968 | |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 969 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 970 | 0, SourceLocation(), AttributeList::AS_GNU); |
DeLesley Hutchins | d30fb9e | 2012-08-20 21:32:18 +0000 | [diff] [blame] | 971 | |
| 972 | if (HasFunScope) { |
| 973 | Actions.ActOnExitFunctionContext(); |
| 974 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 975 | } |
| 976 | if (HasTemplateScope) { |
| 977 | TempScope.Exit(); |
| 978 | } |
| 979 | } else { |
| 980 | // If there are multiple decls, then the decl cannot be within the |
| 981 | // function scope. |
Michael Han | 6880f49 | 2012-10-03 01:56:22 +0000 | [diff] [blame] | 982 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc, |
Michael Han | 45bed13 | 2012-10-04 16:42:52 +0000 | [diff] [blame] | 983 | 0, SourceLocation(), AttributeList::AS_GNU); |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 984 | } |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 985 | } else { |
| 986 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 987 | } |
| 988 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 989 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) { |
| 990 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
| 991 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 992 | |
| 993 | if (Tok.getLocation() != OrigLoc) { |
| 994 | // Due to a parsing error, we either went over the cached tokens or |
| 995 | // there are still cached tokens left, so we skip the leftover tokens. |
| 996 | // Since this is an uncommon situation that should be avoided, use the |
| 997 | // expensive isBeforeInTranslationUnit call. |
| 998 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 999 | OrigLoc)) |
| 1000 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
Douglas Gregor | d78ef5b | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 1001 | ConsumeAnyToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1002 | } |
| 1003 | } |
| 1004 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1005 | /// \brief Wrapper around a case statement checking if AttrName is |
| 1006 | /// one of the thread safety attributes |
Dmitri Gribenko | cfa88f8 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 1007 | bool Parser::IsThreadSafetyAttribute(StringRef AttrName) { |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1008 | return llvm::StringSwitch<bool>(AttrName) |
| 1009 | .Case("guarded_by", true) |
| 1010 | .Case("guarded_var", true) |
| 1011 | .Case("pt_guarded_by", true) |
| 1012 | .Case("pt_guarded_var", true) |
| 1013 | .Case("lockable", true) |
| 1014 | .Case("scoped_lockable", true) |
| 1015 | .Case("no_thread_safety_analysis", true) |
| 1016 | .Case("acquired_after", true) |
| 1017 | .Case("acquired_before", true) |
| 1018 | .Case("exclusive_lock_function", true) |
| 1019 | .Case("shared_lock_function", true) |
| 1020 | .Case("exclusive_trylock_function", true) |
| 1021 | .Case("shared_trylock_function", true) |
| 1022 | .Case("unlock_function", true) |
| 1023 | .Case("lock_returned", true) |
| 1024 | .Case("locks_excluded", true) |
| 1025 | .Case("exclusive_locks_required", true) |
| 1026 | .Case("shared_locks_required", true) |
| 1027 | .Default(false); |
| 1028 | } |
| 1029 | |
| 1030 | /// \brief Parse the contents of thread safety attributes. These |
| 1031 | /// should always be parsed as an expression list. |
| 1032 | /// |
| 1033 | /// We need to special case the parsing due to the fact that if the first token |
| 1034 | /// of the first argument is an identifier, the main parse loop will store |
| 1035 | /// that token as a "parameter" and the rest of |
| 1036 | /// the arguments will be added to a list of "arguments". However, |
| 1037 | /// subsequent tokens in the first argument are lost. We instead parse each |
| 1038 | /// argument as an expression and add all arguments to the list of "arguments". |
| 1039 | /// In future, we will take advantage of this special case to also |
| 1040 | /// deal with some argument scoping issues here (for example, referring to a |
| 1041 | /// function parameter in the attribute on that function). |
| 1042 | void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName, |
| 1043 | SourceLocation AttrNameLoc, |
| 1044 | ParsedAttributes &Attrs, |
| 1045 | SourceLocation *EndLoc) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1046 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1047 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1048 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1049 | T.consumeOpen(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1050 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1051 | ExprVector ArgExprs; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1052 | bool ArgExprsOk = true; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1053 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1054 | // now parse the list of expressions |
DeLesley Hutchins | 4805f15 | 2011-12-14 19:36:06 +0000 | [diff] [blame] | 1055 | while (Tok.isNot(tok::r_paren)) { |
DeLesley Hutchins | ed4330b | 2013-02-07 19:01:07 +0000 | [diff] [blame] | 1056 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1057 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 1058 | if (ArgExpr.isInvalid()) { |
| 1059 | ArgExprsOk = false; |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1060 | T.consumeClose(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1061 | break; |
| 1062 | } else { |
| 1063 | ArgExprs.push_back(ArgExpr.release()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1064 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1065 | if (Tok.isNot(tok::comma)) |
| 1066 | break; |
| 1067 | ConsumeToken(); // Eat the comma, move to the next argument |
| 1068 | } |
| 1069 | // Match the ')'. |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 1070 | if (ArgExprsOk && !T.consumeClose()) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 1071 | Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1072 | ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1073 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1074 | if (EndLoc) |
| 1075 | *EndLoc = T.getCloseLocation(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
Dmitri Gribenko | 0d5a069 | 2012-08-17 00:08:38 +0000 | [diff] [blame] | 1078 | void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName, |
| 1079 | SourceLocation AttrNameLoc, |
| 1080 | ParsedAttributes &Attrs, |
| 1081 | SourceLocation *EndLoc) { |
| 1082 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 1083 | |
| 1084 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1085 | T.consumeOpen(); |
| 1086 | |
| 1087 | if (Tok.isNot(tok::identifier)) { |
| 1088 | Diag(Tok, diag::err_expected_ident); |
| 1089 | T.skipToEnd(); |
| 1090 | return; |
| 1091 | } |
| 1092 | IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo(); |
| 1093 | SourceLocation ArgumentKindLoc = ConsumeToken(); |
| 1094 | |
| 1095 | if (Tok.isNot(tok::comma)) { |
| 1096 | Diag(Tok, diag::err_expected_comma); |
| 1097 | T.skipToEnd(); |
| 1098 | return; |
| 1099 | } |
| 1100 | ConsumeToken(); |
| 1101 | |
| 1102 | SourceRange MatchingCTypeRange; |
| 1103 | TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange); |
| 1104 | if (MatchingCType.isInvalid()) { |
| 1105 | T.skipToEnd(); |
| 1106 | return; |
| 1107 | } |
| 1108 | |
| 1109 | bool LayoutCompatible = false; |
| 1110 | bool MustBeNull = false; |
| 1111 | while (Tok.is(tok::comma)) { |
| 1112 | ConsumeToken(); |
| 1113 | if (Tok.isNot(tok::identifier)) { |
| 1114 | Diag(Tok, diag::err_expected_ident); |
| 1115 | T.skipToEnd(); |
| 1116 | return; |
| 1117 | } |
| 1118 | IdentifierInfo *Flag = Tok.getIdentifierInfo(); |
| 1119 | if (Flag->isStr("layout_compatible")) |
| 1120 | LayoutCompatible = true; |
| 1121 | else if (Flag->isStr("must_be_null")) |
| 1122 | MustBeNull = true; |
| 1123 | else { |
| 1124 | Diag(Tok, diag::err_type_safety_unknown_flag) << Flag; |
| 1125 | T.skipToEnd(); |
| 1126 | return; |
| 1127 | } |
| 1128 | ConsumeToken(); // consume flag |
| 1129 | } |
| 1130 | |
| 1131 | if (!T.consumeClose()) { |
| 1132 | Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 1133 | ArgumentKind, ArgumentKindLoc, |
| 1134 | MatchingCType.release(), LayoutCompatible, |
| 1135 | MustBeNull, AttributeList::AS_GNU); |
| 1136 | } |
| 1137 | |
| 1138 | if (EndLoc) |
| 1139 | *EndLoc = T.getCloseLocation(); |
| 1140 | } |
| 1141 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1142 | /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets |
| 1143 | /// of a C++11 attribute-specifier in a location where an attribute is not |
| 1144 | /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this |
| 1145 | /// situation. |
| 1146 | /// |
| 1147 | /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if |
| 1148 | /// this doesn't appear to actually be an attribute-specifier, and the caller |
| 1149 | /// should try to parse it. |
| 1150 | bool Parser::DiagnoseProhibitedCXX11Attribute() { |
| 1151 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)); |
| 1152 | |
| 1153 | switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) { |
| 1154 | case CAK_NotAttributeSpecifier: |
| 1155 | // No diagnostic: we're in Obj-C++11 and this is not actually an attribute. |
| 1156 | return false; |
| 1157 | |
| 1158 | case CAK_InvalidAttributeSpecifier: |
| 1159 | Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute); |
| 1160 | return false; |
| 1161 | |
| 1162 | case CAK_AttributeSpecifier: |
| 1163 | // Parse and discard the attributes. |
| 1164 | SourceLocation BeginLoc = ConsumeBracket(); |
| 1165 | ConsumeBracket(); |
| 1166 | SkipUntil(tok::r_square, /*StopAtSemi*/ false); |
| 1167 | assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied"); |
| 1168 | SourceLocation EndLoc = ConsumeBracket(); |
| 1169 | Diag(BeginLoc, diag::err_attributes_not_allowed) |
| 1170 | << SourceRange(BeginLoc, EndLoc); |
| 1171 | return true; |
| 1172 | } |
Chandler Carruth | 2c6dbd7 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 1173 | llvm_unreachable("All cases handled above."); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Richard Smith | 975d52c | 2013-02-20 01:17:14 +0000 | [diff] [blame] | 1176 | /// \brief We have found the opening square brackets of a C++11 |
| 1177 | /// attribute-specifier in a location where an attribute is not permitted, but |
| 1178 | /// we know where the attributes ought to be written. Parse them anyway, and |
| 1179 | /// provide a fixit moving them to the right place. |
Richard Smith | 0532140 | 2013-02-19 23:47:15 +0000 | [diff] [blame] | 1180 | void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs, |
| 1181 | SourceLocation CorrectLocation) { |
| 1182 | assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) || |
| 1183 | Tok.is(tok::kw_alignas)); |
| 1184 | |
| 1185 | // Consume the attributes. |
| 1186 | SourceLocation Loc = Tok.getLocation(); |
| 1187 | ParseCXX11Attributes(Attrs); |
| 1188 | CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true); |
| 1189 | |
| 1190 | Diag(Loc, diag::err_attributes_not_allowed) |
| 1191 | << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange) |
| 1192 | << FixItHint::CreateRemoval(AttrRange); |
| 1193 | } |
| 1194 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1195 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 1196 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 1197 | << attrs.Range; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 1198 | } |
| 1199 | |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1200 | void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) { |
| 1201 | AttributeList *AttrList = attrs.getList(); |
| 1202 | while (AttrList) { |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1203 | if (AttrList->isCXX11Attribute()) { |
Richard Smith | d03de6a | 2013-01-29 10:02:16 +0000 | [diff] [blame] | 1204 | Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr) |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 1205 | << AttrList->getName(); |
| 1206 | AttrList->setInvalid(); |
| 1207 | } |
| 1208 | AttrList = AttrList->getNext(); |
| 1209 | } |
| 1210 | } |
| 1211 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1212 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 1213 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1214 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 1215 | /// location of the semicolon in DeclEnd. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1216 | /// |
| 1217 | /// declaration: [C99 6.7] |
| 1218 | /// block-declaration -> |
| 1219 | /// simple-declaration |
| 1220 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1221 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1222 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1223 | /// [C++] using-directive |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 1224 | /// [C++] using-declaration |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 1225 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1226 | /// others... [FIXME] |
| 1227 | /// |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1228 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 1229 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1230 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1231 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 1232 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | e8cff36 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 1233 | // Must temporarily exit the objective-c container scope for |
| 1234 | // parsing c none objective-c decls. |
| 1235 | ObjCDeclContextSwitch ObjCDC(*this); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1236 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1237 | Decl *SingleDecl = 0; |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1238 | Decl *OwnedType = 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1239 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 1240 | case tok::kw_template: |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1241 | case tok::kw_export: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1242 | ProhibitAttributes(attrs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1243 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1244 | break; |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1245 | case tok::kw_inline: |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 1246 | // 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] | 1247 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1248 | ProhibitAttributes(attrs); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 1249 | SourceLocation InlineLoc = ConsumeToken(); |
| 1250 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 1251 | break; |
| 1252 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1253 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1254 | true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1255 | case tok::kw_namespace: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1256 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1257 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1258 | break; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1259 | case tok::kw_using: |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1260 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1261 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1262 | break; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1263 | case tok::kw_static_assert: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1264 | case tok::kw__Static_assert: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1265 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1266 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1267 | break; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1268 | default: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1269 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1270 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1271 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1272 | // 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] | 1273 | // single decl, convert it now. Alias declarations can also declare a type; |
| 1274 | // include that too if it is present. |
| 1275 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1276 | } |
| 1277 | |
| 1278 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1279 | /// declaration-specifiers init-declarator-list[opt] ';' |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1280 | /// [C++11] attribute-specifier-seq decl-specifier-seq[opt] |
| 1281 | /// init-declarator-list ';' |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1282 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 1283 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1284 | /// |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1285 | /// for-range-declaration: [C++11 6.5p1: stmt.ranged] |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1286 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1287 | /// |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1288 | /// 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] | 1289 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1290 | /// |
| 1291 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 1292 | /// of a simple-declaration. If we find that we are, we also parse the |
| 1293 | /// for-range-initializer, and place it here. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1294 | Parser::DeclGroupPtrTy |
| 1295 | Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context, |
| 1296 | SourceLocation &DeclEnd, |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1297 | ParsedAttributesWithRange &Attrs, |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 1298 | bool RequireSemi, ForRangeInit *FRI) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1299 | // Parse the common declaration-specifiers piece. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1300 | ParsingDeclSpec DS(*this); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1301 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1302 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1303 | getDeclSpecContextFromDeclaratorContext(Context)); |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1304 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1305 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1306 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1307 | if (Tok.is(tok::semi)) { |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1308 | ProhibitAttributes(Attrs); |
Argyrios Kyrtzidis | 5641b0d | 2012-05-16 23:49:15 +0000 | [diff] [blame] | 1309 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1310 | if (RequireSemi) ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1311 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1312 | DS); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1313 | DS.complete(TheDecl); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1314 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1315 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1316 | |
Richard Smith | 68ea3ae | 2013-02-22 09:06:26 +0000 | [diff] [blame] | 1317 | DS.takeAttributesFrom(Attrs); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1318 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1319 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1320 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1321 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1322 | /// for a declarator. |
| 1323 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1324 | switch (Tok.getKind()) { |
| 1325 | case tok::annot_cxxscope: |
| 1326 | case tok::annot_template_id: |
| 1327 | case tok::caret: |
| 1328 | case tok::code_completion: |
| 1329 | case tok::coloncolon: |
| 1330 | case tok::ellipsis: |
| 1331 | case tok::kw___attribute: |
| 1332 | case tok::kw_operator: |
| 1333 | case tok::l_paren: |
| 1334 | case tok::star: |
| 1335 | return true; |
| 1336 | |
| 1337 | case tok::amp: |
| 1338 | case tok::ampamp: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1339 | return getLangOpts().CPlusPlus; |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1340 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1341 | 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] | 1342 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 && |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1343 | NextToken().is(tok::l_square); |
| 1344 | |
| 1345 | 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] | 1346 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1347 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1348 | case tok::identifier: |
| 1349 | switch (NextToken().getKind()) { |
| 1350 | case tok::code_completion: |
| 1351 | case tok::coloncolon: |
| 1352 | case tok::comma: |
| 1353 | case tok::equal: |
| 1354 | case tok::equalequal: // Might be a typo for '='. |
| 1355 | case tok::kw_alignas: |
| 1356 | case tok::kw_asm: |
| 1357 | case tok::kw___attribute: |
| 1358 | case tok::l_brace: |
| 1359 | case tok::l_paren: |
| 1360 | case tok::l_square: |
| 1361 | case tok::less: |
| 1362 | case tok::r_brace: |
| 1363 | case tok::r_paren: |
| 1364 | case tok::r_square: |
| 1365 | case tok::semi: |
| 1366 | return true; |
| 1367 | |
| 1368 | case tok::colon: |
| 1369 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1370 | // and in block scope it's probably a label. Inside a class definition, |
| 1371 | // this is a bit-field. |
| 1372 | return Context == Declarator::MemberContext || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1373 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1374 | |
| 1375 | case tok::identifier: // Possible virt-specifier. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 1376 | return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken()); |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1377 | |
| 1378 | default: |
| 1379 | return false; |
| 1380 | } |
| 1381 | |
| 1382 | default: |
| 1383 | return false; |
| 1384 | } |
| 1385 | } |
| 1386 | |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1387 | /// Skip until we reach something which seems like a sensible place to pick |
| 1388 | /// up parsing after a malformed declaration. This will sometimes stop sooner |
| 1389 | /// than SkipUntil(tok::r_brace) would, but will never stop later. |
| 1390 | void Parser::SkipMalformedDecl() { |
| 1391 | while (true) { |
| 1392 | switch (Tok.getKind()) { |
| 1393 | case tok::l_brace: |
| 1394 | // Skip until matching }, then stop. We've probably skipped over |
| 1395 | // a malformed class or function definition or similar. |
| 1396 | ConsumeBrace(); |
| 1397 | SkipUntil(tok::r_brace, /*StopAtSemi*/false); |
| 1398 | if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) { |
| 1399 | // This declaration isn't over yet. Keep skipping. |
| 1400 | continue; |
| 1401 | } |
| 1402 | if (Tok.is(tok::semi)) |
| 1403 | ConsumeToken(); |
| 1404 | return; |
| 1405 | |
| 1406 | case tok::l_square: |
| 1407 | ConsumeBracket(); |
| 1408 | SkipUntil(tok::r_square, /*StopAtSemi*/false); |
| 1409 | continue; |
| 1410 | |
| 1411 | case tok::l_paren: |
| 1412 | ConsumeParen(); |
| 1413 | SkipUntil(tok::r_paren, /*StopAtSemi*/false); |
| 1414 | continue; |
| 1415 | |
| 1416 | case tok::r_brace: |
| 1417 | return; |
| 1418 | |
| 1419 | case tok::semi: |
| 1420 | ConsumeToken(); |
| 1421 | return; |
| 1422 | |
| 1423 | case tok::kw_inline: |
| 1424 | // 'inline namespace' at the start of a line is almost certainly |
Jordan Rose | 94f29f4 | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1425 | // a good place to pick back up parsing, except in an Objective-C |
| 1426 | // @interface context. |
| 1427 | if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) && |
| 1428 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1429 | return; |
| 1430 | break; |
| 1431 | |
| 1432 | case tok::kw_namespace: |
| 1433 | // 'namespace' at the start of a line is almost certainly a good |
Jordan Rose | 94f29f4 | 2012-07-09 16:54:53 +0000 | [diff] [blame] | 1434 | // place to pick back up parsing, except in an Objective-C |
| 1435 | // @interface context. |
| 1436 | if (Tok.isAtStartOfLine() && |
| 1437 | (!ParsingInObjCContainer || CurParsedObjCImpl)) |
| 1438 | return; |
| 1439 | break; |
| 1440 | |
| 1441 | case tok::at: |
| 1442 | // @end is very much like } in Objective-C contexts. |
| 1443 | if (NextToken().isObjCAtKeyword(tok::objc_end) && |
| 1444 | ParsingInObjCContainer) |
| 1445 | return; |
| 1446 | break; |
| 1447 | |
| 1448 | case tok::minus: |
| 1449 | case tok::plus: |
| 1450 | // - and + probably start new method declarations in Objective-C contexts. |
| 1451 | if (Tok.isAtStartOfLine() && ParsingInObjCContainer) |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1452 | return; |
| 1453 | break; |
| 1454 | |
| 1455 | case tok::eof: |
| 1456 | return; |
| 1457 | |
| 1458 | default: |
| 1459 | break; |
| 1460 | } |
| 1461 | |
| 1462 | ConsumeAnyToken(); |
| 1463 | } |
| 1464 | } |
| 1465 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1466 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1467 | /// definition or a group of object declarations, actually parse the |
| 1468 | /// result. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1469 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1470 | unsigned Context, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1471 | bool AllowFunctionDefinitions, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1472 | SourceLocation *DeclEnd, |
| 1473 | ForRangeInit *FRI) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1474 | // Parse the first declarator. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1475 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1476 | ParseDeclarator(D); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1477 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1478 | // Bail out if the first declarator didn't seem well-formed. |
| 1479 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1480 | SkipMalformedDecl(); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1481 | return DeclGroupPtrTy(); |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1482 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1483 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1484 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1485 | // appropriate function scope after the function Decl has been constructed. |
DeLesley Hutchins | 161db02 | 2012-11-02 21:44:32 +0000 | [diff] [blame] | 1486 | // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList. |
| 1487 | LateParsedAttrList LateParsedAttrs(true); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1488 | if (D.isFunctionDeclarator()) |
| 1489 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1490 | |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1491 | // Check to see if we have a function *definition* which must have a body. |
| 1492 | if (AllowFunctionDefinitions && D.isFunctionDeclarator() && |
| 1493 | // Look at the next token to make sure that this isn't a function |
| 1494 | // declaration. We have to check this because __attribute__ might be the |
| 1495 | // start of a function definition in GCC-extended K&R C. |
Fariborz Jahanian | be1d4ec | 2012-08-10 15:54:40 +0000 | [diff] [blame] | 1496 | !isDeclarationAfterDeclarator()) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1497 | |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1498 | if (isStartOfFunctionDefinition(D)) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1499 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1500 | Diag(Tok, diag::err_function_declared_typedef); |
| 1501 | |
| 1502 | // Recover by treating the 'typedef' as spurious. |
| 1503 | DS.ClearStorageClassSpecs(); |
| 1504 | } |
| 1505 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1506 | Decl *TheDecl = |
| 1507 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1508 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1509 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1510 | |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1511 | if (isDeclarationSpecifier()) { |
| 1512 | // If there is an invalid declaration specifier right after the function |
| 1513 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1514 | // actually a body. Just fall through into the code that handles it as a |
| 1515 | // prototype, and let the top-level code handle the erroneous declspec |
| 1516 | // where it would otherwise expect a comma or semicolon. |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1517 | } else { |
| 1518 | Diag(Tok, diag::err_expected_fn_body); |
| 1519 | SkipUntil(tok::semi); |
| 1520 | return DeclGroupPtrTy(); |
| 1521 | } |
| 1522 | } |
| 1523 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1524 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1525 | return DeclGroupPtrTy(); |
| 1526 | |
| 1527 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1528 | // must parse and analyze the for-range-initializer before the declaration is |
| 1529 | // analyzed. |
| 1530 | if (FRI && Tok.is(tok::colon)) { |
| 1531 | FRI->ColonLoc = ConsumeToken(); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1532 | if (Tok.is(tok::l_brace)) |
| 1533 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1534 | else |
| 1535 | FRI->RangeExpr = ParseExpression(); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1536 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1537 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
| 1538 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | 6895a64 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 1539 | D.complete(ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1540 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1); |
| 1541 | } |
| 1542 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1543 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1544 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1545 | if (LateParsedAttrs.size() > 0) |
| 1546 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1547 | D.complete(FirstDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1548 | if (FirstDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1549 | DeclsInGroup.push_back(FirstDecl); |
| 1550 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1551 | bool ExpectSemi = Context != Declarator::ForContext; |
Fariborz Jahanian | 6c89eaf | 2012-07-02 23:37:09 +0000 | [diff] [blame] | 1552 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1553 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1554 | // error, bail out. |
| 1555 | while (Tok.is(tok::comma)) { |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1556 | SourceLocation CommaLoc = ConsumeToken(); |
| 1557 | |
| 1558 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1559 | // This comma was followed by a line-break and something which can't be |
| 1560 | // the start of a declarator. The comma was probably a typo for a |
| 1561 | // semicolon. |
| 1562 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1563 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1564 | ExpectSemi = false; |
| 1565 | break; |
| 1566 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1567 | |
| 1568 | // Parse the next declarator. |
| 1569 | D.clear(); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1570 | D.setCommaLoc(CommaLoc); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1571 | |
| 1572 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1573 | // declaration, these would be part of the declspec. In subsequent |
| 1574 | // declarators, they become part of the declarator itself, so that they |
| 1575 | // don't apply to declarators after *this* one. Examples: |
| 1576 | // short __attribute__((common)) var; -> declspec |
| 1577 | // short var __attribute__((common)); -> declarator |
| 1578 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1579 | MaybeParseGNUAttributes(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1580 | |
| 1581 | ParseDeclarator(D); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1582 | if (!D.isInvalidType()) { |
| 1583 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1584 | D.complete(ThisDecl); |
| 1585 | if (ThisDecl) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1586 | DeclsInGroup.push_back(ThisDecl); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1587 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1588 | } |
| 1589 | |
| 1590 | if (DeclEnd) |
| 1591 | *DeclEnd = Tok.getLocation(); |
| 1592 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1593 | if (ExpectSemi && |
Chris Lattner | 8bb21d3 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 1594 | ExpectAndConsumeSemi(Context == Declarator::FileContext |
| 1595 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1596 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1597 | // Okay, there was no semicolon and one was expected. If we see a |
| 1598 | // declaration specifier, just assume it was missing and continue parsing. |
| 1599 | // Otherwise things are very confused and we skip to recover. |
| 1600 | if (!isDeclarationSpecifier()) { |
| 1601 | SkipUntil(tok::r_brace, true, true); |
| 1602 | if (Tok.is(tok::semi)) |
| 1603 | ConsumeToken(); |
| 1604 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1607 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1608 | DeclsInGroup.data(), |
| 1609 | DeclsInGroup.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1610 | } |
| 1611 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1612 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1613 | /// declarator. Returns true on an error. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1614 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1615 | // If a simple-asm-expr is present, parse it. |
| 1616 | if (Tok.is(tok::kw_asm)) { |
| 1617 | SourceLocation Loc; |
| 1618 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1619 | if (AsmLabel.isInvalid()) { |
| 1620 | SkipUntil(tok::semi, true, true); |
| 1621 | return true; |
| 1622 | } |
| 1623 | |
| 1624 | D.setAsmLabel(AsmLabel.release()); |
| 1625 | D.SetRangeEnd(Loc); |
| 1626 | } |
| 1627 | |
| 1628 | MaybeParseGNUAttributes(D); |
| 1629 | return false; |
| 1630 | } |
| 1631 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1632 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1633 | /// declarator'. This method parses the remainder of the declaration |
| 1634 | /// (including any attributes or initializer, among other things) and |
| 1635 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1636 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1637 | /// init-declarator: [C99 6.7] |
| 1638 | /// declarator |
| 1639 | /// declarator '=' initializer |
| 1640 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1641 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1642 | /// [C++] declarator initializer[opt] |
| 1643 | /// |
| 1644 | /// [C++] initializer: |
| 1645 | /// [C++] '=' initializer-clause |
| 1646 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1647 | /// [C++0x] '=' 'default' [TODO] |
| 1648 | /// [C++0x] '=' 'delete' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1649 | /// [C++0x] braced-init-list |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1650 | /// |
| 1651 | /// According to the standard grammar, =default and =delete are function |
| 1652 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1653 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1654 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1655 | const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1656 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1657 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1658 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1659 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1660 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1661 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1662 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1663 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1664 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1665 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1666 | switch (TemplateInfo.Kind) { |
| 1667 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1668 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1669 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1670 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1671 | case ParsedTemplateInfo::Template: |
| 1672 | case ParsedTemplateInfo::ExplicitSpecialization: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1673 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1674 | *TemplateInfo.TemplateParams, |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1675 | D); |
| 1676 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1677 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1678 | case ParsedTemplateInfo::ExplicitInstantiation: { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1679 | DeclResult ThisRes |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1680 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1681 | TemplateInfo.ExternLoc, |
| 1682 | TemplateInfo.TemplateLoc, |
| 1683 | D); |
| 1684 | if (ThisRes.isInvalid()) { |
| 1685 | SkipUntil(tok::semi, true, true); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1686 | return 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1687 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1688 | |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1689 | ThisDecl = ThisRes.get(); |
| 1690 | break; |
| 1691 | } |
| 1692 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1693 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1694 | bool TypeContainsAuto = |
| 1695 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
| 1696 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1697 | // Parse declarator '=' initializer. |
Richard Trieu | d6c7c67 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1698 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | fcaf27e | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1699 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1700 | ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1701 | if (Tok.is(tok::kw_delete)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1702 | if (D.isFunctionDeclarator()) |
| 1703 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1704 | << 1 /* delete */; |
| 1705 | else |
| 1706 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Sean Hunt | fe2695e | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1707 | } else if (Tok.is(tok::kw_default)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1708 | if (D.isFunctionDeclarator()) |
Sebastian Redl | ecfcd56 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1709 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1710 | << 0 /* default */; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1711 | else |
| 1712 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1713 | } else { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1714 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1715 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1716 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1717 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1718 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1719 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1720 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Peter Collingbourne | ec98f2f | 2012-07-27 12:56:09 +0000 | [diff] [blame] | 1721 | Actions.FinalizeDeclaration(ThisDecl); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1722 | cutOffParsing(); |
| 1723 | return 0; |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1724 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 1725 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1726 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1727 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1728 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1729 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1730 | ExitScope(); |
| 1731 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1732 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1733 | if (Init.isInvalid()) { |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1734 | SkipUntil(tok::comma, true, true); |
| 1735 | Actions.ActOnInitializerError(ThisDecl); |
| 1736 | } else |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1737 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1738 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1739 | } |
| 1740 | } else if (Tok.is(tok::l_paren)) { |
| 1741 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1742 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1743 | T.consumeOpen(); |
| 1744 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 1745 | ExprVector Exprs; |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1746 | CommaLocsTy CommaLocs; |
| 1747 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1748 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1749 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1750 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1753 | if (ParseExpressionList(Exprs, CommaLocs)) { |
David Blaikie | 3ea19c8 | 2012-10-10 23:15:05 +0000 | [diff] [blame] | 1754 | Actions.ActOnInitializerError(ThisDecl); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1755 | SkipUntil(tok::r_paren); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1756 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1757 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1758 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1759 | ExitScope(); |
| 1760 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1761 | } else { |
| 1762 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1763 | T.consumeClose(); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1764 | |
| 1765 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1766 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1767 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1768 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1769 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1770 | ExitScope(); |
| 1771 | } |
| 1772 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1773 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 1774 | T.getCloseLocation(), |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1775 | Exprs); |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1776 | Actions.AddInitializerToDecl(ThisDecl, Initializer.take(), |
| 1777 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1778 | } |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 1779 | } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) && |
Fariborz Jahanian | b0ed95c | 2012-07-03 23:22:13 +0000 | [diff] [blame] | 1780 | (!CurParsedObjCImpl || !D.isFunctionDeclarator())) { |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1781 | // Parse C++0x braced-init-list. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1782 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1783 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1784 | if (D.getCXXScopeSpec().isSet()) { |
| 1785 | EnterScope(0); |
| 1786 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1787 | } |
| 1788 | |
| 1789 | ExprResult Init(ParseBraceInitializer()); |
| 1790 | |
| 1791 | if (D.getCXXScopeSpec().isSet()) { |
| 1792 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1793 | ExitScope(); |
| 1794 | } |
| 1795 | |
| 1796 | if (Init.isInvalid()) { |
| 1797 | Actions.ActOnInitializerError(ThisDecl); |
| 1798 | } else |
| 1799 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1800 | /*DirectInit=*/true, TypeContainsAuto); |
| 1801 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1802 | } else { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1803 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1806 | Actions.FinalizeDeclaration(ThisDecl); |
| 1807 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1808 | return ThisDecl; |
| 1809 | } |
| 1810 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1811 | /// ParseSpecifierQualifierList |
| 1812 | /// specifier-qualifier-list: |
| 1813 | /// type-specifier specifier-qualifier-list[opt] |
| 1814 | /// type-qualifier specifier-qualifier-list[opt] |
| 1815 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 1816 | /// |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1817 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 1818 | DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1819 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1820 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1821 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1822 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1823 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1824 | // Validate declspec for type-name. |
| 1825 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 1826 | if ((DSC == DSC_type_specifier || DSC == DSC_trailing) && |
| 1827 | !DS.hasTypeSpecifier()) { |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1828 | Diag(Tok, diag::err_expected_type); |
| 1829 | DS.SetTypeSpecError(); |
| 1830 | } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 1831 | !DS.hasAttributes()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1832 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1833 | if (!DS.hasTypeSpecifier()) |
| 1834 | DS.SetTypeSpecError(); |
| 1835 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1836 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1837 | // Issue diagnostic and remove storage class if present. |
| 1838 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 1839 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1840 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 1841 | else |
| 1842 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
| 1843 | DS.ClearStorageClassSpecs(); |
| 1844 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1845 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1846 | // Issue diagnostic and remove function specfier if present. |
| 1847 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1848 | if (DS.isInlineSpecified()) |
| 1849 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1850 | if (DS.isVirtualSpecified()) |
| 1851 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1852 | if (DS.isExplicitSpecified()) |
| 1853 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1854 | DS.ClearFunctionSpecs(); |
| 1855 | } |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1856 | |
| 1857 | // Issue diagnostic and remove constexpr specfier if present. |
| 1858 | if (DS.isConstexprSpecified()) { |
| 1859 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 1860 | DS.ClearConstexprSpec(); |
| 1861 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1862 | } |
| 1863 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1864 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 1865 | /// specified token is valid after the identifier in a declarator which |
| 1866 | /// immediately follows the declspec. For example, these things are valid: |
| 1867 | /// |
| 1868 | /// int x [ 4]; // direct-declarator |
| 1869 | /// int x ( int y); // direct-declarator |
| 1870 | /// int(int x ) // direct-declarator |
| 1871 | /// int x ; // simple-declaration |
| 1872 | /// int x = 17; // init-declarator-list |
| 1873 | /// int x , y; // init-declarator-list |
| 1874 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1875 | /// int x : 4; // struct-declarator |
Chris Lattner | c83c27a | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 1876 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1877 | /// |
| 1878 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 1879 | /// ')' happens to be valid anyway). |
| 1880 | /// int (x) |
| 1881 | /// |
| 1882 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 1883 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 1884 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1885 | 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] | 1886 | } |
| 1887 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1888 | |
| 1889 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 1890 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 1891 | /// the declspec has no type specifier. In this case, the declspec is either |
| 1892 | /// malformed or is "implicit int" (in K&R and C89). |
| 1893 | /// |
| 1894 | /// This method handles diagnosing this prettily and returns false if the |
| 1895 | /// declspec is done being processed. If it recovers and thinks there may be |
| 1896 | /// other pieces of declspec after it, it returns true. |
| 1897 | /// |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1898 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1899 | const ParsedTemplateInfo &TemplateInfo, |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1900 | AccessSpecifier AS, DeclSpecContext DSC, |
| 1901 | ParsedAttributesWithRange &Attrs) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1902 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1903 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1904 | SourceLocation Loc = Tok.getLocation(); |
| 1905 | // If we see an identifier that is not a type name, we normally would |
| 1906 | // parse it as the identifer being declared. However, when a typename |
| 1907 | // is typo'd or the definition is not included, this will incorrectly |
| 1908 | // parse the typename as the identifier name and fall over misparsing |
| 1909 | // later parts of the diagnostic. |
| 1910 | // |
| 1911 | // As such, we try to do some look-ahead in cases where this would |
| 1912 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 1913 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 1914 | // an identifier with implicit int, we'd get a parse error because the |
| 1915 | // next token is obviously invalid for a type. Parse these as a case |
| 1916 | // with an invalid type specifier. |
| 1917 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1918 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1919 | // 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] | 1920 | // error, do lookahead to try to do better recovery. This never applies |
| 1921 | // within a type specifier. Outside of C++, we allow this even if the |
| 1922 | // language doesn't "officially" support implicit int -- we support |
| 1923 | // implicit int as an extension in C99 and C11. Allegedly, MS also |
| 1924 | // supports implicit int in C++ mode. |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 1925 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 1926 | (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) && |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1927 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1928 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 1929 | // we just avoid eating the identifier, so it will be parsed as the |
| 1930 | // identifier in the declarator. |
| 1931 | return false; |
| 1932 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1933 | |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 1934 | if (getLangOpts().CPlusPlus && |
| 1935 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 1936 | // Don't require a type specifier if we have the 'auto' storage class |
| 1937 | // specifier in C++98 -- we'll promote it to a type specifier. |
| 1938 | return false; |
| 1939 | } |
| 1940 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1941 | // Otherwise, if we don't consume this token, we are going to emit an |
| 1942 | // error anyway. Try to recover from various common problems. Check |
| 1943 | // to see if this was a reference to a tag name without a tag specified. |
| 1944 | // 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] | 1945 | // |
| 1946 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 1947 | if (SS == 0) { |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1948 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1949 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1950 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1951 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1952 | default: break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1953 | case DeclSpec::TST_enum: |
| 1954 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 1955 | case DeclSpec::TST_union: |
| 1956 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 1957 | case DeclSpec::TST_struct: |
| 1958 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 1959 | case DeclSpec::TST_interface: |
| 1960 | TagName="__interface"; FixitTagName = "__interface "; |
| 1961 | TagKind=tok::kw___interface;break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1962 | case DeclSpec::TST_class: |
| 1963 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1964 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1965 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1966 | if (TagName) { |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 1967 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 1968 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 1969 | Sema::LookupOrdinaryName); |
| 1970 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1971 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 1972 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 1973 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 1974 | |
| 1975 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 1976 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 1977 | I != IEnd; ++I) |
Kaelyn Uhrain | 392b3f5 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 1978 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 1979 | << TokenName << TagName; |
| 1980 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1981 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1982 | // Parse this as a tag as if the missing tag were present. |
| 1983 | if (TagKind == tok::kw_enum) |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1984 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1985 | else |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1986 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 1987 | /*EnteringContext*/ false, DSC_normal, Attrs); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1988 | return true; |
| 1989 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1990 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1991 | |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 1992 | // Determine whether this identifier could plausibly be the name of something |
Richard Smith | 7514db2 | 2012-05-15 21:42:17 +0000 | [diff] [blame] | 1993 | // being declared (with a missing type). |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 1994 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
| 1995 | (!SS || DSC == DSC_top_level || DSC == DSC_class)) { |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 1996 | // Look ahead to the next token to try to figure out what this declaration |
| 1997 | // was supposed to be. |
| 1998 | switch (NextToken().getKind()) { |
| 1999 | case tok::comma: |
| 2000 | case tok::equal: |
| 2001 | case tok::kw_asm: |
| 2002 | case tok::l_brace: |
| 2003 | case tok::l_square: |
| 2004 | case tok::semi: |
| 2005 | // This looks like a variable declaration. The type is probably missing. |
| 2006 | // We're done parsing decl-specifiers. |
| 2007 | return false; |
| 2008 | |
| 2009 | case tok::l_paren: { |
| 2010 | // static x(4); // 'x' is not a type |
| 2011 | // x(int n); // 'x' is not a type |
| 2012 | // x (*p)[]; // 'x' is a type |
| 2013 | // |
| 2014 | // Since we're in an error case (or the rare 'implicit int in C++' MS |
| 2015 | // extension), we can afford to perform a tentative parse to determine |
| 2016 | // which case we're in. |
| 2017 | TentativeParsingAction PA(*this); |
| 2018 | ConsumeToken(); |
| 2019 | TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false); |
| 2020 | PA.Revert(); |
| 2021 | if (TPR == TPResult::False()) |
| 2022 | return false; |
| 2023 | // The identifier is followed by a parenthesized declarator. |
| 2024 | // It's supposed to be a type. |
| 2025 | break; |
| 2026 | } |
| 2027 | |
| 2028 | default: |
| 2029 | // This is probably supposed to be a type. This includes cases like: |
| 2030 | // int f(itn); |
| 2031 | // struct S { unsinged : 4; }; |
| 2032 | break; |
| 2033 | } |
| 2034 | } |
| 2035 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2036 | // 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] | 2037 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2038 | ParsedType T; |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2039 | IdentifierInfo *II = Tok.getIdentifierInfo(); |
| 2040 | if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2041 | // The action emitted a diagnostic, so we don't have to. |
| 2042 | if (T) { |
| 2043 | // The action has suggested that the type T could be used. Set that as |
| 2044 | // the type in the declaration specifiers, consume the would-be type |
| 2045 | // name token, and we're done. |
| 2046 | const char *PrevSpec; |
| 2047 | unsigned DiagID; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2048 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2049 | DS.SetRangeEnd(Tok.getLocation()); |
| 2050 | ConsumeToken(); |
Kaelyn Uhrain | 50dc12a | 2012-06-15 23:45:58 +0000 | [diff] [blame] | 2051 | // There may be other declaration specifiers after this. |
| 2052 | return true; |
| 2053 | } else if (II != Tok.getIdentifierInfo()) { |
| 2054 | // If no type was suggested, the correction is to a keyword |
| 2055 | Tok.setKind(II->getTokenID()); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2056 | // There may be other declaration specifiers after this. |
| 2057 | return true; |
| 2058 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2059 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2060 | // Fall through; the action had no suggestion for us. |
| 2061 | } else { |
| 2062 | // The action did not emit a diagnostic, so emit one now. |
| 2063 | SourceRange R; |
| 2064 | if (SS) R = SS->getRange(); |
| 2065 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 2066 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2067 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 2068 | // Mark this as an error. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2069 | DS.SetTypeSpecError(); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2070 | DS.SetRangeEnd(Tok.getLocation()); |
| 2071 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2072 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 2073 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 2074 | // avoid rippling error messages on subsequent uses of the same type, |
| 2075 | // could be useful if #include was forgotten. |
| 2076 | return false; |
| 2077 | } |
| 2078 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2079 | /// \brief Determine the declaration specifier context from the declarator |
| 2080 | /// context. |
| 2081 | /// |
| 2082 | /// \param Context the declarator context, which is one of the |
| 2083 | /// Declarator::TheContext enumerator values. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2084 | Parser::DeclSpecContext |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2085 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 2086 | if (Context == Declarator::MemberContext) |
| 2087 | return DSC_class; |
| 2088 | if (Context == Declarator::FileContext) |
| 2089 | return DSC_top_level; |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 2090 | if (Context == Declarator::TrailingReturnContext) |
| 2091 | return DSC_trailing; |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2092 | return DSC_normal; |
| 2093 | } |
| 2094 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2095 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 2096 | /// |
| 2097 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 2098 | /// type. Ideally, the type should be propagated directly into Sema. |
| 2099 | /// |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2100 | /// [C11] type-id |
| 2101 | /// [C11] constant-expression |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2102 | /// [C++0x] type-id ...[opt] |
| 2103 | /// [C++0x] assignment-expression ...[opt] |
| 2104 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 2105 | SourceLocation &EllipsisLoc) { |
| 2106 | ExprResult ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2107 | if (isTypeIdInParens()) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2108 | SourceLocation TypeLoc = Tok.getLocation(); |
| 2109 | ParsedType Ty = ParseTypeName().get(); |
| 2110 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2111 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 2112 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2113 | } else |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2114 | ER = ParseConstantExpression(); |
| 2115 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2116 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis)) |
Peter Collingbourne | fe9b2a8 | 2011-10-24 17:56:00 +0000 | [diff] [blame] | 2117 | EllipsisLoc = ConsumeToken(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2118 | |
| 2119 | return ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2120 | } |
| 2121 | |
| 2122 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 2123 | /// attribute to Attrs. |
| 2124 | /// |
| 2125 | /// alignment-specifier: |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2126 | /// [C11] '_Alignas' '(' type-id ')' |
| 2127 | /// [C11] '_Alignas' '(' constant-expression ')' |
Richard Smith | 33f04a2 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2128 | /// [C++11] 'alignas' '(' type-id ...[opt] ')' |
| 2129 | /// [C++11] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2130 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2131 | SourceLocation *EndLoc) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2132 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 2133 | "Not an alignment-specifier!"); |
| 2134 | |
Richard Smith | 33f04a2 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2135 | IdentifierInfo *KWName = Tok.getIdentifierInfo(); |
| 2136 | SourceLocation KWLoc = ConsumeToken(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2137 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2138 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 2139 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2140 | return; |
| 2141 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2142 | SourceLocation EllipsisLoc; |
| 2143 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2144 | if (ArgExpr.isInvalid()) { |
| 2145 | SkipUntil(tok::r_paren); |
| 2146 | return; |
| 2147 | } |
| 2148 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2149 | T.consumeClose(); |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2150 | if (EndLoc) |
| 2151 | *EndLoc = T.getCloseLocation(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 2152 | |
Benjamin Kramer | 4e28d9e | 2012-08-23 22:51:59 +0000 | [diff] [blame] | 2153 | ExprVector ArgExprs; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2154 | ArgExprs.push_back(ArgExpr.release()); |
Richard Smith | 33f04a2 | 2013-01-29 01:48:07 +0000 | [diff] [blame] | 2155 | Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(), |
Richard Smith | f6565a9 | 2013-02-22 08:32:16 +0000 | [diff] [blame] | 2156 | ArgExprs.data(), 1, AttributeList::AS_Keyword, EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2159 | /// ParseDeclarationSpecifiers |
| 2160 | /// declaration-specifiers: [C99 6.7] |
| 2161 | /// storage-class-specifier declaration-specifiers[opt] |
| 2162 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2163 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2164 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2165 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2166 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2167 | /// |
| 2168 | /// storage-class-specifier: [C99 6.7.1] |
| 2169 | /// 'typedef' |
| 2170 | /// 'extern' |
| 2171 | /// 'static' |
| 2172 | /// 'auto' |
| 2173 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2174 | /// [C++] 'mutable' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2175 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2176 | /// function-specifier: [C99 6.7.4] |
| 2177 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2178 | /// [C++] 'virtual' |
| 2179 | /// [C++] 'explicit' |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2180 | /// [OpenCL] '__kernel' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2181 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2182 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2183 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2184 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 2185 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 2186 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2187 | AccessSpecifier AS, |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2188 | DeclSpecContext DSContext, |
| 2189 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 2190 | if (DS.getSourceRange().isInvalid()) { |
| 2191 | DS.SetRangeStart(Tok.getLocation()); |
| 2192 | DS.SetRangeEnd(Tok.getLocation()); |
| 2193 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2194 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2195 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2196 | bool AttrsLastTime = false; |
| 2197 | ParsedAttributesWithRange attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2198 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2199 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2200 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2201 | unsigned DiagID = 0; |
| 2202 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2203 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 2204 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2205 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2206 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2207 | DoneWithDeclSpec: |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2208 | if (!AttrsLastTime) |
| 2209 | ProhibitAttributes(attrs); |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2210 | else { |
| 2211 | // Reject C++11 attributes that appertain to decl specifiers as |
| 2212 | // we don't support any C++11 attributes that appertain to decl |
| 2213 | // specifiers. This also conforms to what g++ 4.8 is doing. |
| 2214 | ProhibitCXX11Attributes(attrs); |
| 2215 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2216 | DS.takeAttributesFrom(attrs); |
Michael Han | f64231e | 2012-11-06 19:34:54 +0000 | [diff] [blame] | 2217 | } |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 2218 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2219 | // If this is not a declaration specifier token, we're done reading decl |
| 2220 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2221 | DS.Finish(Diags, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2222 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2223 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2224 | case tok::l_square: |
| 2225 | case tok::kw_alignas: |
Richard Smith | 672edb0 | 2013-02-22 09:15:49 +0000 | [diff] [blame] | 2226 | if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier()) |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2227 | goto DoneWithDeclSpec; |
| 2228 | |
| 2229 | ProhibitAttributes(attrs); |
| 2230 | // FIXME: It would be good to recover by accepting the attributes, |
| 2231 | // but attempting to do that now would cause serious |
| 2232 | // madness in terms of diagnostics. |
| 2233 | attrs.clear(); |
| 2234 | attrs.Range = SourceRange(); |
| 2235 | |
| 2236 | ParseCXX11Attributes(attrs); |
| 2237 | AttrsLastTime = true; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2238 | continue; |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2239 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2240 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2241 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2242 | if (DS.hasTypeSpecifier()) { |
| 2243 | bool AllowNonIdentifiers |
| 2244 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 2245 | Scope::BlockScope | |
| 2246 | Scope::TemplateParamScope | |
| 2247 | Scope::FunctionPrototypeScope | |
| 2248 | Scope::AtCatchScope)) == 0; |
| 2249 | bool AllowNestedNameSpecifiers |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2250 | = DSContext == DSC_top_level || |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2251 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 2252 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2253 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2254 | AllowNonIdentifiers, |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 2255 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2256 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 2259 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 2260 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 2261 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2262 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2263 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2264 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2265 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 2266 | else if (CurParsedObjCImpl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2267 | CCC = Sema::PCC_ObjCImplementation; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2268 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2269 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2270 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 2271 | } |
| 2272 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2273 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2274 | // C++ scope specifier. Annotate and loop, or bail out on error. |
| 2275 | if (TryAnnotateCXXScopeToken(true)) { |
| 2276 | if (!DS.hasTypeSpecifier()) |
| 2277 | DS.SetTypeSpecError(); |
| 2278 | goto DoneWithDeclSpec; |
| 2279 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 2280 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 2281 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2282 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2283 | |
| 2284 | case tok::annot_cxxscope: { |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2285 | if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector()) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2286 | goto DoneWithDeclSpec; |
| 2287 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2288 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 2289 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 2290 | Tok.getAnnotationRange(), |
| 2291 | SS); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2292 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2293 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2294 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2295 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2296 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2297 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2298 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2299 | |
| 2300 | // C++ [class.qual]p2: |
| 2301 | // In a lookup in which the constructor is an acceptable lookup |
| 2302 | // result and the nested-name-specifier nominates a class C: |
| 2303 | // |
| 2304 | // - if the name specified after the |
| 2305 | // nested-name-specifier, when looked up in C, is the |
| 2306 | // injected-class-name of C (Clause 9), or |
| 2307 | // |
| 2308 | // - if the name specified after the nested-name-specifier |
| 2309 | // is the same as the identifier or the |
| 2310 | // simple-template-id's template-name in the last |
| 2311 | // component of the nested-name-specifier, |
| 2312 | // |
| 2313 | // the name is instead considered to name the constructor of |
| 2314 | // class C. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2315 | // |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2316 | // Thus, if the template-name is actually the constructor |
| 2317 | // name, then the code is ill-formed; this interpretation is |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2318 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2319 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2320 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2321 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2322 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2323 | if (isConstructorDeclarator()) { |
| 2324 | // The user meant this to be an out-of-line constructor |
| 2325 | // definition, but template arguments are not allowed |
| 2326 | // there. Just allow this as a constructor; we'll |
| 2327 | // complain about it later. |
| 2328 | goto DoneWithDeclSpec; |
| 2329 | } |
| 2330 | |
| 2331 | // The user meant this to name a type, but it actually names |
| 2332 | // a constructor with some extraneous template |
| 2333 | // arguments. Complain, then parse it as a type as the user |
| 2334 | // intended. |
| 2335 | Diag(TemplateId->TemplateNameLoc, |
| 2336 | diag::err_out_of_line_template_id_names_constructor) |
| 2337 | << TemplateId->Name; |
| 2338 | } |
| 2339 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2340 | DS.getTypeSpecScope() = SS; |
| 2341 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2342 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2343 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2344 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2345 | continue; |
| 2346 | } |
| 2347 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2348 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2349 | DS.getTypeSpecScope() = SS; |
| 2350 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2351 | if (Tok.getAnnotationValue()) { |
| 2352 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2353 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2354 | Tok.getAnnotationEndLoc(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2355 | PrevSpec, DiagID, T); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2356 | if (isInvalid) |
| 2357 | break; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2358 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2359 | else |
| 2360 | DS.SetTypeSpecError(); |
| 2361 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2362 | ConsumeToken(); // The typename |
| 2363 | } |
| 2364 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2365 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2366 | goto DoneWithDeclSpec; |
| 2367 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2368 | // If we're in a context where the identifier could be a class name, |
| 2369 | // check whether this is a constructor declaration. |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 2370 | if ((DSContext == DSC_top_level || DSContext == DSC_class) && |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2371 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2372 | &SS)) { |
| 2373 | if (isConstructorDeclarator()) |
| 2374 | goto DoneWithDeclSpec; |
| 2375 | |
| 2376 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 2377 | // of the class is qualified in a context where it could name |
| 2378 | // a constructor, its a constructor name. However, we've |
| 2379 | // looked at the declarator, and the user probably meant this |
| 2380 | // to be a type. Complain that it isn't supposed to be treated |
| 2381 | // as a type, then proceed to parse it as a type. |
| 2382 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 2383 | << Next.getIdentifierInfo(); |
| 2384 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2385 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2386 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 2387 | Next.getLocation(), |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2388 | getCurScope(), &SS, |
| 2389 | false, false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2390 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2391 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2392 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2393 | // If the referenced identifier is not a type, then this declspec is |
| 2394 | // erroneous: We already checked about that it has no type specifier, and |
| 2395 | // 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] | 2396 | // typename. |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2397 | if (TypeRep == 0) { |
| 2398 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2399 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2400 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) { |
| 2401 | if (!Attrs.empty()) { |
| 2402 | AttrsLastTime = true; |
| 2403 | attrs.takeAllFrom(Attrs); |
| 2404 | } |
| 2405 | continue; |
| 2406 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2407 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2408 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2409 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2410 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2411 | ConsumeToken(); // The C++ scope. |
| 2412 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2413 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2414 | DiagID, TypeRep); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2415 | if (isInvalid) |
| 2416 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2417 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2418 | DS.SetRangeEnd(Tok.getLocation()); |
| 2419 | ConsumeToken(); // The typename. |
| 2420 | |
| 2421 | continue; |
| 2422 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2423 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2424 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2425 | if (Tok.getAnnotationValue()) { |
| 2426 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | c43271e | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 2427 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2428 | DiagID, T); |
| 2429 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2430 | DS.SetTypeSpecError(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2431 | |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 2432 | if (isInvalid) |
| 2433 | break; |
| 2434 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2435 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2436 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2437 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2438 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2439 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2440 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2441 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2442 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2443 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2444 | continue; |
| 2445 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2446 | |
Douglas Gregor | bfad915 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2447 | case tok::kw___is_signed: |
| 2448 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 2449 | // typically treats it as a trait. If we see __is_signed as it appears |
| 2450 | // in libstdc++, e.g., |
| 2451 | // |
| 2452 | // static const bool __is_signed; |
| 2453 | // |
| 2454 | // then treat __is_signed as an identifier rather than as a keyword. |
| 2455 | if (DS.getTypeSpecType() == TST_bool && |
| 2456 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
| 2457 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 2458 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
| 2459 | Tok.setKind(tok::identifier); |
| 2460 | } |
| 2461 | |
| 2462 | // We're done with the declaration-specifiers. |
| 2463 | goto DoneWithDeclSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2464 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2465 | // typedef-name |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2466 | case tok::kw_decltype: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2467 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2468 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 2469 | // so handle it as such. This is important for ctor parsing. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2470 | if (getLangOpts().CPlusPlus) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2471 | if (TryAnnotateCXXScopeToken(true)) { |
| 2472 | if (!DS.hasTypeSpecifier()) |
| 2473 | DS.SetTypeSpecError(); |
| 2474 | goto DoneWithDeclSpec; |
| 2475 | } |
| 2476 | if (!Tok.is(tok::identifier)) |
| 2477 | continue; |
| 2478 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2479 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2480 | // This identifier can only be a typedef name if we haven't already seen |
| 2481 | // a type-specifier. Without this check we misparse: |
| 2482 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 2483 | if (DS.hasTypeSpecifier()) |
| 2484 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2485 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2486 | // Check for need to substitute AltiVec keyword tokens. |
| 2487 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2488 | break; |
| 2489 | |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2490 | // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not |
| 2491 | // allow the use of a typedef name as a type specifier. |
| 2492 | if (DS.isTypeAltiVecVector()) |
| 2493 | goto DoneWithDeclSpec; |
| 2494 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2495 | ParsedType TypeRep = |
| 2496 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 2497 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2498 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2499 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 2500 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2501 | if (!TypeRep) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2502 | ParsedAttributesWithRange Attrs(AttrFactory); |
| 2503 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) { |
| 2504 | if (!Attrs.empty()) { |
| 2505 | AttrsLastTime = true; |
| 2506 | attrs.takeAllFrom(Attrs); |
| 2507 | } |
| 2508 | continue; |
| 2509 | } |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2510 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2511 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2512 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2513 | // If we're in a context where the identifier could be a class name, |
| 2514 | // check whether this is a constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2515 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2516 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2517 | isConstructorDeclarator()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2518 | goto DoneWithDeclSpec; |
| 2519 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2520 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2521 | DiagID, TypeRep); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2522 | if (isInvalid) |
| 2523 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2524 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2525 | DS.SetRangeEnd(Tok.getLocation()); |
| 2526 | ConsumeToken(); // The identifier |
| 2527 | |
| 2528 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2529 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2530 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2531 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2532 | ParseObjCProtocolQualifiers(DS); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2533 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2534 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2535 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2536 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2537 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2538 | |
| 2539 | // type-name |
| 2540 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2541 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2542 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2543 | // This template-id does not refer to a type name, so we're |
| 2544 | // done with the type-specifiers. |
| 2545 | goto DoneWithDeclSpec; |
| 2546 | } |
| 2547 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2548 | // If we're in a context where the template-id could be a |
| 2549 | // constructor name or specialization, check whether this is a |
| 2550 | // constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2551 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2552 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2553 | isConstructorDeclarator()) |
| 2554 | goto DoneWithDeclSpec; |
| 2555 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2556 | // Turn the template-id annotation token into a type annotation |
| 2557 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2558 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2559 | continue; |
| 2560 | } |
| 2561 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2562 | // GNU attributes support. |
| 2563 | case tok::kw___attribute: |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2564 | ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2565 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2566 | |
| 2567 | // Microsoft declspec support. |
| 2568 | case tok::kw___declspec: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2569 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2570 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2571 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2572 | // Microsoft single token adornments. |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2573 | case tok::kw___forceinline: { |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2574 | isInvalid = DS.setFunctionSpecInline(Loc); |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2575 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2576 | SourceLocation AttrNameLoc = Tok.getLocation(); |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 2577 | // FIXME: This does not work correctly if it is set to be a declspec |
| 2578 | // attribute, and a GNU attribute is simply incorrect. |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2579 | DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
Sean Hunt | 93f95f2 | 2012-06-18 16:13:52 +0000 | [diff] [blame] | 2580 | SourceLocation(), 0, 0, AttributeList::AS_GNU); |
Richard Smith | b3cd3c0 | 2012-09-14 18:27:01 +0000 | [diff] [blame] | 2581 | break; |
Michael J. Spencer | adc6cbf | 2012-06-18 07:00:48 +0000 | [diff] [blame] | 2582 | } |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2583 | |
| 2584 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2585 | case tok::kw___ptr32: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2586 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2587 | case tok::kw___cdecl: |
| 2588 | case tok::kw___stdcall: |
| 2589 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2590 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2591 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2592 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2593 | continue; |
| 2594 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2595 | // Borland single token adornments. |
| 2596 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2597 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2598 | continue; |
| 2599 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2600 | // OpenCL single token adornments. |
| 2601 | case tok::kw___kernel: |
| 2602 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2603 | continue; |
| 2604 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2605 | // storage-class-specifier |
| 2606 | case tok::kw_typedef: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2607 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
| 2608 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2609 | break; |
| 2610 | case tok::kw_extern: |
| 2611 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2612 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2613 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
| 2614 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2615 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2616 | case tok::kw___private_extern__: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2617 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
| 2618 | Loc, PrevSpec, DiagID); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2619 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2620 | case tok::kw_static: |
| 2621 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2622 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2623 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
| 2624 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2625 | break; |
| 2626 | case tok::kw_auto: |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 2627 | if (getLangOpts().CPlusPlus11) { |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2628 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2629 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2630 | PrevSpec, DiagID); |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2631 | if (!isInvalid) |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2632 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2633 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2634 | } else |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2635 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 2636 | DiagID); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2637 | } else |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2638 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2639 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2640 | break; |
| 2641 | case tok::kw_register: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2642 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
| 2643 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2644 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2645 | case tok::kw_mutable: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2646 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
| 2647 | PrevSpec, DiagID); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2648 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2649 | case tok::kw___thread: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2650 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2651 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2652 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2653 | // function-specifier |
| 2654 | case tok::kw_inline: |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2655 | isInvalid = DS.setFunctionSpecInline(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2656 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2657 | case tok::kw_virtual: |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2658 | isInvalid = DS.setFunctionSpecVirtual(Loc); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2659 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2660 | case tok::kw_explicit: |
Chad Rosier | 22aa690 | 2012-12-21 22:24:43 +0000 | [diff] [blame] | 2661 | isInvalid = DS.setFunctionSpecExplicit(Loc); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2662 | break; |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 2663 | case tok::kw__Noreturn: |
| 2664 | if (!getLangOpts().C11) |
| 2665 | Diag(Loc, diag::ext_c11_noreturn); |
| 2666 | isInvalid = DS.setFunctionSpecNoreturn(Loc); |
| 2667 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2668 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2669 | // alignment-specifier |
| 2670 | case tok::kw__Alignas: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2671 | if (!getLangOpts().C11) |
Jordan Rose | f70a886 | 2012-06-30 21:33:57 +0000 | [diff] [blame] | 2672 | Diag(Tok, diag::ext_c11_alignment) << Tok.getName(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2673 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2674 | continue; |
| 2675 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2676 | // friend |
| 2677 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2678 | if (DSContext == DSC_class) |
| 2679 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 2680 | else { |
| 2681 | PrevSpec = ""; // not actually used by the diagnostic |
| 2682 | DiagID = diag::err_friend_invalid_in_context; |
| 2683 | isInvalid = true; |
| 2684 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2685 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2686 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2687 | // Modules |
| 2688 | case tok::kw___module_private__: |
| 2689 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 2690 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2691 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2692 | // constexpr |
| 2693 | case tok::kw_constexpr: |
| 2694 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 2695 | break; |
| 2696 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2697 | // type-specifier |
| 2698 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2699 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 2700 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2701 | break; |
| 2702 | case tok::kw_long: |
| 2703 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2704 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2705 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2706 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2707 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2708 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2709 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2710 | case tok::kw___int64: |
| 2711 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2712 | DiagID); |
| 2713 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2714 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2715 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 2716 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2717 | break; |
| 2718 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2719 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2720 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2721 | break; |
| 2722 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2723 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2724 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2725 | break; |
| 2726 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2727 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2728 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2729 | break; |
| 2730 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2731 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 2732 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2733 | break; |
| 2734 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2735 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 2736 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2737 | break; |
| 2738 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2739 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 2740 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2741 | break; |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 2742 | case tok::kw___int128: |
| 2743 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
| 2744 | DiagID); |
| 2745 | break; |
| 2746 | case tok::kw_half: |
| 2747 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
| 2748 | DiagID); |
| 2749 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2750 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2751 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 2752 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2753 | break; |
| 2754 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2755 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 2756 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2757 | break; |
| 2758 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2759 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 2760 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2761 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2762 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2763 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 2764 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2765 | break; |
| 2766 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2767 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 2768 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2769 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2770 | case tok::kw_bool: |
| 2771 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2772 | if (Tok.is(tok::kw_bool) && |
| 2773 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 2774 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 2775 | PrevSpec = ""; // Not used by the diagnostic. |
| 2776 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2777 | // For better error recovery. |
| 2778 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2779 | isInvalid = true; |
| 2780 | } else { |
| 2781 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 2782 | DiagID); |
| 2783 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2784 | break; |
| 2785 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2786 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2787 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2788 | break; |
| 2789 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2790 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2791 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2792 | break; |
| 2793 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2794 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2795 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2796 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2797 | case tok::kw___vector: |
| 2798 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2799 | break; |
| 2800 | case tok::kw___pixel: |
| 2801 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2802 | break; |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 2803 | case tok::kw_image1d_t: |
| 2804 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc, |
| 2805 | PrevSpec, DiagID); |
| 2806 | break; |
| 2807 | case tok::kw_image1d_array_t: |
| 2808 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc, |
| 2809 | PrevSpec, DiagID); |
| 2810 | break; |
| 2811 | case tok::kw_image1d_buffer_t: |
| 2812 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc, |
| 2813 | PrevSpec, DiagID); |
| 2814 | break; |
| 2815 | case tok::kw_image2d_t: |
| 2816 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc, |
| 2817 | PrevSpec, DiagID); |
| 2818 | break; |
| 2819 | case tok::kw_image2d_array_t: |
| 2820 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc, |
| 2821 | PrevSpec, DiagID); |
| 2822 | break; |
| 2823 | case tok::kw_image3d_t: |
| 2824 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc, |
| 2825 | PrevSpec, DiagID); |
| 2826 | break; |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 2827 | case tok::kw_sampler_t: |
| 2828 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc, |
| 2829 | PrevSpec, DiagID); |
| 2830 | break; |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 2831 | case tok::kw_event_t: |
| 2832 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc, |
| 2833 | PrevSpec, DiagID); |
| 2834 | break; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 2835 | case tok::kw___unknown_anytype: |
| 2836 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 2837 | PrevSpec, DiagID); |
| 2838 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2839 | |
| 2840 | // class-specifier: |
| 2841 | case tok::kw_class: |
| 2842 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 2843 | case tok::kw___interface: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2844 | case tok::kw_union: { |
| 2845 | tok::TokenKind Kind = Tok.getKind(); |
| 2846 | ConsumeToken(); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2847 | |
| 2848 | // These are attributes following class specifiers. |
| 2849 | // To produce better diagnostic, we parse them when |
| 2850 | // parsing class specifier. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2851 | ParsedAttributesWithRange Attributes(AttrFactory); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2852 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2853 | EnteringContext, DSContext, Attributes); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2854 | |
| 2855 | // If there are attributes following class specifier, |
| 2856 | // take them over and handle them here. |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2857 | if (!Attributes.empty()) { |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2858 | AttrsLastTime = true; |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 2859 | attrs.takeAllFrom(Attributes); |
Michael Han | 2e39713 | 2012-11-26 22:54:45 +0000 | [diff] [blame] | 2860 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2861 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2862 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2863 | |
| 2864 | // enum-specifier: |
| 2865 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2866 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2867 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2868 | continue; |
| 2869 | |
| 2870 | // cv-qualifier: |
| 2871 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2872 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 2873 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2874 | break; |
| 2875 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2876 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 2877 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2878 | break; |
| 2879 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2880 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 2881 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2882 | break; |
| 2883 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2884 | // C++ typename-specifier: |
| 2885 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2886 | if (TryAnnotateTypeOrScopeToken()) { |
| 2887 | DS.SetTypeSpecError(); |
| 2888 | goto DoneWithDeclSpec; |
| 2889 | } |
| 2890 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2891 | continue; |
| 2892 | break; |
| 2893 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2894 | // GNU typeof support. |
| 2895 | case tok::kw_typeof: |
| 2896 | ParseTypeofSpecifier(DS); |
| 2897 | continue; |
| 2898 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2899 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2900 | ParseDecltypeSpecifier(DS); |
| 2901 | continue; |
| 2902 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2903 | case tok::kw___underlying_type: |
| 2904 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2905 | continue; |
| 2906 | |
| 2907 | case tok::kw__Atomic: |
| 2908 | ParseAtomicSpecifier(DS); |
| 2909 | continue; |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2910 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2911 | // OpenCL qualifiers: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2912 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2913 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2914 | goto DoneWithDeclSpec; |
| 2915 | case tok::kw___private: |
| 2916 | case tok::kw___global: |
| 2917 | case tok::kw___local: |
| 2918 | case tok::kw___constant: |
| 2919 | case tok::kw___read_only: |
| 2920 | case tok::kw___write_only: |
| 2921 | case tok::kw___read_write: |
| 2922 | ParseOpenCLQualifiers(DS); |
| 2923 | break; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2924 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 2925 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2926 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2927 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 2928 | // but we support it. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2929 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2930 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2931 | |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 2932 | if (!ParseObjCProtocolQualifiers(DS)) |
| 2933 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 2934 | << FixItHint::CreateInsertion(Loc, "id") |
| 2935 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2936 | |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2937 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2938 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2939 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2940 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2941 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2942 | if (isInvalid) { |
| 2943 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2944 | assert(DiagID); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2945 | |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 2946 | if (DiagID == diag::ext_duplicate_declspec) |
| 2947 | Diag(Tok, DiagID) |
| 2948 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 2949 | else |
| 2950 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2951 | } |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2952 | |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 2953 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2954 | if (DiagID != diag::err_bool_redeclaration) |
| 2955 | ConsumeToken(); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 2956 | |
| 2957 | AttrsLastTime = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2958 | } |
| 2959 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 2960 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2961 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 2962 | /// semicolon. |
| 2963 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2964 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2965 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2966 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2967 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2968 | /// struct-declarator-list: |
| 2969 | /// struct-declarator |
| 2970 | /// struct-declarator-list ',' struct-declarator |
| 2971 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 2972 | /// struct-declarator: |
| 2973 | /// declarator |
| 2974 | /// [GNU] declarator attributes[opt] |
| 2975 | /// declarator[opt] ':' constant-expression |
| 2976 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 2977 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2978 | void Parser:: |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 2979 | ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 2980 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2981 | if (Tok.is(tok::kw___extension__)) { |
| 2982 | // __extension__ silences extension warnings in the subexpression. |
| 2983 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2984 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2985 | return ParseStructDeclaration(DS, Fields); |
| 2986 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2987 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2988 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2989 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2990 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 2991 | // If there are no declarators, this is a free-standing declaration |
| 2992 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2993 | if (Tok.is(tok::semi)) { |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 2994 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
| 2995 | DS); |
| 2996 | DS.complete(TheDecl); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2997 | return; |
| 2998 | } |
| 2999 | |
| 3000 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3001 | bool FirstDeclarator = true; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3002 | SourceLocation CommaLoc; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3003 | while (1) { |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3004 | ParsingFieldDeclarator DeclaratorInfo(*this, DS); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3005 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3006 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3007 | // Attributes are only allowed here on successive declarators. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3008 | if (!FirstDeclarator) |
| 3009 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3010 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3011 | /// struct-declarator: declarator |
| 3012 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3013 | if (Tok.isNot(tok::colon)) { |
| 3014 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 3015 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3016 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 3017 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3018 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3019 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3020 | ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3021 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3022 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3023 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 3024 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 3025 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3026 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3027 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3028 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3029 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3030 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3031 | // We're done with this declarator; invoke the callback. |
Eli Friedman | 817a886 | 2012-08-08 23:35:12 +0000 | [diff] [blame] | 3032 | Fields.invoke(DeclaratorInfo); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3033 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3034 | // If we don't have a comma, it is either the end of the list (a ';') |
| 3035 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3036 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 3037 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3038 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3039 | // Consume the comma. |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 3040 | CommaLoc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3041 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3042 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3043 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
| 3046 | /// ParseStructUnionBody |
| 3047 | /// struct-contents: |
| 3048 | /// struct-declaration-list |
| 3049 | /// [EXT] empty |
| 3050 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 3051 | /// struct-declaration-list: |
| 3052 | /// struct-declaration |
| 3053 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3054 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 3055 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3056 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3057 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3058 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 3059 | "parsing struct/union body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3060 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3061 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3062 | if (T.consumeOpen()) |
| 3063 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3064 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 3065 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3066 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3067 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3068 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 3069 | // C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3070 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) { |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 3071 | Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union); |
| 3072 | Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union); |
| 3073 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3074 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3075 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3076 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3077 | // 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] | 3078 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3079 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3080 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3081 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3082 | if (Tok.is(tok::semi)) { |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3083 | ConsumeExtraSemi(InsideStruct, TagType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3084 | continue; |
| 3085 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 3086 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3087 | if (!Tok.is(tok::at)) { |
| 3088 | struct CFieldCallback : FieldCallback { |
| 3089 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3090 | Decl *TagDecl; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3091 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3092 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3093 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3094 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3095 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 3096 | |
Eli Friedman | dcdff46 | 2012-08-08 23:53:27 +0000 | [diff] [blame] | 3097 | void invoke(ParsingFieldDeclarator &FD) { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3098 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3099 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 3100 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 3101 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3102 | FieldDecls.push_back(Field); |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3103 | FD.complete(Field); |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3104 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3105 | } Callback(*this, TagDecl, FieldDecls); |
| 3106 | |
Eli Friedman | f66a0dd | 2012-08-08 23:04:35 +0000 | [diff] [blame] | 3107 | // Parse all the comma separated declarators. |
| 3108 | ParsingDeclSpec DS(*this); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 3109 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3110 | } else { // Handle @defs |
| 3111 | ConsumeToken(); |
| 3112 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 3113 | Diag(Tok, diag::err_unexpected_at); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3114 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3115 | continue; |
| 3116 | } |
| 3117 | ConsumeToken(); |
| 3118 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 3119 | if (!Tok.is(tok::identifier)) { |
| 3120 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3121 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3122 | continue; |
| 3123 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3124 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3125 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 3126 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 3127 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 3128 | ConsumeToken(); |
| 3129 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3130 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3131 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3132 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3133 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3134 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3135 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3136 | break; |
| 3137 | } else { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3138 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 3139 | // 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] | 3140 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 3141 | // If we stopped at a ';', eat it. |
| 3142 | if (Tok.is(tok::semi)) ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3143 | } |
| 3144 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3145 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3146 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3147 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3148 | ParsedAttributes attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3149 | // If attributes exist after struct contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3150 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 3151 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3152 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 3153 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3154 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3155 | attrs.getList()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3156 | StructScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3157 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 3158 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3159 | } |
| 3160 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3161 | /// ParseEnumSpecifier |
| 3162 | /// enum-specifier: [C99 6.7.2.2] |
| 3163 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3164 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3165 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3166 | /// '}' attributes[opt] |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 3167 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 3168 | /// '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3169 | /// 'enum' identifier |
| 3170 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3171 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3172 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 3173 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3174 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3175 | /// enum-head: [C++11] |
| 3176 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 3177 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 3178 | /// identifier enum-base[opt] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3179 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3180 | /// enum-key: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3181 | /// 'enum' |
| 3182 | /// 'enum' 'class' |
| 3183 | /// 'enum' 'struct' |
| 3184 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3185 | /// enum-base: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3186 | /// ':' type-specifier-seq |
| 3187 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3188 | /// [C++] elaborated-type-specifier: |
| 3189 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 3190 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 3191 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 3192 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 3193 | AccessSpecifier AS, DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3194 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3195 | if (Tok.is(tok::code_completion)) { |
| 3196 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3197 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3198 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 3199 | } |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3200 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3201 | // If attributes exist after tag, parse them. |
| 3202 | ParsedAttributesWithRange attrs(AttrFactory); |
| 3203 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3204 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3205 | |
| 3206 | // If declspecs exist after tag, parse them. |
| 3207 | while (Tok.is(tok::kw___declspec)) |
| 3208 | ParseMicrosoftDeclSpec(attrs); |
| 3209 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3210 | SourceLocation ScopedEnumKWLoc; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3211 | bool IsScopedUsingClassTag = false; |
| 3212 | |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3213 | // In C++11, recognize 'enum class' and 'enum struct'. |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3214 | if (getLangOpts().CPlusPlus11 && |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3215 | (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3216 | Diag(Tok, diag::warn_cxx98_compat_scoped_enum); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3217 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3218 | ScopedEnumKWLoc = ConsumeToken(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3219 | |
Bill Wendling | ad017fa | 2012-12-20 19:22:21 +0000 | [diff] [blame] | 3220 | // Attributes are not allowed between these keywords. Diagnose, |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3221 | // but then just treat them like they appeared in the right place. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3222 | ProhibitAttributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3223 | |
| 3224 | // They are allowed afterwards, though. |
| 3225 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3226 | MaybeParseCXX11Attributes(attrs); |
John McCall | 1e12b3d | 2012-06-23 22:30:04 +0000 | [diff] [blame] | 3227 | while (Tok.is(tok::kw___declspec)) |
| 3228 | ParseMicrosoftDeclSpec(attrs); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3229 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3230 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3231 | // C++11 [temp.explicit]p12: |
| 3232 | // The usual access controls do not apply to names used to specify |
| 3233 | // explicit instantiations. |
| 3234 | // We extend this to also cover explicit specializations. Note that |
| 3235 | // we don't suppress if this turns out to be an elaborated type |
| 3236 | // specifier. |
| 3237 | bool shouldDelayDiagsInTag = |
| 3238 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 3239 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 3240 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3241 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3242 | // Enum definitions should not be parsed in a trailing-return-type. |
| 3243 | bool AllowDeclaration = DSC != DSC_trailing; |
| 3244 | |
| 3245 | bool AllowFixedUnderlyingType = AllowDeclaration && |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3246 | (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt || |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3247 | getLangOpts().ObjC2); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3248 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 3249 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3250 | if (getLangOpts().CPlusPlus) { |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 3251 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 3252 | // if a fixed underlying type is allowed. |
| 3253 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3254 | |
| 3255 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3256 | /*EnteringContext=*/false)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3257 | return; |
| 3258 | |
| 3259 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3260 | Diag(Tok, diag::err_expected_ident); |
| 3261 | if (Tok.isNot(tok::l_brace)) { |
| 3262 | // Has no name and is not a definition. |
| 3263 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3264 | SkipUntil(tok::comma, true); |
| 3265 | return; |
| 3266 | } |
| 3267 | } |
| 3268 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3269 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3270 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3271 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3272 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3273 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3274 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3275 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3276 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3277 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3278 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3279 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3280 | // If an identifier is present, consume and remember it. |
| 3281 | IdentifierInfo *Name = 0; |
| 3282 | SourceLocation NameLoc; |
| 3283 | if (Tok.is(tok::identifier)) { |
| 3284 | Name = Tok.getIdentifierInfo(); |
| 3285 | NameLoc = ConsumeToken(); |
| 3286 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3287 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3288 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3289 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 3290 | // declaration of a scoped enumeration. |
| 3291 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3292 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3293 | IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3294 | } |
| 3295 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3296 | // Okay, end the suppression area. We'll decide whether to emit the |
| 3297 | // diagnostics in a second. |
| 3298 | if (shouldDelayDiagsInTag) |
| 3299 | diagsFromTag.done(); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3300 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3301 | TypeResult BaseType; |
| 3302 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3303 | // Parse the fixed underlying type. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3304 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3305 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3306 | bool PossibleBitfield = false; |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3307 | if (CanBeBitfield) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3308 | // If we're in class scope, this can either be an enum declaration with |
| 3309 | // an underlying type, or a declaration of a bitfield member. We try to |
| 3310 | // use a simple disambiguation scheme first to catch the common cases |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3311 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 3312 | // anything that's a simple-type-specifier followed by '(' as an |
| 3313 | // expression. This suffices because function types are not valid |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3314 | // underlying types anyway. |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 3315 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 3316 | Sema::ConstantEvaluated); |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3317 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3318 | // 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] | 3319 | // bit-field. This is the common case. |
| 3320 | if (TPR == TPResult::True()) |
| 3321 | PossibleBitfield = true; |
| 3322 | // If the next token starts a type-specifier-seq, it may be either a |
| 3323 | // 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] | 3324 | // 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] | 3325 | // fixed underlying type. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3326 | else if (TPR == TPResult::False() && |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3327 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 3328 | // Consume the ':'. |
| 3329 | ConsumeToken(); |
| 3330 | } else { |
| 3331 | // We have the start of a type-specifier-seq, so we have to perform |
| 3332 | // tentative parsing to determine whether we have an expression or a |
| 3333 | // type. |
| 3334 | TentativeParsingAction TPA(*this); |
| 3335 | |
| 3336 | // Consume the ':'. |
| 3337 | ConsumeToken(); |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3338 | |
| 3339 | // If we see a type specifier followed by an open-brace, we have an |
| 3340 | // ambiguity between an underlying type and a C++11 braced |
| 3341 | // function-style cast. Resolve this by always treating it as an |
| 3342 | // underlying type. |
| 3343 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 3344 | // this case. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3345 | if ((getLangOpts().CPlusPlus && |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 3346 | isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3347 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3348 | // We'll parse this as a bitfield later. |
| 3349 | PossibleBitfield = true; |
| 3350 | TPA.Revert(); |
| 3351 | } else { |
| 3352 | // We have a type-specifier-seq. |
| 3353 | TPA.Commit(); |
| 3354 | } |
| 3355 | } |
| 3356 | } else { |
| 3357 | // Consume the ':'. |
| 3358 | ConsumeToken(); |
| 3359 | } |
| 3360 | |
| 3361 | if (!PossibleBitfield) { |
| 3362 | SourceRange Range; |
| 3363 | BaseType = ParseTypeName(&Range); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3364 | |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3365 | if (getLangOpts().CPlusPlus11) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3366 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Eli Friedman | cef3a7b | 2012-11-02 01:34:28 +0000 | [diff] [blame] | 3367 | } else if (!getLangOpts().ObjC2) { |
| 3368 | if (getLangOpts().CPlusPlus) |
| 3369 | Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range; |
| 3370 | else |
| 3371 | Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range; |
| 3372 | } |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 3373 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3374 | } |
| 3375 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3376 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 3377 | // friend declaration, and cannot have an accompanying definition. If we have |
| 3378 | // 'enum foo;', then this is a forward declaration. If we have |
| 3379 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 3380 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 3381 | // |
| 3382 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 3383 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 3384 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 3385 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3386 | Sema::TagUseKind TUK; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3387 | if (!AllowDeclaration) { |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3388 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3389 | } else if (Tok.is(tok::l_brace)) { |
| 3390 | if (DS.isFriendSpecified()) { |
| 3391 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
| 3392 | << SourceRange(DS.getFriendSpecLoc()); |
| 3393 | ConsumeBrace(); |
| 3394 | SkipUntil(tok::r_brace); |
| 3395 | TUK = Sema::TUK_Friend; |
| 3396 | } else { |
| 3397 | TUK = Sema::TUK_Definition; |
| 3398 | } |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3399 | } else if (DSC != DSC_type_specifier && |
| 3400 | (Tok.is(tok::semi) || |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3401 | (Tok.isAtStartOfLine() && |
| 3402 | !isValidAfterTypeSpecifier(CanBeBitfield)))) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3403 | TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration; |
| 3404 | if (Tok.isNot(tok::semi)) { |
| 3405 | // A semicolon was missing after this declaration. Diagnose and recover. |
| 3406 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, |
| 3407 | "enum"); |
| 3408 | PP.EnterToken(Tok); |
| 3409 | Tok.setKind(tok::semi); |
| 3410 | } |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3411 | } else { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3412 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3413 | } |
| 3414 | |
| 3415 | // If this is an elaborated type specifier, and we delayed |
| 3416 | // diagnostics before, just merge them into the current pool. |
| 3417 | if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) { |
| 3418 | diagsFromTag.redelay(); |
| 3419 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3420 | |
| 3421 | MultiTemplateParamsArg TParams; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3422 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3423 | TUK != Sema::TUK_Reference) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3424 | if (!getLangOpts().CPlusPlus11 || !SS.isSet()) { |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3425 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3426 | Diag(Tok, diag::err_enum_template); |
| 3427 | SkipUntil(tok::comma, true); |
| 3428 | return; |
| 3429 | } |
| 3430 | |
| 3431 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 3432 | // Enumerations can't be explicitly instantiated. |
| 3433 | DS.SetTypeSpecError(); |
| 3434 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 3435 | return; |
| 3436 | } |
| 3437 | |
| 3438 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 3439 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 3440 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3441 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3442 | |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3443 | if (TUK == Sema::TUK_Reference) |
| 3444 | ProhibitAttributes(attrs); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3445 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3446 | if (!Name && TUK != Sema::TUK_Definition) { |
| 3447 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3448 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3449 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3450 | SkipUntil(tok::comma, true); |
| 3451 | return; |
| 3452 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3453 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3454 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3455 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3456 | const char *PrevSpec = 0; |
| 3457 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3458 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3459 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3460 | AS, DS.getModulePrivateSpecLoc(), TParams, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3461 | Owned, IsDependent, ScopedEnumKWLoc, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3462 | IsScopedUsingClassTag, BaseType); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3463 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3464 | if (IsDependent) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3465 | // This enum has a dependent nested-name-specifier. Handle it as a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3466 | // dependent tag. |
| 3467 | if (!Name) { |
| 3468 | DS.SetTypeSpecError(); |
| 3469 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 3470 | return; |
| 3471 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3472 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3473 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3474 | TUK, SS, Name, StartLoc, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3475 | NameLoc); |
| 3476 | if (Type.isInvalid()) { |
| 3477 | DS.SetTypeSpecError(); |
| 3478 | return; |
| 3479 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3480 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3481 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 3482 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3483 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3484 | Diag(StartLoc, DiagID) << PrevSpec; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3485 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3486 | return; |
| 3487 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3488 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3489 | if (!TagDecl) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3490 | // The action failed to produce an enumeration tag. If this is a |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3491 | // definition, consume the entire definition. |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3492 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3493 | ConsumeBrace(); |
| 3494 | SkipUntil(tok::r_brace); |
| 3495 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3496 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3497 | DS.SetTypeSpecError(); |
| 3498 | return; |
| 3499 | } |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3500 | |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3501 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3502 | ParseEnumBody(StartLoc, TagDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3503 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3504 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3505 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3506 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3507 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3511 | /// enumerator-list: |
| 3512 | /// enumerator |
| 3513 | /// enumerator-list ',' enumerator |
| 3514 | /// enumerator: |
| 3515 | /// enumeration-constant |
| 3516 | /// enumeration-constant '=' constant-expression |
| 3517 | /// enumeration-constant: |
| 3518 | /// identifier |
| 3519 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3520 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3521 | // Enter the scope of the enum body and start the definition. |
| 3522 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3523 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3524 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3525 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3526 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3527 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3528 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3529 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3530 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3531 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3532 | SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3533 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3534 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3535 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3536 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3537 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3538 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3539 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3540 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3541 | // If attributes exist after the enumerator, parse them. |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3542 | ParsedAttributesWithRange attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3543 | MaybeParseGNUAttributes(attrs); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3544 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | 2edf0a2 | 2012-06-23 05:07:58 +0000 | [diff] [blame] | 3545 | ProhibitAttributes(attrs); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3546 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3547 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3548 | ExprResult AssignedVal; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 3549 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3550 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3551 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3552 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3553 | AssignedVal = ParseConstantExpression(); |
| 3554 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3555 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3556 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3557 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3558 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3559 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3560 | LastEnumConstDecl, |
| 3561 | IdentLoc, Ident, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3562 | attrs.getList(), EqualLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3563 | AssignedVal.release()); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3564 | PD.complete(EnumConstDecl); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3565 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3566 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3567 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3568 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3569 | if (Tok.is(tok::identifier)) { |
| 3570 | // We're missing a comma between enumerators. |
| 3571 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3572 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3573 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3574 | continue; |
| 3575 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3576 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3577 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3578 | break; |
| 3579 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3580 | |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3581 | if (Tok.isNot(tok::identifier)) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3582 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11) |
Richard Smith | eab9d6f | 2012-07-23 05:45:25 +0000 | [diff] [blame] | 3583 | Diag(CommaLoc, getLangOpts().CPlusPlus ? |
| 3584 | diag::ext_enumerator_list_comma_cxx : |
| 3585 | diag::ext_enumerator_list_comma_c) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3586 | << FixItHint::CreateRemoval(CommaLoc); |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3587 | else if (getLangOpts().CPlusPlus11) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3588 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3589 | << FixItHint::CreateRemoval(CommaLoc); |
| 3590 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3591 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3592 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3593 | // Eat the }. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3594 | T.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3595 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3596 | // If attributes exist after the identifier list, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3597 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3598 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3599 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3600 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
| 3601 | EnumDecl, EnumConstantDecls.data(), |
| 3602 | EnumConstantDecls.size(), getCurScope(), |
| 3603 | attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3604 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3605 | EnumScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3606 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3607 | T.getCloseLocation()); |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3608 | |
| 3609 | // The next token must be valid after an enum definition. If not, a ';' |
| 3610 | // was probably forgotten. |
Richard Smith | 139be70 | 2012-07-02 19:14:01 +0000 | [diff] [blame] | 3611 | bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope; |
| 3612 | if (!isValidAfterTypeSpecifier(CanBeBitfield)) { |
Richard Smith | c9f3517 | 2012-06-25 21:37:02 +0000 | [diff] [blame] | 3613 | ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum"); |
| 3614 | // Push this token back into the preprocessor and change our current token |
| 3615 | // to ';' so that the rest of the code recovers as though there were an |
| 3616 | // ';' after the definition. |
| 3617 | PP.EnterToken(Tok); |
| 3618 | Tok.setKind(tok::semi); |
| 3619 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3620 | } |
| 3621 | |
| 3622 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3623 | /// start of a type-qualifier-list. |
| 3624 | bool Parser::isTypeQualifier() const { |
| 3625 | switch (Tok.getKind()) { |
| 3626 | default: return false; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3627 | |
| 3628 | // type-qualifier only in OpenCL |
| 3629 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3630 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3631 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3632 | // type-qualifier |
| 3633 | case tok::kw_const: |
| 3634 | case tok::kw_volatile: |
| 3635 | case tok::kw_restrict: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3636 | case tok::kw___private: |
| 3637 | case tok::kw___local: |
| 3638 | case tok::kw___global: |
| 3639 | case tok::kw___constant: |
| 3640 | case tok::kw___read_only: |
| 3641 | case tok::kw___read_write: |
| 3642 | case tok::kw___write_only: |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3643 | return true; |
| 3644 | } |
| 3645 | } |
| 3646 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3647 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3648 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3649 | /// specifier or if we're not sure. |
| 3650 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3651 | switch (Tok.getKind()) { |
| 3652 | default: return false; |
| 3653 | // type-specifiers |
| 3654 | case tok::kw_short: |
| 3655 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3656 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3657 | case tok::kw___int128: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3658 | case tok::kw_signed: |
| 3659 | case tok::kw_unsigned: |
| 3660 | case tok::kw__Complex: |
| 3661 | case tok::kw__Imaginary: |
| 3662 | case tok::kw_void: |
| 3663 | case tok::kw_char: |
| 3664 | case tok::kw_wchar_t: |
| 3665 | case tok::kw_char16_t: |
| 3666 | case tok::kw_char32_t: |
| 3667 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3668 | case tok::kw_half: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3669 | case tok::kw_float: |
| 3670 | case tok::kw_double: |
| 3671 | case tok::kw_bool: |
| 3672 | case tok::kw__Bool: |
| 3673 | case tok::kw__Decimal32: |
| 3674 | case tok::kw__Decimal64: |
| 3675 | case tok::kw__Decimal128: |
| 3676 | case tok::kw___vector: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3677 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3678 | // OpenCL specific types: |
| 3679 | case tok::kw_image1d_t: |
| 3680 | case tok::kw_image1d_array_t: |
| 3681 | case tok::kw_image1d_buffer_t: |
| 3682 | case tok::kw_image2d_t: |
| 3683 | case tok::kw_image2d_array_t: |
| 3684 | case tok::kw_image3d_t: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3685 | case tok::kw_sampler_t: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3686 | case tok::kw_event_t: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3687 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3688 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3689 | case tok::kw_class: |
| 3690 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3691 | case tok::kw___interface: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3692 | case tok::kw_union: |
| 3693 | // enum-specifier |
| 3694 | case tok::kw_enum: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3695 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3696 | // typedef-name |
| 3697 | case tok::annot_typename: |
| 3698 | return true; |
| 3699 | } |
| 3700 | } |
| 3701 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3702 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3703 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3704 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3705 | switch (Tok.getKind()) { |
| 3706 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3707 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3708 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3709 | if (TryAltiVecVectorToken()) |
| 3710 | return true; |
| 3711 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3712 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3713 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3714 | // recurse to handle whatever we get. |
| 3715 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3716 | return true; |
| 3717 | if (Tok.is(tok::identifier)) |
| 3718 | return false; |
| 3719 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3720 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3721 | case tok::coloncolon: // ::foo::bar |
| 3722 | if (NextToken().is(tok::kw_new) || // ::new |
| 3723 | NextToken().is(tok::kw_delete)) // ::delete |
| 3724 | return false; |
| 3725 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3726 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3727 | return true; |
| 3728 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3729 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3730 | // GNU attributes support. |
| 3731 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3732 | // GNU typeof support. |
| 3733 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3734 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3735 | // type-specifiers |
| 3736 | case tok::kw_short: |
| 3737 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3738 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3739 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3740 | case tok::kw_signed: |
| 3741 | case tok::kw_unsigned: |
| 3742 | case tok::kw__Complex: |
| 3743 | case tok::kw__Imaginary: |
| 3744 | case tok::kw_void: |
| 3745 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3746 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3747 | case tok::kw_char16_t: |
| 3748 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3749 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3750 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3751 | case tok::kw_float: |
| 3752 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3753 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3754 | case tok::kw__Bool: |
| 3755 | case tok::kw__Decimal32: |
| 3756 | case tok::kw__Decimal64: |
| 3757 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3758 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3759 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3760 | // OpenCL specific types: |
| 3761 | case tok::kw_image1d_t: |
| 3762 | case tok::kw_image1d_array_t: |
| 3763 | case tok::kw_image1d_buffer_t: |
| 3764 | case tok::kw_image2d_t: |
| 3765 | case tok::kw_image2d_array_t: |
| 3766 | case tok::kw_image3d_t: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3767 | case tok::kw_sampler_t: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3768 | case tok::kw_event_t: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3769 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3770 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3771 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3772 | case tok::kw_struct: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3773 | case tok::kw___interface: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3774 | case tok::kw_union: |
| 3775 | // enum-specifier |
| 3776 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3777 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3778 | // type-qualifier |
| 3779 | case tok::kw_const: |
| 3780 | case tok::kw_volatile: |
| 3781 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3782 | |
John McCall | b8a8de3 | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 3783 | // Debugger support. |
| 3784 | case tok::kw___unknown_anytype: |
| 3785 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3786 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 3787 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3788 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3789 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 3790 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3791 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3792 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3793 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3794 | case tok::kw___cdecl: |
| 3795 | case tok::kw___stdcall: |
| 3796 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3797 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3798 | case tok::kw___w64: |
| 3799 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3800 | case tok::kw___ptr32: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3801 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3802 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3803 | |
| 3804 | case tok::kw___private: |
| 3805 | case tok::kw___local: |
| 3806 | case tok::kw___global: |
| 3807 | case tok::kw___constant: |
| 3808 | case tok::kw___read_only: |
| 3809 | case tok::kw___read_write: |
| 3810 | case tok::kw___write_only: |
| 3811 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3812 | return true; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3813 | |
| 3814 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3815 | return getLangOpts().OpenCL; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3816 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3817 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3818 | case tok::kw__Atomic: |
| 3819 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3820 | } |
| 3821 | } |
| 3822 | |
| 3823 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 3824 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3825 | /// |
| 3826 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 3827 | /// this check is to disambiguate between an expression and a declaration. |
| 3828 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3829 | switch (Tok.getKind()) { |
| 3830 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3831 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3832 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3833 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3834 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3835 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3836 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3837 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3838 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3839 | if (TryAltiVecVectorToken()) |
| 3840 | return true; |
| 3841 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3842 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3843 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3844 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3845 | // recurse to handle whatever we get. |
| 3846 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3847 | return true; |
| 3848 | if (Tok.is(tok::identifier)) |
| 3849 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3850 | |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3851 | // 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] | 3852 | // by an identifier and then either ':' or ']', in a place where an |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3853 | // expression is permitted, then this is probably a class message send |
| 3854 | // missing the initial '['. In this case, we won't consider this to be |
| 3855 | // the start of a declaration. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3856 | if (DisambiguatingWithExpression && |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3857 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 3858 | return false; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3859 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3860 | return isDeclarationSpecifier(); |
| 3861 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3862 | case tok::coloncolon: // ::foo::bar |
| 3863 | if (NextToken().is(tok::kw_new) || // ::new |
| 3864 | NextToken().is(tok::kw_delete)) // ::delete |
| 3865 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3866 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3867 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3868 | // recurse to handle whatever we get. |
| 3869 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3870 | return true; |
| 3871 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3872 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3873 | // storage-class-specifier |
| 3874 | case tok::kw_typedef: |
| 3875 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 3876 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3877 | case tok::kw_static: |
| 3878 | case tok::kw_auto: |
| 3879 | case tok::kw_register: |
| 3880 | case tok::kw___thread: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3881 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 3882 | // Modules |
| 3883 | case tok::kw___module_private__: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3884 | |
John McCall | b8a8de3 | 2012-11-14 00:49:39 +0000 | [diff] [blame] | 3885 | // Debugger support |
| 3886 | case tok::kw___unknown_anytype: |
| 3887 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3888 | // type-specifiers |
| 3889 | case tok::kw_short: |
| 3890 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3891 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3892 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3893 | case tok::kw_signed: |
| 3894 | case tok::kw_unsigned: |
| 3895 | case tok::kw__Complex: |
| 3896 | case tok::kw__Imaginary: |
| 3897 | case tok::kw_void: |
| 3898 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3899 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3900 | case tok::kw_char16_t: |
| 3901 | case tok::kw_char32_t: |
| 3902 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3903 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3904 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3905 | case tok::kw_float: |
| 3906 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3907 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3908 | case tok::kw__Bool: |
| 3909 | case tok::kw__Decimal32: |
| 3910 | case tok::kw__Decimal64: |
| 3911 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3912 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3913 | |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3914 | // OpenCL specific types: |
| 3915 | case tok::kw_image1d_t: |
| 3916 | case tok::kw_image1d_array_t: |
| 3917 | case tok::kw_image1d_buffer_t: |
| 3918 | case tok::kw_image2d_t: |
| 3919 | case tok::kw_image2d_array_t: |
| 3920 | case tok::kw_image3d_t: |
Guy Benyei | 21f18c4 | 2013-02-07 10:55:47 +0000 | [diff] [blame] | 3921 | case tok::kw_sampler_t: |
Guy Benyei | e6b9d80 | 2013-01-20 12:31:11 +0000 | [diff] [blame] | 3922 | case tok::kw_event_t: |
Guy Benyei | b13621d | 2012-12-18 14:38:23 +0000 | [diff] [blame] | 3923 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3924 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3925 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3926 | case tok::kw_struct: |
| 3927 | case tok::kw_union: |
Joao Matos | 6666ed4 | 2012-08-31 18:45:21 +0000 | [diff] [blame] | 3928 | case tok::kw___interface: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3929 | // enum-specifier |
| 3930 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3931 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3932 | // type-qualifier |
| 3933 | case tok::kw_const: |
| 3934 | case tok::kw_volatile: |
| 3935 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3936 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3937 | // function-specifier |
| 3938 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3939 | case tok::kw_virtual: |
| 3940 | case tok::kw_explicit: |
Richard Smith | de03c15 | 2013-01-17 22:16:11 +0000 | [diff] [blame] | 3941 | case tok::kw__Noreturn: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3942 | |
Richard Smith | 4cd81c5 | 2013-01-29 09:02:09 +0000 | [diff] [blame] | 3943 | // alignment-specifier |
| 3944 | case tok::kw__Alignas: |
| 3945 | |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 3946 | // friend keyword. |
| 3947 | case tok::kw_friend: |
| 3948 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 3949 | // static_assert-declaration |
| 3950 | case tok::kw__Static_assert: |
| 3951 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3952 | // GNU typeof support. |
| 3953 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3954 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3955 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3956 | case tok::kw___attribute: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3957 | |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 3958 | // C++11 decltype and constexpr. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3959 | case tok::annot_decltype: |
Richard Smith | 53aec2a | 2012-10-25 00:00:53 +0000 | [diff] [blame] | 3960 | case tok::kw_constexpr: |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3961 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3962 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3963 | case tok::kw__Atomic: |
| 3964 | return true; |
| 3965 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 3966 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3967 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3968 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3969 | |
Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 3970 | // typedef-name |
| 3971 | case tok::annot_typename: |
| 3972 | return !DisambiguatingWithExpression || |
| 3973 | !isStartOfObjCClassMessageMissingOpenBracket(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 3974 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 3975 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3976 | case tok::kw___cdecl: |
| 3977 | case tok::kw___stdcall: |
| 3978 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3979 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3980 | case tok::kw___w64: |
| 3981 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3982 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3983 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3984 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3985 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3986 | |
| 3987 | case tok::kw___private: |
| 3988 | case tok::kw___local: |
| 3989 | case tok::kw___global: |
| 3990 | case tok::kw___constant: |
| 3991 | case tok::kw___read_only: |
| 3992 | case tok::kw___read_write: |
| 3993 | case tok::kw___write_only: |
| 3994 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3995 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3996 | } |
| 3997 | } |
| 3998 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3999 | bool Parser::isConstructorDeclarator() { |
| 4000 | TentativeParsingAction TPA(*this); |
| 4001 | |
| 4002 | // Parse the C++ scope specifier. |
| 4003 | CXXScopeSpec SS; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4004 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4005 | /*EnteringContext=*/true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4006 | TPA.Revert(); |
| 4007 | return false; |
| 4008 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4009 | |
| 4010 | // Parse the constructor name. |
| 4011 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 4012 | // We already know that we have a constructor name; just consume |
| 4013 | // the token. |
| 4014 | ConsumeToken(); |
| 4015 | } else { |
| 4016 | TPA.Revert(); |
| 4017 | return false; |
| 4018 | } |
| 4019 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4020 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4021 | if (Tok.isNot(tok::l_paren)) { |
| 4022 | TPA.Revert(); |
| 4023 | return false; |
| 4024 | } |
| 4025 | ConsumeParen(); |
| 4026 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4027 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 4028 | // that we have a constructor. |
| 4029 | if (Tok.is(tok::r_paren) || |
| 4030 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4031 | TPA.Revert(); |
| 4032 | return true; |
| 4033 | } |
| 4034 | |
| 4035 | // If we need to, enter the specified scope. |
| 4036 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4037 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4038 | DeclScopeObj.EnterDeclaratorScope(); |
| 4039 | |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4040 | // Optionally skip Microsoft attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4041 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 4042 | MaybeParseMicrosoftAttributes(Attrs); |
| 4043 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4044 | // Check whether the next token(s) are part of a declaration |
| 4045 | // specifier, in which case we have the start of a parameter and, |
| 4046 | // therefore, we know that this is a constructor. |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4047 | bool IsConstructor = false; |
| 4048 | if (isDeclarationSpecifier()) |
| 4049 | IsConstructor = true; |
| 4050 | else if (Tok.is(tok::identifier) || |
| 4051 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 4052 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 4053 | // This might be a parenthesized member name, but is more likely to |
| 4054 | // be a constructor declaration with an invalid argument type. Keep |
| 4055 | // looking. |
| 4056 | if (Tok.is(tok::annot_cxxscope)) |
| 4057 | ConsumeToken(); |
| 4058 | ConsumeToken(); |
| 4059 | |
| 4060 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4061 | // which must have one of the following syntactic forms (see the |
| 4062 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 4063 | switch (Tok.getKind()) { |
| 4064 | case tok::l_paren: |
| 4065 | // C(X ( int)); |
| 4066 | case tok::l_square: |
| 4067 | // C(X [ 5]); |
| 4068 | // C(X [ [attribute]]); |
| 4069 | case tok::coloncolon: |
| 4070 | // C(X :: Y); |
| 4071 | // C(X :: *p); |
| 4072 | case tok::r_paren: |
| 4073 | // C(X ) |
| 4074 | // Assume this isn't a constructor, rather than assuming it's a |
| 4075 | // constructor with an unnamed parameter of an ill-formed type. |
| 4076 | break; |
| 4077 | |
| 4078 | default: |
| 4079 | IsConstructor = true; |
| 4080 | break; |
| 4081 | } |
| 4082 | } |
| 4083 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4084 | TPA.Revert(); |
| 4085 | return IsConstructor; |
| 4086 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4087 | |
| 4088 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4089 | /// type-qualifier-list: [C99 6.7.5] |
| 4090 | /// type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4091 | /// [vendor] attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4092 | /// [ only if VendorAttributesAllowed=true ] |
| 4093 | /// type-qualifier-list type-qualifier |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4094 | /// [vendor] type-qualifier-list attributes |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4095 | /// [ only if VendorAttributesAllowed=true ] |
| 4096 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4097 | /// [ only if CXX11AttributesAllowed=true ] |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4098 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4099 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4100 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 4101 | bool VendorAttributesAllowed, |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4102 | bool CXX11AttributesAllowed) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4103 | if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed && |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4104 | isCXX11AttributeSpecifier()) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4105 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 4106 | ParseCXX11Attributes(attrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4107 | DS.takeAttributesFrom(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4108 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4109 | |
| 4110 | SourceLocation EndLoc; |
| 4111 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4112 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4113 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4114 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4115 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4116 | SourceLocation Loc = Tok.getLocation(); |
| 4117 | |
| 4118 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 4119 | case tok::code_completion: |
| 4120 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 4121 | return cutOffParsing(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4122 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4123 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4124 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4125 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4126 | break; |
| 4127 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4128 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4129 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4130 | break; |
| 4131 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4132 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
Richard Smith | d654f2d | 2012-10-17 23:31:46 +0000 | [diff] [blame] | 4133 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4134 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4135 | |
| 4136 | // OpenCL qualifiers: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4137 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4138 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 4139 | goto DoneWithTypeQuals; |
| 4140 | case tok::kw___private: |
| 4141 | case tok::kw___global: |
| 4142 | case tok::kw___local: |
| 4143 | case tok::kw___constant: |
| 4144 | case tok::kw___read_only: |
| 4145 | case tok::kw___write_only: |
| 4146 | case tok::kw___read_write: |
| 4147 | ParseOpenCLQualifiers(DS); |
| 4148 | break; |
| 4149 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4150 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 4151 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4152 | case tok::kw___ptr32: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4153 | case tok::kw___cdecl: |
| 4154 | case tok::kw___stdcall: |
| 4155 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4156 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4157 | case tok::kw___unaligned: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4158 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4159 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4160 | continue; |
| 4161 | } |
| 4162 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4163 | case tok::kw___pascal: |
| 4164 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4165 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4166 | continue; |
| 4167 | } |
| 4168 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4169 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4170 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4171 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4172 | continue; // do *not* consume the next token! |
| 4173 | } |
| 4174 | // otherwise, FALL THROUGH! |
| 4175 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4176 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4177 | // If this is not a type-qualifier token, we're done reading type |
| 4178 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 4179 | DS.Finish(Diags, PP); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4180 | if (EndLoc.isValid()) |
| 4181 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4182 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4183 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4184 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4185 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 4186 | if (isInvalid) { |
| 4187 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4188 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4189 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 4190 | EndLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4191 | } |
| 4192 | } |
| 4193 | |
| 4194 | |
| 4195 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 4196 | /// |
| 4197 | void Parser::ParseDeclarator(Declarator &D) { |
| 4198 | /// This implements the 'declarator' production in the C grammar, then checks |
| 4199 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4200 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4201 | } |
| 4202 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4203 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) { |
| 4204 | if (Kind == tok::star || Kind == tok::caret) |
| 4205 | return true; |
| 4206 | |
| 4207 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 4208 | if (!Lang.CPlusPlus) |
| 4209 | return false; |
| 4210 | |
| 4211 | return Kind == tok::amp || Kind == tok::ampamp; |
| 4212 | } |
| 4213 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4214 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 4215 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 4216 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4217 | /// ptr-operator production. |
| 4218 | /// |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4219 | /// 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] | 4220 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 4221 | /// isConstructorDeclarator. |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 4222 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4223 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 4224 | /// [C] pointer[opt] direct-declarator |
| 4225 | /// [C++] direct-declarator |
| 4226 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4227 | /// |
| 4228 | /// pointer: [C99 6.7.5] |
| 4229 | /// '*' type-qualifier-list[opt] |
| 4230 | /// '*' type-qualifier-list[opt] pointer |
| 4231 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4232 | /// ptr-operator: |
| 4233 | /// '*' cv-qualifier-seq[opt] |
| 4234 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4235 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4236 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4237 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4238 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4239 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 4240 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 4241 | if (Diags.hasAllExtensionsSilenced()) |
| 4242 | D.setExtension(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4243 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4244 | // C++ member pointers start with a '::' or a nested-name. |
| 4245 | // Member pointers get special handling, since there's no place for the |
| 4246 | // scope spec in the generic path below. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4247 | if (getLangOpts().CPlusPlus && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 4248 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 4249 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4250 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4251 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4252 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4253 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4254 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 4255 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4256 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4257 | // The scope spec really belongs to the direct-declarator. |
Richard Smith | 6a502c4 | 2013-01-08 22:43:49 +0000 | [diff] [blame] | 4258 | if (D.mayHaveIdentifier()) |
| 4259 | D.getCXXScopeSpec() = SS; |
| 4260 | else |
| 4261 | AnnotateScopeToken(SS, true); |
| 4262 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4263 | if (DirectDeclParser) |
| 4264 | (this->*DirectDeclParser)(D); |
| 4265 | return; |
| 4266 | } |
| 4267 | |
| 4268 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4269 | D.SetRangeEnd(Loc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4270 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4271 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4272 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4273 | |
| 4274 | // Recurse to parse whatever is left. |
| 4275 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 4276 | |
| 4277 | // Sema will have to catch (syntactically invalid) pointers into global |
| 4278 | // scope. It has to catch pointers into namespace scope anyway. |
| 4279 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4280 | Loc), |
| 4281 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4282 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4283 | return; |
| 4284 | } |
| 4285 | } |
| 4286 | |
| 4287 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4288 | // Not a pointer, C++ reference, or block. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4289 | if (!isPtrOperatorToken(Kind, getLangOpts())) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4290 | if (DirectDeclParser) |
| 4291 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4292 | return; |
| 4293 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4294 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4295 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 4296 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4297 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4298 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4299 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 4300 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4301 | // Is a pointer. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4302 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4303 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4304 | // FIXME: GNU attributes are not allowed here in a new-type-id. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4305 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4306 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 4307 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4308 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4309 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4310 | if (Kind == tok::star) |
| 4311 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 4312 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 4313 | DS.getConstSpecLoc(), |
| 4314 | DS.getVolatileSpecLoc(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4315 | DS.getRestrictSpecLoc()), |
| 4316 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4317 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 4318 | else |
| 4319 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4320 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4321 | Loc), |
| 4322 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4323 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4324 | } else { |
| 4325 | // Is a reference |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4326 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4327 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4328 | // Complain about rvalue references in C++03, but then go on and build |
| 4329 | // the declarator. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4330 | if (Kind == tok::ampamp) |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4331 | Diag(Loc, getLangOpts().CPlusPlus11 ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4332 | diag::warn_cxx98_compat_rvalue_reference : |
| 4333 | diag::ext_rvalue_reference); |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 4334 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4335 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 4336 | ParseTypeQualifierListOpt(DS); |
| 4337 | D.ExtendWithDeclSpec(DS); |
| 4338 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4339 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 4340 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 4341 | // template type argument, in which case the cv-qualifiers are ignored. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4342 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 4343 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 4344 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4345 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4346 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 4347 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4348 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4349 | } |
| 4350 | |
| 4351 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4352 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4353 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4354 | if (D.getNumTypeObjects() > 0) { |
| 4355 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 4356 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 4357 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 4358 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 4359 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4360 | << II; |
| 4361 | else |
| 4362 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 4363 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4364 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4365 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 4366 | // can go ahead and build the (technically ill-formed) |
| 4367 | // declarator: reference collapsing will take care of it. |
| 4368 | } |
| 4369 | } |
| 4370 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4371 | // Remember that we parsed a reference type. It doesn't have type-quals. |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 4372 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 4373 | Kind == tok::amp), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4374 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4375 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4376 | } |
| 4377 | } |
| 4378 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4379 | static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D, |
| 4380 | SourceLocation EllipsisLoc) { |
| 4381 | if (EllipsisLoc.isValid()) { |
| 4382 | FixItHint Insertion; |
| 4383 | if (!D.getEllipsisLoc().isValid()) { |
| 4384 | Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "..."); |
| 4385 | D.setEllipsisLoc(EllipsisLoc); |
| 4386 | } |
| 4387 | P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) |
| 4388 | << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName(); |
| 4389 | } |
| 4390 | } |
| 4391 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4392 | /// ParseDirectDeclarator |
| 4393 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4394 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4395 | /// '(' declarator ')' |
| 4396 | /// [GNU] '(' attributes declarator ')' |
| 4397 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4398 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4399 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4400 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4401 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4402 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 4403 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4404 | /// direct-declarator '(' parameter-type-list ')' |
| 4405 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4406 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4407 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 4408 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 4409 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4410 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 4411 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 4412 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 4413 | /// [C++] declarator-id |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4414 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4415 | /// |
| 4416 | /// declarator-id: [C++ 8] |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4417 | /// '...'[opt] id-expression |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4418 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 4419 | /// |
| 4420 | /// id-expression: [C++ 5.1] |
| 4421 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4422 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 4423 | /// |
| 4424 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4425 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4426 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 4427 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4428 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 4429 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 4430 | /// |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 4431 | /// Note, any additional constructs added here may need corresponding changes |
| 4432 | /// in isConstructorDeclarator. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4433 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4434 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4435 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4436 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4437 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4438 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4439 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 4440 | D.getContext() == Declarator::MemberContext; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4441 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 4442 | EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 4443 | } |
| 4444 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4445 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 4446 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 4447 | // Change the declaration context for name lookup, until this function |
| 4448 | // is exited (and the declarator has been parsed). |
| 4449 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4450 | } |
| 4451 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4452 | // C++0x [dcl.fct]p14: |
| 4453 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4454 | // of a parameter-declaration-clause without a preceding comma. In |
| 4455 | // this case, the ellipsis is parsed as part of the |
| 4456 | // abstract-declarator if the type of the parameter names a template |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4457 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 4458 | // as part of the parameter-declaration-clause. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4459 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4460 | !((D.getContext() == Declarator::PrototypeContext || |
| 4461 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 4462 | NextToken().is(tok::r_paren) && |
Richard Smith | 30f2a74 | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4463 | !D.hasGroupingParens() && |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4464 | !Actions.containsUnexpandedParameterPacks(D))) { |
| 4465 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 4466 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) { |
| 4467 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 4468 | // the user what they should have done. |
| 4469 | ParseDeclarator(D); |
| 4470 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4471 | return; |
| 4472 | } else |
| 4473 | D.setEllipsisLoc(EllipsisLoc); |
| 4474 | |
| 4475 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 4476 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 4477 | // the l_paren token. |
| 4478 | } |
| 4479 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4480 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 4481 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 4482 | // We found something that indicates the start of an unqualified-id. |
| 4483 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4484 | bool AllowConstructorName; |
| 4485 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 4486 | AllowConstructorName = false; |
| 4487 | else if (D.getCXXScopeSpec().isSet()) |
| 4488 | AllowConstructorName = |
| 4489 | (D.getContext() == Declarator::FileContext || |
Dmitri Gribenko | 1b9e8f7 | 2013-02-12 17:27:41 +0000 | [diff] [blame] | 4490 | D.getContext() == Declarator::MemberContext); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4491 | else |
| 4492 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 4493 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4494 | SourceLocation TemplateKWLoc; |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4495 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 4496 | /*EnteringContext=*/true, |
| 4497 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4498 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4499 | ParsedType(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4500 | TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4501 | D.getName()) || |
| 4502 | // Once we're past the identifier, if the scope was bad, mark the |
| 4503 | // whole declarator bad. |
| 4504 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4505 | D.SetIdentifier(0, Tok.getLocation()); |
| 4506 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4507 | } else { |
| 4508 | // Parsed the unqualified-id; update range information and move along. |
| 4509 | if (D.getSourceRange().getBegin().isInvalid()) |
| 4510 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 4511 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4512 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4513 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4514 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4515 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4516 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4517 | "There's a C++-specific check for tok::identifier above"); |
| 4518 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 4519 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 4520 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4521 | goto PastIdentifier; |
| 4522 | } |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4523 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4524 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4525 | // direct-declarator: '(' declarator ')' |
| 4526 | // direct-declarator: '(' attributes declarator ')' |
| 4527 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 4528 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4529 | |
| 4530 | // If the declarator was parenthesized, we entered the declarator |
| 4531 | // scope when parsing the parenthesized declarator, then exited |
| 4532 | // the scope already. Re-enter the scope, if we need to. |
| 4533 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4534 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4535 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4536 | if (!D.isInvalidType() && |
| 4537 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4538 | // Change the declaration context for name lookup, until this function |
| 4539 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4540 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4541 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4542 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4543 | // This could be something simple like "int" (in which case the declarator |
| 4544 | // portion is empty), if an abstract-declarator is allowed. |
| 4545 | D.SetIdentifier(0, Tok.getLocation()); |
Richard Smith | 30f2a74 | 2013-02-20 20:19:27 +0000 | [diff] [blame] | 4546 | |
| 4547 | // The grammar for abstract-pack-declarator does not allow grouping parens. |
| 4548 | // FIXME: Revisit this once core issue 1488 is resolved. |
| 4549 | if (D.hasEllipsis() && D.hasGroupingParens()) |
| 4550 | Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()), |
| 4551 | diag::ext_abstract_pack_declarator_parens); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4552 | } else { |
David Blaikie | e75d9cf | 2012-06-29 22:03:56 +0000 | [diff] [blame] | 4553 | if (Tok.getKind() == tok::annot_pragma_parser_crash) |
David Blaikie | 377da4c | 2012-08-21 18:56:49 +0000 | [diff] [blame] | 4554 | LLVM_BUILTIN_TRAP; |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 4555 | if (D.getContext() == Declarator::MemberContext) |
| 4556 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 4557 | << D.getDeclSpec().getSourceRange(); |
Richard Trieu | db55c04c | 2013-01-26 02:31:38 +0000 | [diff] [blame] | 4558 | else if (getLangOpts().CPlusPlus) { |
| 4559 | if (Tok.is(tok::period) || Tok.is(tok::arrow)) |
| 4560 | Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow); |
| 4561 | else |
| 4562 | Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus; |
| 4563 | } else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4564 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4565 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 4566 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4567 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4568 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4569 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4570 | assert(D.isPastIdentifier() && |
| 4571 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4572 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4573 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 4574 | if (D.hasName() && !D.getNumTypeObjects()) |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4575 | MaybeParseCXX11Attributes(D); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4576 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4577 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4578 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4579 | // Enter function-declaration scope, limiting any declarators to the |
| 4580 | // function prototype scope, including parameter declarators. |
| 4581 | ParseScope PrototypeScope(this, |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4582 | Scope::FunctionPrototypeScope|Scope::DeclScope| |
| 4583 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 4584 | ? Scope::FunctionDeclarationScope : 0)); |
| 4585 | |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4586 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 4587 | // In such a case, check if we actually have a function declarator; if it |
| 4588 | // is not, the declarator has been fully parsed. |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4589 | bool IsAmbiguous = false; |
Richard Smith | 0576681 | 2012-08-18 00:55:03 +0000 | [diff] [blame] | 4590 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
| 4591 | // The name of the declarator, if any, is tentatively declared within |
| 4592 | // a possible direct initializer. |
| 4593 | TentativelyDeclaredIdentifiers.push_back(D.getIdentifier()); |
| 4594 | bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous); |
| 4595 | TentativelyDeclaredIdentifiers.pop_back(); |
| 4596 | if (!IsFunctionDecl) |
| 4597 | break; |
| 4598 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4599 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4600 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4601 | T.consumeOpen(); |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4602 | ParseFunctionDeclarator(D, attrs, T, IsAmbiguous); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4603 | PrototypeScope.Exit(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4604 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4605 | ParseBracketDeclarator(D); |
| 4606 | } else { |
| 4607 | break; |
| 4608 | } |
| 4609 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4610 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4611 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4612 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 4613 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4614 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4615 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 4616 | /// |
| 4617 | /// direct-declarator: |
| 4618 | /// '(' declarator ')' |
| 4619 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4620 | /// direct-declarator '(' parameter-type-list ')' |
| 4621 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4622 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4623 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4624 | /// |
| 4625 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4626 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4627 | T.consumeOpen(); |
| 4628 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4629 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4630 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4631 | // Eat any attributes before we look at whether this is a grouping or function |
| 4632 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 4633 | // the type being built up, for example: |
| 4634 | // int (__attribute__(()) *x)(long y) |
| 4635 | // If this ends up not being a grouping paren, the attribute applies to the |
| 4636 | // first argument, for example: |
| 4637 | // int (__attribute__(()) int x) |
| 4638 | // In either case, we need to eat any attributes to be able to determine what |
| 4639 | // sort of paren this is. |
| 4640 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4641 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4642 | bool RequiresArg = false; |
| 4643 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4644 | ParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4645 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4646 | // We require that the argument list (if this is a non-grouping paren) be |
| 4647 | // present even if the attribute list was empty. |
| 4648 | RequiresArg = true; |
| 4649 | } |
Chad Rosier | 9cab1c9 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 4650 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4651 | // Eat any Microsoft extensions. |
Chad Rosier | 9cab1c9 | 2012-12-21 21:22:20 +0000 | [diff] [blame] | 4652 | ParseMicrosoftTypeAttributes(attrs); |
| 4653 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4654 | // Eat any Borland extensions. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 4655 | if (Tok.is(tok::kw___pascal)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4656 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4657 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4658 | // If we haven't past the identifier yet (or where the identifier would be |
| 4659 | // stored, if this is an abstract declarator), then this is probably just |
| 4660 | // grouping parens. However, if this could be an abstract-declarator, then |
| 4661 | // this could also be the start of function arguments (consider 'void()'). |
| 4662 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4663 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4664 | if (!D.mayOmitIdentifier()) { |
| 4665 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 4666 | // paren, because we haven't seen the identifier yet. |
| 4667 | isGrouping = true; |
| 4668 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4669 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 4670 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4671 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 4672 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4673 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 4674 | // considered to be a type, not a K&R identifier-list. |
| 4675 | isGrouping = false; |
| 4676 | } else { |
| 4677 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 4678 | isGrouping = true; |
| 4679 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4680 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4681 | // If this is a grouping paren, handle: |
| 4682 | // direct-declarator: '(' declarator ')' |
| 4683 | // direct-declarator: '(' attributes declarator ')' |
| 4684 | if (isGrouping) { |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4685 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 4686 | D.setEllipsisLoc(SourceLocation()); |
| 4687 | |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4688 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4689 | D.setGroupingParens(true); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4690 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4691 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4692 | T.consumeClose(); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4693 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4694 | T.getCloseLocation()), |
| 4695 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4696 | |
| 4697 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4698 | |
| 4699 | // An ellipsis cannot be placed outside parentheses. |
| 4700 | if (EllipsisLoc.isValid()) |
| 4701 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4702 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4703 | return; |
| 4704 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4705 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4706 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 4707 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4708 | // identifier (and remember where it would have been), then call into |
| 4709 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4710 | D.SetIdentifier(0, Tok.getLocation()); |
| 4711 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4712 | // Enter function-declaration scope, limiting any declarators to the |
| 4713 | // function prototype scope, including parameter declarators. |
| 4714 | ParseScope PrototypeScope(this, |
Richard Smith | 3a2b7a1 | 2013-01-28 22:42:45 +0000 | [diff] [blame] | 4715 | Scope::FunctionPrototypeScope | Scope::DeclScope | |
| 4716 | (D.isFunctionDeclaratorAFunctionDeclaration() |
| 4717 | ? Scope::FunctionDeclarationScope : 0)); |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4718 | ParseFunctionDeclarator(D, attrs, T, false, RequiresArg); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4719 | PrototypeScope.Exit(); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4720 | } |
| 4721 | |
| 4722 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 4723 | /// declarator D up to a paren, which indicates that we are parsing function |
| 4724 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4725 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4726 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 4727 | /// immediately after the open paren - they should be considered to be the |
| 4728 | /// first argument of a parameter. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4729 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4730 | /// If RequiresArg is true, then the first argument of the function is required |
| 4731 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4732 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4733 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 4734 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 4735 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 4736 | /// |
| 4737 | /// [C++11] exception-specification: |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4738 | /// dynamic-exception-specification |
| 4739 | /// noexcept-specification |
| 4740 | /// |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4741 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4742 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4743 | BalancedDelimiterTracker &Tracker, |
Richard Smith | b9c6261 | 2012-07-30 21:30:52 +0000 | [diff] [blame] | 4744 | bool IsAmbiguous, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4745 | bool RequiresArg) { |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4746 | assert(getCurScope()->isFunctionPrototypeScope() && |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4747 | "Should call from a Function scope"); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4748 | // lparen is already consumed! |
| 4749 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 4750 | |
| 4751 | // This should be true when the function has typed arguments. |
| 4752 | // Otherwise, it is treated as a K&R-style function. |
| 4753 | bool HasProto = false; |
| 4754 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4755 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4756 | // Remember where we see an ellipsis, if any. |
| 4757 | SourceLocation EllipsisLoc; |
| 4758 | |
| 4759 | DeclSpec DS(AttrFactory); |
| 4760 | bool RefQualifierIsLValueRef = true; |
| 4761 | SourceLocation RefQualifierLoc; |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4762 | SourceLocation ConstQualifierLoc; |
| 4763 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4764 | ExceptionSpecificationType ESpecType = EST_None; |
| 4765 | SourceRange ESpecRange; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4766 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 4767 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4768 | ExprResult NoexceptExpr; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4769 | ParsedAttributes FnAttrs(AttrFactory); |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 4770 | TypeResult TrailingReturnType; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4771 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4772 | Actions.ActOnStartFunctionDeclarator(); |
| 4773 | |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4774 | /* LocalEndLoc is the end location for the local FunctionTypeLoc. |
| 4775 | EndLoc is the end location for the function declarator. |
| 4776 | They differ for trailing return types. */ |
| 4777 | SourceLocation StartLoc, LocalEndLoc, EndLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4778 | SourceLocation LParenLoc, RParenLoc; |
| 4779 | LParenLoc = Tracker.getOpenLocation(); |
| 4780 | StartLoc = LParenLoc; |
| 4781 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4782 | if (isFunctionDeclaratorIdentifierList()) { |
| 4783 | if (RequiresArg) |
| 4784 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4785 | |
| 4786 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 4787 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4788 | Tracker.consumeClose(); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4789 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4790 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4791 | EndLoc = RParenLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4792 | } else { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4793 | if (Tok.isNot(tok::r_paren)) |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4794 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4795 | else if (RequiresArg) |
| 4796 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4797 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4798 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4799 | |
| 4800 | // If we have the closing ')', eat it. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4801 | Tracker.consumeClose(); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4802 | RParenLoc = Tracker.getCloseLocation(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4803 | LocalEndLoc = RParenLoc; |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4804 | EndLoc = RParenLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4805 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4806 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4807 | // FIXME: Accept these components in any order, and produce fixits to |
| 4808 | // correct the order if the user gets it wrong. Ideally we should deal |
| 4809 | // with the virt-specifier-seq and pure-specifier in the same way. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4810 | |
| 4811 | // Parse cv-qualifier-seq[opt]. |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4812 | ParseTypeQualifierListOpt(DS, false /*no attributes*/, false); |
| 4813 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 4814 | EndLoc = DS.getSourceRange().getEnd(); |
| 4815 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 4816 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 4817 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4818 | |
| 4819 | // Parse ref-qualifier[opt]. |
| 4820 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4821 | Diag(Tok, getLangOpts().CPlusPlus11 ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4822 | diag::warn_cxx98_compat_ref_qualifier : |
| 4823 | diag::ext_ref_qualifier); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4824 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4825 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 4826 | RefQualifierLoc = ConsumeToken(); |
| 4827 | EndLoc = RefQualifierLoc; |
| 4828 | } |
| 4829 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4830 | // C++11 [expr.prim.general]p3: |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4831 | // If a declaration declares a member function or member function |
| 4832 | // 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] | 4833 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4834 | // and the end of the function-definition, member-declarator, or |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4835 | // declarator. |
Richard Smith | d922779 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 4836 | // FIXME: currently, "static" case isn't handled correctly. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 4837 | bool IsCXX11MemberFunction = |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4838 | getLangOpts().CPlusPlus11 && |
Richard Smith | d922779 | 2013-03-15 00:41:52 +0000 | [diff] [blame] | 4839 | (D.getContext() == Declarator::MemberContext |
| 4840 | ? !D.getDeclSpec().isFriendSpecified() |
| 4841 | : D.getContext() == Declarator::FileContext && |
| 4842 | D.getCXXScopeSpec().isValid() && |
| 4843 | Actions.CurContext->isRecord()); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4844 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 4845 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
Richard Smith | 7b19cb1 | 2013-01-14 01:55:13 +0000 | [diff] [blame] | 4846 | DS.getTypeQualifiers() | |
| 4847 | (D.getDeclSpec().isConstexprSpecified() |
| 4848 | ? Qualifiers::Const : 0), |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4849 | IsCXX11MemberFunction); |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 4850 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4851 | // Parse exception-specification[opt]. |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 4852 | ESpecType = tryParseExceptionSpecification(ESpecRange, |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 4853 | DynamicExceptions, |
| 4854 | DynamicExceptionRanges, |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 4855 | NoexceptExpr); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4856 | if (ESpecType != EST_None) |
| 4857 | EndLoc = ESpecRange.getEnd(); |
| 4858 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4859 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 4860 | // after the exception-specification. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 4861 | MaybeParseCXX11Attributes(FnAttrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4862 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4863 | // Parse trailing-return-type[opt]. |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4864 | LocalEndLoc = EndLoc; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 4865 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4866 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4867 | if (D.getDeclSpec().getTypeSpecType() == TST_auto) |
| 4868 | StartLoc = D.getDeclSpec().getTypeSpecTypeLoc(); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4869 | LocalEndLoc = Tok.getLocation(); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 4870 | SourceRange Range; |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame] | 4871 | TrailingReturnType = ParseTrailingReturnType(Range); |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4872 | EndLoc = Range.getEnd(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4873 | } |
| 4874 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4875 | } |
| 4876 | |
| 4877 | // Remember that we parsed a function type, and remember the attributes. |
| 4878 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4879 | IsAmbiguous, |
| 4880 | LParenLoc, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4881 | ParamInfo.data(), ParamInfo.size(), |
Abramo Bagnara | 59c0a81 | 2012-10-04 21:42:10 +0000 | [diff] [blame] | 4882 | EllipsisLoc, RParenLoc, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4883 | DS.getTypeQualifiers(), |
| 4884 | RefQualifierIsLValueRef, |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4885 | RefQualifierLoc, ConstQualifierLoc, |
| 4886 | VolatileQualifierLoc, |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 4887 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4888 | ESpecType, ESpecRange.getBegin(), |
| 4889 | DynamicExceptions.data(), |
| 4890 | DynamicExceptionRanges.data(), |
| 4891 | DynamicExceptions.size(), |
| 4892 | NoexceptExpr.isUsable() ? |
| 4893 | NoexceptExpr.get() : 0, |
Abramo Bagnara | ac8ea05 | 2012-10-15 21:05:46 +0000 | [diff] [blame] | 4894 | StartLoc, LocalEndLoc, D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4895 | TrailingReturnType), |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4896 | FnAttrs, EndLoc); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4897 | |
| 4898 | Actions.ActOnEndFunctionDeclarator(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4899 | } |
| 4900 | |
| 4901 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 4902 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 4903 | /// |
| 4904 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 4905 | /// abstract-declarators. |
| 4906 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4907 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4908 | && Tok.is(tok::identifier) |
| 4909 | && !TryAltiVecVectorToken() |
| 4910 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 4911 | // 6.7.5.3p11. |
| 4912 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 4913 | // Identifier lists follow a really simple grammar: the identifiers can |
| 4914 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 4915 | // identifier lists are really rare in the brave new modern world, and |
| 4916 | // it is very common for someone to typo a type in a non-K&R style |
| 4917 | // list. If we are presented with something like: "void foo(intptr x, |
| 4918 | // float y)", we don't want to start parsing the function declarator as |
| 4919 | // though it is a K&R style declarator just because intptr is an |
| 4920 | // invalid type. |
| 4921 | // |
| 4922 | // To handle this, we check to see if the token after the first |
| 4923 | // identifier is a "," or ")". Only then do we parse it as an |
| 4924 | // identifier list. |
| 4925 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 4926 | } |
| 4927 | |
| 4928 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 4929 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 4930 | /// |
| 4931 | /// After returning, ParamInfo will hold the parsed parameters. |
| 4932 | /// |
| 4933 | /// identifier-list: [C99 6.7.5] |
| 4934 | /// identifier |
| 4935 | /// identifier-list ',' identifier |
| 4936 | /// |
| 4937 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 4938 | Declarator &D, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4939 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4940 | // If there was no identifier specified for the declarator, either we are in |
| 4941 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 4942 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 4943 | // diagnose this. |
| 4944 | if (!D.getIdentifier()) |
| 4945 | Diag(Tok, diag::ext_ident_list_in_param); |
| 4946 | |
| 4947 | // Maintain an efficient lookup of params we have seen so far. |
| 4948 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 4949 | |
| 4950 | while (1) { |
| 4951 | // If this isn't an identifier, report the error and skip until ')'. |
| 4952 | if (Tok.isNot(tok::identifier)) { |
| 4953 | Diag(Tok, diag::err_expected_ident); |
| 4954 | SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 4955 | // Forget we parsed anything. |
| 4956 | ParamInfo.clear(); |
| 4957 | return; |
| 4958 | } |
| 4959 | |
| 4960 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 4961 | |
| 4962 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 4963 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 4964 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 4965 | |
| 4966 | // Verify that the argument identifier has not already been mentioned. |
| 4967 | if (!ParamsSoFar.insert(ParmII)) { |
| 4968 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 4969 | } else { |
| 4970 | // Remember this identifier in ParamInfo. |
| 4971 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4972 | Tok.getLocation(), |
| 4973 | 0)); |
| 4974 | } |
| 4975 | |
| 4976 | // Eat the identifier. |
| 4977 | ConsumeToken(); |
| 4978 | |
| 4979 | // The list continues if we see a comma. |
| 4980 | if (Tok.isNot(tok::comma)) |
| 4981 | break; |
| 4982 | ConsumeToken(); |
| 4983 | } |
| 4984 | } |
| 4985 | |
| 4986 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 4987 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 4988 | /// identifier list. |
| 4989 | /// |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4990 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 4991 | /// caller parsed those arguments immediately after the open paren - they should |
| 4992 | /// be considered to be part of the first parameter. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4993 | /// |
| 4994 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 4995 | /// be the location of the ellipsis, if any was parsed. |
| 4996 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4997 | /// parameter-type-list: [C99 6.7.5] |
| 4998 | /// parameter-list |
| 4999 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5000 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5001 | /// |
| 5002 | /// parameter-list: [C99 6.7.5] |
| 5003 | /// parameter-declaration |
| 5004 | /// parameter-list ',' parameter-declaration |
| 5005 | /// |
| 5006 | /// parameter-declaration: [C99 6.7.5] |
| 5007 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5008 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5009 | /// [C++11] initializer-clause |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5010 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 5011 | /// declaration-specifiers abstract-declarator[opt] |
| 5012 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 5013 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5014 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5015 | /// [C++11] attribute-specifier-seq parameter-declaration |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5016 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5017 | void Parser::ParseParameterDeclarationClause( |
| 5018 | Declarator &D, |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5019 | ParsedAttributes &FirstArgAttrs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 5020 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 5021 | SourceLocation &EllipsisLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5022 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5023 | while (1) { |
| 5024 | if (Tok.is(tok::ellipsis)) { |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5025 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 5026 | // before deciding this was a parameter-declaration-clause. |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 5027 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5028 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5029 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5030 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5031 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 5032 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5033 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5034 | |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5035 | // Parse any C++11 attributes. |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5036 | MaybeParseCXX11Attributes(DS.getAttributes()); |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 5037 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5038 | // Skip any Microsoft attributes before a param. |
Chad Rosier | 16f90bf | 2012-12-20 20:37:53 +0000 | [diff] [blame] | 5039 | MaybeParseMicrosoftAttributes(DS.getAttributes()); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5040 | |
| 5041 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 5042 | |
| 5043 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5044 | // 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] | 5045 | // 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] | 5046 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 5047 | // too much hassle. |
| 5048 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5049 | |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 5050 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5051 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5052 | // Parse the declarator. This is "PrototypeContext", because we must |
| 5053 | // accept either 'declarator' or 'abstract-declarator' here. |
| 5054 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 5055 | ParseDeclarator(ParmDecl); |
| 5056 | |
| 5057 | // Parse GNU attributes, if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5058 | MaybeParseGNUAttributes(ParmDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5059 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5060 | // Remember this parsed parameter in ParamInfo. |
| 5061 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5062 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5063 | // DefArgToks is used when the parsing of default arguments needs |
| 5064 | // to be delayed. |
| 5065 | CachedTokens *DefArgToks = 0; |
| 5066 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5067 | // If no parameter was specified, verify that *something* was specified, |
| 5068 | // otherwise we have a missing type and identifier. |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 5069 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 5070 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5071 | // Completely missing, emit error. |
| 5072 | Diag(DSStart, diag::err_missing_param); |
| 5073 | } else { |
| 5074 | // Otherwise, we have something. Add it and let semantic analysis try |
| 5075 | // 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] | 5076 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5077 | // Inform the actions module about the parameter declarator, so it gets |
| 5078 | // added to the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 5079 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5080 | |
| 5081 | // Parse the default argument, if any. We parse the default |
| 5082 | // arguments in all dialects; the semantic analysis in |
| 5083 | // ActOnParamDefaultArgument will reject the default argument in |
| 5084 | // C. |
| 5085 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5086 | SourceLocation EqualLoc = Tok.getLocation(); |
| 5087 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5088 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5089 | if (D.getContext() == Declarator::MemberContext) { |
| 5090 | // If we're inside a class definition, cache the tokens |
| 5091 | // corresponding to the default argument. We'll actually parse |
| 5092 | // them when we see the end of the class definition. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5093 | // FIXME: Can we use a smart pointer for Toks? |
| 5094 | DefArgToks = new CachedTokens; |
| 5095 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5096 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 5097 | /*StopAtSemi=*/true, |
| 5098 | /*ConsumeFinalToken=*/false)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5099 | delete DefArgToks; |
| 5100 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5101 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5102 | } else { |
| 5103 | // Mark the end of the default argument so that we know when to |
| 5104 | // stop when we parse it later on. |
| 5105 | Token DefArgEnd; |
| 5106 | DefArgEnd.startToken(); |
| 5107 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 5108 | DefArgEnd.setLocation(Tok.getLocation()); |
| 5109 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5110 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 5111 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 5112 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5113 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5114 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 5115 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5116 | |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5117 | // The argument isn't actually potentially evaluated unless it is |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5118 | // used. |
| 5119 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 5120 | Sema::PotentiallyEvaluatedIfUsed, |
| 5121 | Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 5122 | |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5123 | ExprResult DefArgResult; |
Richard Smith | 80ad52f | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 5124 | if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) { |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5125 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5126 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 5127 | } else |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 5128 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5129 | if (DefArgResult.isInvalid()) { |
| 5130 | Actions.ActOnParamDefaultArgumentError(Param); |
| 5131 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 5132 | } else { |
| 5133 | // Inform the actions module about the default argument |
| 5134 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5135 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5136 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5137 | } |
| 5138 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5139 | |
| 5140 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 5141 | ParmDecl.getIdentifierLoc(), Param, |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 5142 | DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5143 | } |
| 5144 | |
| 5145 | // 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] | 5146 | if (Tok.isNot(tok::comma)) { |
| 5147 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5148 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5149 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5150 | if (!getLangOpts().CPlusPlus) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5151 | // We have ellipsis without a preceding ',', which is ill-formed |
| 5152 | // in C. Complain and provide the fix. |
| 5153 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 5154 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5155 | } |
| 5156 | } |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5157 | |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 5158 | break; |
| 5159 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5160 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 5161 | // Consume the comma. |
| 5162 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5163 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5164 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 5165 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 5166 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5167 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 5168 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 5169 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 5170 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 5171 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5172 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 5173 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5174 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 5175 | if (CheckProhibitedCXX11Attribute()) |
| 5176 | return; |
| 5177 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5178 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 5179 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5180 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5181 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 5182 | // This code does a fast path to handle some of the most obvious cases. |
| 5183 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5184 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5185 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5186 | MaybeParseCXX11Attributes(attrs); |
Chad Rosier | 8decdee | 2012-06-26 22:30:43 +0000 | [diff] [blame] | 5187 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5188 | // Remember that we parsed the empty array type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5189 | ExprResult NumElements; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5190 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5191 | T.getOpenLocation(), |
| 5192 | T.getCloseLocation()), |
| 5193 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5194 | return; |
| 5195 | } else if (Tok.getKind() == tok::numeric_constant && |
| 5196 | GetLookAheadToken(1).is(tok::r_square)) { |
| 5197 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 5198 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5199 | ConsumeToken(); |
| 5200 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5201 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5202 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5203 | MaybeParseCXX11Attributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5204 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5205 | // Remember that we parsed a array type, and remember its features. |
Nikola Smiljanic | ebf0fa8 | 2013-01-11 08:33:05 +0000 | [diff] [blame] | 5206 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 5207 | ExprRes.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5208 | T.getOpenLocation(), |
| 5209 | T.getCloseLocation()), |
| 5210 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5211 | return; |
| 5212 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5213 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5214 | // If valid, this location is the position where we read the 'static' keyword. |
| 5215 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5216 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5217 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5218 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5219 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5220 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5221 | DeclSpec DS(AttrFactory); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 5222 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5223 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5224 | // If we haven't already read 'static', check to see if there is one after the |
| 5225 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5226 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5227 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5228 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5229 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 5230 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5231 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5232 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5233 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 5234 | // 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] | 5235 | // the token after the star is a ']'. Since stars in arrays are |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5236 | // infrequent, use of lookahead is not costly here. |
| 5237 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 5238 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5239 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5240 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5241 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 5242 | StaticLoc = SourceLocation(); // Drop the static. |
| 5243 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 5244 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5245 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5246 | // Note, in C89, this production uses the constant-expr production instead |
| 5247 | // of assignment-expr. The only difference is that assignment-expr allows |
| 5248 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 5249 | // 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] | 5250 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5251 | // Parse the constant-expression or assignment-expression now (depending |
| 5252 | // on dialect). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5253 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5254 | NumElements = ParseConstantExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5255 | } else { |
| 5256 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 5257 | Sema::ConstantEvaluated); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 5258 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5259 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5260 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5261 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5262 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 5263 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 5264 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5265 | // If the expression was invalid, skip it. |
| 5266 | SkipUntil(tok::r_square); |
| 5267 | return; |
| 5268 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5269 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5270 | T.consumeClose(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 5271 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5272 | ParsedAttributes attrs(AttrFactory); |
Richard Smith | 4e24f0f | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 5273 | MaybeParseCXX11Attributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 5274 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 5275 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 5276 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5277 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 5278 | NumElements.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5279 | T.getOpenLocation(), |
| 5280 | T.getCloseLocation()), |
| 5281 | attrs, T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5282 | } |
| 5283 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5284 | /// [GNU] typeof-specifier: |
| 5285 | /// typeof ( expressions ) |
| 5286 | /// typeof ( type-name ) |
| 5287 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5288 | /// |
| 5289 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 5290 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5291 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5292 | SourceLocation StartLoc = ConsumeToken(); |
| 5293 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5294 | const bool hasParens = Tok.is(tok::l_paren); |
| 5295 | |
Eli Friedman | 80bfa3d | 2012-09-26 04:34:21 +0000 | [diff] [blame] | 5296 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated, |
| 5297 | Sema::ReuseLambdaContextDecl); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5298 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5299 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5300 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5301 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 5302 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 5303 | CastTy, CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 5304 | if (hasParens) |
| 5305 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5306 | |
| 5307 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5308 | // FIXME: Not accurate, the range gets one token more than it should. |
| 5309 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5310 | else |
| 5311 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5312 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5313 | if (isCastExpr) { |
| 5314 | if (!CastTy) { |
| 5315 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5316 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 5317 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5318 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5319 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5320 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5321 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5322 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5323 | DiagID, CastTy)) |
| 5324 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 5325 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5326 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5327 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5328 | // If we get here, the operand to the typeof was an expresion. |
| 5329 | if (Operand.isInvalid()) { |
| 5330 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 5331 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5332 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 5333 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 5334 | // We might need to transform the operand if it is potentially evaluated. |
| 5335 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 5336 | if (Operand.isInvalid()) { |
| 5337 | DS.SetTypeSpecError(); |
| 5338 | return; |
| 5339 | } |
| 5340 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5341 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5342 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 5343 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 5344 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5345 | DiagID, Operand.get())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 5346 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 5347 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5348 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 5349 | /// [C11] atomic-specifier: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5350 | /// _Atomic ( type-name ) |
| 5351 | /// |
| 5352 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
| 5353 | assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier"); |
| 5354 | |
| 5355 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5356 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 5357 | if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) { |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5358 | SkipUntil(tok::r_paren); |
| 5359 | return; |
| 5360 | } |
| 5361 | |
| 5362 | TypeResult Result = ParseTypeName(); |
| 5363 | if (Result.isInvalid()) { |
| 5364 | SkipUntil(tok::r_paren); |
| 5365 | return; |
| 5366 | } |
| 5367 | |
| 5368 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5369 | T.consumeClose(); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5370 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5371 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5372 | return; |
| 5373 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 5374 | DS.setTypeofParensRange(T.getRange()); |
| 5375 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5376 | |
| 5377 | const char *PrevSpec = 0; |
| 5378 | unsigned DiagID; |
| 5379 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
| 5380 | DiagID, Result.release())) |
| 5381 | Diag(StartLoc, DiagID) << PrevSpec; |
| 5382 | } |
| 5383 | |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5384 | |
| 5385 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 5386 | /// from TryAltiVecVectorToken. |
| 5387 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 5388 | Token Next = NextToken(); |
| 5389 | switch (Next.getKind()) { |
| 5390 | default: return false; |
| 5391 | case tok::kw_short: |
| 5392 | case tok::kw_long: |
| 5393 | case tok::kw_signed: |
| 5394 | case tok::kw_unsigned: |
| 5395 | case tok::kw_void: |
| 5396 | case tok::kw_char: |
| 5397 | case tok::kw_int: |
| 5398 | case tok::kw_float: |
| 5399 | case tok::kw_double: |
| 5400 | case tok::kw_bool: |
| 5401 | case tok::kw___pixel: |
| 5402 | Tok.setKind(tok::kw___vector); |
| 5403 | return true; |
| 5404 | case tok::identifier: |
| 5405 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5406 | Tok.setKind(tok::kw___vector); |
| 5407 | return true; |
| 5408 | } |
| 5409 | return false; |
| 5410 | } |
| 5411 | } |
| 5412 | |
| 5413 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 5414 | const char *&PrevSpec, unsigned &DiagID, |
| 5415 | bool &isInvalid) { |
| 5416 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 5417 | Token Next = NextToken(); |
| 5418 | switch (Next.getKind()) { |
| 5419 | case tok::kw_short: |
| 5420 | case tok::kw_long: |
| 5421 | case tok::kw_signed: |
| 5422 | case tok::kw_unsigned: |
| 5423 | case tok::kw_void: |
| 5424 | case tok::kw_char: |
| 5425 | case tok::kw_int: |
| 5426 | case tok::kw_float: |
| 5427 | case tok::kw_double: |
| 5428 | case tok::kw_bool: |
| 5429 | case tok::kw___pixel: |
| 5430 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5431 | return true; |
| 5432 | case tok::identifier: |
| 5433 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 5434 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 5435 | return true; |
| 5436 | } |
| 5437 | break; |
| 5438 | default: |
| 5439 | break; |
| 5440 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 5441 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 5442 | DS.isTypeAltiVecVector()) { |
| 5443 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 5444 | return true; |
| 5445 | } |
| 5446 | return false; |
| 5447 | } |