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" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 15 | #include "clang/Parse/ParseDiagnostic.h" |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 16 | #include "clang/Basic/OpenCL.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Scope.h" |
| 18 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 19 | #include "clang/Sema/PrettyDeclStackTrace.h" |
Chris Lattner | d167ca0 | 2009-12-10 00:21:05 +0000 | [diff] [blame] | 20 | #include "RAIIObjectsForParser.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/SmallSet.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallString.h" |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/StringSwitch.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 24 | using namespace clang; |
| 25 | |
| 26 | //===----------------------------------------------------------------------===// |
| 27 | // C99 6.7: Declarations. |
| 28 | //===----------------------------------------------------------------------===// |
| 29 | |
| 30 | /// ParseTypeName |
| 31 | /// type-name: [C99 6.7.6] |
| 32 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 33 | /// |
| 34 | /// Called type-id in C++. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 35 | TypeResult Parser::ParseTypeName(SourceRange *Range, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 36 | Declarator::TheContext Context, |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 37 | AccessSpecifier AS, |
| 38 | Decl **OwnedType) { |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 39 | DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context); |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 40 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | // Parse the common declaration-specifiers piece. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 42 | DeclSpec DS(AttrFactory); |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 43 | ParseSpecifierQualifierList(DS, AS, DSC); |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 44 | if (OwnedType) |
| 45 | *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0; |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 46 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | // Parse the abstract-declarator, if present. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 48 | Declarator DeclaratorInfo(DS, Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 50 | if (Range) |
| 51 | *Range = DeclaratorInfo.getSourceRange(); |
| 52 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 53 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 54 | return true; |
| 55 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 56 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 59 | |
| 60 | /// isAttributeLateParsed - Return true if the attribute has arguments that |
| 61 | /// require late parsing. |
| 62 | static bool isAttributeLateParsed(const IdentifierInfo &II) { |
| 63 | return llvm::StringSwitch<bool>(II.getName()) |
| 64 | #include "clang/Parse/AttrLateParsed.inc" |
| 65 | .Default(false); |
| 66 | } |
| 67 | |
| 68 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 69 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 70 | /// |
| 71 | /// [GNU] attributes: |
| 72 | /// attribute |
| 73 | /// attributes attribute |
| 74 | /// |
| 75 | /// [GNU] attribute: |
| 76 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 77 | /// |
| 78 | /// [GNU] attribute-list: |
| 79 | /// attrib |
| 80 | /// attribute_list ',' attrib |
| 81 | /// |
| 82 | /// [GNU] attrib: |
| 83 | /// empty |
| 84 | /// attrib-name |
| 85 | /// attrib-name '(' identifier ')' |
| 86 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 87 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 88 | /// |
| 89 | /// [GNU] attrib-name: |
| 90 | /// identifier |
| 91 | /// typespec |
| 92 | /// typequal |
| 93 | /// storageclass |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 94 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 95 | /// FIXME: The GCC grammar/code for this construct implies we need two |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 97 | /// which is followed by a comma or close parenthesis, then the arguments |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 98 | /// start with that identifier; otherwise they are an expression list." |
| 99 | /// |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 100 | /// GCC does not require the ',' between attribs in an attribute-list. |
| 101 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 102 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 103 | /// any attributes that don't work (based on my limited testing). Most |
| 104 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 105 | /// a pressing need to implement the 2 token lookahead. |
| 106 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 107 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 108 | SourceLocation *endLoc, |
| 109 | LateParsedAttrList *LateAttrs) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 110 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 112 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 113 | ConsumeToken(); |
| 114 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 115 | "attribute")) { |
| 116 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 117 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 118 | } |
| 119 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 120 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 121 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 122 | } |
| 123 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 124 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 125 | Tok.is(tok::comma)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 126 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 127 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 128 | ConsumeToken(); |
| 129 | continue; |
| 130 | } |
| 131 | // we have an identifier or declaration specifier (const, int, etc.) |
| 132 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 133 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 135 | if (Tok.is(tok::l_paren)) { |
| 136 | // handle "parameterized" attributes |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 137 | if (LateAttrs && isAttributeLateParsed(*AttrName)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 138 | LateParsedAttribute *LA = |
| 139 | new LateParsedAttribute(this, *AttrName, AttrNameLoc); |
| 140 | LateAttrs->push_back(LA); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 141 | |
| 142 | // Attributes in a class are parsed at the end of the class, along |
| 143 | // with other late-parsed declarations. |
| 144 | if (!ClassStack.empty()) |
| 145 | getCurrentClass().LateParsedDeclarations.push_back(LA); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 147 | // consume everything up to and including the matching right parens |
| 148 | ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 150 | Token Eof; |
| 151 | Eof.startToken(); |
| 152 | Eof.setLocation(Tok.getLocation()); |
| 153 | LA->Toks.push_back(Eof); |
| 154 | } else { |
| 155 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 156 | } |
| 157 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 158 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 159 | 0, SourceLocation(), 0, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 163 | SkipUntil(tok::r_paren, false); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 164 | SourceLocation Loc = Tok.getLocation(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 165 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 166 | SkipUntil(tok::r_paren, false); |
| 167 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 168 | if (endLoc) |
| 169 | *endLoc = Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 170 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 173 | |
| 174 | /// Parse the arguments to a parameterized GNU attribute |
| 175 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 176 | SourceLocation AttrNameLoc, |
| 177 | ParsedAttributes &Attrs, |
| 178 | SourceLocation *EndLoc) { |
| 179 | |
| 180 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 181 | |
| 182 | // Availability attributes have their own grammar. |
| 183 | if (AttrName->isStr("availability")) { |
| 184 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 185 | return; |
| 186 | } |
| 187 | // Thread safety attributes fit into the FIXME case above, so we |
| 188 | // just parse the arguments as a list of expressions |
| 189 | if (IsThreadSafetyAttribute(AttrName->getName())) { |
| 190 | ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | ConsumeParen(); // ignore the left paren loc for now |
| 195 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 196 | IdentifierInfo *ParmName = 0; |
| 197 | SourceLocation ParmLoc; |
| 198 | bool BuiltinType = false; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 199 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 200 | switch (Tok.getKind()) { |
| 201 | case tok::kw_char: |
| 202 | case tok::kw_wchar_t: |
| 203 | case tok::kw_char16_t: |
| 204 | case tok::kw_char32_t: |
| 205 | case tok::kw_bool: |
| 206 | case tok::kw_short: |
| 207 | case tok::kw_int: |
| 208 | case tok::kw_long: |
| 209 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 210 | case tok::kw___int128: |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 211 | case tok::kw_signed: |
| 212 | case tok::kw_unsigned: |
| 213 | case tok::kw_float: |
| 214 | case tok::kw_double: |
| 215 | case tok::kw_void: |
| 216 | case tok::kw_typeof: |
| 217 | // __attribute__(( vec_type_hint(char) )) |
| 218 | // FIXME: Don't just discard the builtin type token. |
| 219 | ConsumeToken(); |
| 220 | BuiltinType = true; |
| 221 | break; |
| 222 | |
| 223 | case tok::identifier: |
| 224 | ParmName = Tok.getIdentifierInfo(); |
| 225 | ParmLoc = ConsumeToken(); |
| 226 | break; |
| 227 | |
| 228 | default: |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | ExprVector ArgExprs(Actions); |
| 233 | |
| 234 | if (!BuiltinType && |
| 235 | (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) { |
| 236 | // Eat the comma. |
| 237 | if (ParmLoc.isValid()) |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 238 | ConsumeToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 239 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 240 | // Parse the non-empty comma-separated list of expressions. |
| 241 | while (1) { |
| 242 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 243 | if (ArgExpr.isInvalid()) { |
| 244 | SkipUntil(tok::r_paren); |
| 245 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 246 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 247 | ArgExprs.push_back(ArgExpr.release()); |
| 248 | if (Tok.isNot(tok::comma)) |
| 249 | break; |
| 250 | ConsumeToken(); // Eat the comma, move to the next argument |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 251 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 252 | } |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 253 | else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) { |
| 254 | if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<", |
| 255 | tok::greater)) { |
Fariborz Jahanian | b224343 | 2011-10-18 23:13:50 +0000 | [diff] [blame] | 256 | while (Tok.is(tok::identifier)) { |
| 257 | ConsumeToken(); |
| 258 | if (Tok.is(tok::greater)) |
| 259 | break; |
| 260 | if (Tok.is(tok::comma)) { |
| 261 | ConsumeToken(); |
| 262 | continue; |
| 263 | } |
| 264 | } |
| 265 | if (Tok.isNot(tok::greater)) |
| 266 | Diag(Tok, diag::err_iboutletcollection_with_protocol); |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 267 | SkipUntil(tok::r_paren, false, true); // skip until ')' |
| 268 | } |
| 269 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 270 | |
| 271 | SourceLocation RParen = Tok.getLocation(); |
| 272 | if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 273 | AttributeList *attr = |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 274 | Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc, |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 275 | ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size()); |
Michael Han | e53ac8a | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 276 | if (BuiltinType && attr->getKind() == AttributeList::AT_iboutletcollection) |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 277 | Diag(Tok, diag::err_iboutletcollection_builtintype); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
| 281 | |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 282 | /// ParseMicrosoftDeclSpec - Parse an __declspec construct |
| 283 | /// |
| 284 | /// [MS] decl-specifier: |
| 285 | /// __declspec ( extended-decl-modifier-seq ) |
| 286 | /// |
| 287 | /// [MS] extended-decl-modifier-seq: |
| 288 | /// extended-decl-modifier[opt] |
| 289 | /// extended-decl-modifier extended-decl-modifier-seq |
| 290 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 291 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) { |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 292 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 293 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 294 | ConsumeToken(); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 295 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 296 | "declspec")) { |
| 297 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 298 | return; |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 299 | } |
Francois Pichet | 373197b | 2011-05-07 19:04:49 +0000 | [diff] [blame] | 300 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 301 | while (Tok.getIdentifierInfo()) { |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 302 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 303 | SourceLocation AttrNameLoc = ConsumeToken(); |
Francois Pichet | 373197b | 2011-05-07 19:04:49 +0000 | [diff] [blame] | 304 | |
| 305 | // FIXME: Remove this when we have proper __declspec(property()) support. |
| 306 | // Just skip everything inside property(). |
| 307 | if (AttrName->getName() == "property") { |
| 308 | ConsumeParen(); |
| 309 | SkipUntil(tok::r_paren); |
| 310 | } |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 311 | if (Tok.is(tok::l_paren)) { |
| 312 | ConsumeParen(); |
| 313 | // FIXME: This doesn't parse __declspec(property(get=get_func_name)) |
| 314 | // correctly. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 315 | ExprResult ArgExpr(ParseAssignmentExpression()); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 316 | if (!ArgExpr.isInvalid()) { |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 317 | Expr *ExprList = ArgExpr.take(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 318 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 319 | SourceLocation(), &ExprList, 1, true); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 320 | } |
| 321 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 322 | SkipUntil(tok::r_paren, false); |
| 323 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 324 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 325 | 0, SourceLocation(), 0, 0, true); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 329 | SkipUntil(tok::r_paren, false); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 330 | return; |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 331 | } |
| 332 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 333 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 334 | // Treat these like attributes |
| 335 | // FIXME: Allow Sema to distinguish between these and real attributes! |
| 336 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 337 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 338 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 339 | Tok.is(tok::kw___ptr32) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 340 | Tok.is(tok::kw___unaligned)) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 341 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 342 | SourceLocation AttrNameLoc = ConsumeToken(); |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 343 | if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
| 344 | Tok.is(tok::kw___ptr32)) |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 345 | // FIXME: Support these properly! |
| 346 | continue; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 347 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 348 | SourceLocation(), 0, 0, true); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 349 | } |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 350 | } |
| 351 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 352 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 353 | // Treat these like attributes |
| 354 | while (Tok.is(tok::kw___pascal)) { |
| 355 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 356 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 357 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 358 | SourceLocation(), 0, 0, true); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 359 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 360 | } |
| 361 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 362 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 363 | // Treat these like attributes |
| 364 | while (Tok.is(tok::kw___kernel)) { |
| 365 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 366 | attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"), |
| 367 | AttrNameLoc, 0, AttrNameLoc, 0, |
| 368 | SourceLocation(), 0, 0, false); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 369 | } |
| 370 | } |
| 371 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 372 | void Parser::ParseOpenCLQualifiers(DeclSpec &DS) { |
| 373 | SourceLocation Loc = Tok.getLocation(); |
| 374 | switch(Tok.getKind()) { |
| 375 | // OpenCL qualifiers: |
| 376 | case tok::kw___private: |
| 377 | case tok::kw_private: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 378 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 379 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 380 | PP.getIdentifierInfo("address_space"), Loc, 0); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 381 | break; |
| 382 | |
| 383 | case tok::kw___global: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 384 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 385 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 386 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 387 | break; |
| 388 | |
| 389 | case tok::kw___local: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 390 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 391 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 392 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 393 | break; |
| 394 | |
| 395 | case tok::kw___constant: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 396 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 397 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 398 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 399 | break; |
| 400 | |
| 401 | case tok::kw___read_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 402 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 403 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 404 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 405 | break; |
| 406 | |
| 407 | case tok::kw___write_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 408 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 409 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 410 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 411 | break; |
| 412 | |
| 413 | case tok::kw___read_write: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 414 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 415 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 416 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 417 | break; |
| 418 | default: break; |
| 419 | } |
| 420 | } |
| 421 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 422 | /// \brief Parse a version number. |
| 423 | /// |
| 424 | /// version: |
| 425 | /// simple-integer |
| 426 | /// simple-integer ',' simple-integer |
| 427 | /// simple-integer ',' simple-integer ',' simple-integer |
| 428 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 429 | Range = Tok.getLocation(); |
| 430 | |
| 431 | if (!Tok.is(tok::numeric_constant)) { |
| 432 | Diag(Tok, diag::err_expected_version); |
| 433 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 434 | return VersionTuple(); |
| 435 | } |
| 436 | |
| 437 | // Parse the major (and possibly minor and subminor) versions, which |
| 438 | // are stored in the numeric constant. We utilize a quirk of the |
| 439 | // lexer, which is that it handles something like 1.2.3 as a single |
| 440 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 441 | SmallString<512> Buffer; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 442 | Buffer.resize(Tok.getLength()+1); |
| 443 | const char *ThisTokBegin = &Buffer[0]; |
| 444 | |
| 445 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 446 | bool Invalid = false; |
| 447 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 448 | if (Invalid) |
| 449 | return VersionTuple(); |
| 450 | |
| 451 | // Parse the major version. |
| 452 | unsigned AfterMajor = 0; |
| 453 | unsigned Major = 0; |
| 454 | while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) { |
| 455 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 456 | ++AfterMajor; |
| 457 | } |
| 458 | |
| 459 | if (AfterMajor == 0) { |
| 460 | Diag(Tok, diag::err_expected_version); |
| 461 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 462 | return VersionTuple(); |
| 463 | } |
| 464 | |
| 465 | if (AfterMajor == ActualLength) { |
| 466 | ConsumeToken(); |
| 467 | |
| 468 | // We only had a single version component. |
| 469 | if (Major == 0) { |
| 470 | Diag(Tok, diag::err_zero_version); |
| 471 | return VersionTuple(); |
| 472 | } |
| 473 | |
| 474 | return VersionTuple(Major); |
| 475 | } |
| 476 | |
| 477 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 478 | Diag(Tok, diag::err_expected_version); |
| 479 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 480 | return VersionTuple(); |
| 481 | } |
| 482 | |
| 483 | // Parse the minor version. |
| 484 | unsigned AfterMinor = AfterMajor + 1; |
| 485 | unsigned Minor = 0; |
| 486 | while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) { |
| 487 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 488 | ++AfterMinor; |
| 489 | } |
| 490 | |
| 491 | if (AfterMinor == ActualLength) { |
| 492 | ConsumeToken(); |
| 493 | |
| 494 | // We had major.minor. |
| 495 | if (Major == 0 && Minor == 0) { |
| 496 | Diag(Tok, diag::err_zero_version); |
| 497 | return VersionTuple(); |
| 498 | } |
| 499 | |
| 500 | return VersionTuple(Major, Minor); |
| 501 | } |
| 502 | |
| 503 | // If what follows is not a '.', we have a problem. |
| 504 | if (ThisTokBegin[AfterMinor] != '.') { |
| 505 | Diag(Tok, diag::err_expected_version); |
| 506 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 507 | return VersionTuple(); |
| 508 | } |
| 509 | |
| 510 | // Parse the subminor version. |
| 511 | unsigned AfterSubminor = AfterMinor + 1; |
| 512 | unsigned Subminor = 0; |
| 513 | while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) { |
| 514 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 515 | ++AfterSubminor; |
| 516 | } |
| 517 | |
| 518 | if (AfterSubminor != ActualLength) { |
| 519 | Diag(Tok, diag::err_expected_version); |
| 520 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 521 | return VersionTuple(); |
| 522 | } |
| 523 | ConsumeToken(); |
| 524 | return VersionTuple(Major, Minor, Subminor); |
| 525 | } |
| 526 | |
| 527 | /// \brief Parse the contents of the "availability" attribute. |
| 528 | /// |
| 529 | /// availability-attribute: |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 530 | /// 'availability' '(' platform ',' version-arg-list, opt-message')' |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 531 | /// |
| 532 | /// platform: |
| 533 | /// identifier |
| 534 | /// |
| 535 | /// version-arg-list: |
| 536 | /// version-arg |
| 537 | /// version-arg ',' version-arg-list |
| 538 | /// |
| 539 | /// version-arg: |
| 540 | /// 'introduced' '=' version |
| 541 | /// 'deprecated' '=' version |
Douglas Gregor | 93a7067 | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 542 | /// 'obsoleted' = version |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 543 | /// 'unavailable' |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 544 | /// opt-message: |
| 545 | /// 'message' '=' <string> |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 546 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 547 | SourceLocation AvailabilityLoc, |
| 548 | ParsedAttributes &attrs, |
| 549 | SourceLocation *endLoc) { |
| 550 | SourceLocation PlatformLoc; |
| 551 | IdentifierInfo *Platform = 0; |
| 552 | |
| 553 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 554 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 555 | ExprResult MessageExpr; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 556 | |
| 557 | // Opening '('. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 558 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 559 | if (T.consumeOpen()) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 560 | Diag(Tok, diag::err_expected_lparen); |
| 561 | return; |
| 562 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 563 | |
| 564 | // Parse the platform name, |
| 565 | if (Tok.isNot(tok::identifier)) { |
| 566 | Diag(Tok, diag::err_availability_expected_platform); |
| 567 | SkipUntil(tok::r_paren); |
| 568 | return; |
| 569 | } |
| 570 | Platform = Tok.getIdentifierInfo(); |
| 571 | PlatformLoc = ConsumeToken(); |
| 572 | |
| 573 | // Parse the ',' following the platform name. |
| 574 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren)) |
| 575 | return; |
| 576 | |
| 577 | // If we haven't grabbed the pointers for the identifiers |
| 578 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 579 | if (!Ident_introduced) { |
| 580 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 581 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 582 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 583 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 584 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 588 | SourceLocation UnavailableLoc; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 589 | do { |
| 590 | if (Tok.isNot(tok::identifier)) { |
| 591 | Diag(Tok, diag::err_availability_expected_change); |
| 592 | SkipUntil(tok::r_paren); |
| 593 | return; |
| 594 | } |
| 595 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 596 | SourceLocation KeywordLoc = ConsumeToken(); |
| 597 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 598 | if (Keyword == Ident_unavailable) { |
| 599 | if (UnavailableLoc.isValid()) { |
| 600 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 601 | << Keyword << SourceRange(UnavailableLoc); |
| 602 | } |
| 603 | UnavailableLoc = KeywordLoc; |
| 604 | |
| 605 | if (Tok.isNot(tok::comma)) |
| 606 | break; |
| 607 | |
| 608 | ConsumeToken(); |
| 609 | continue; |
| 610 | } |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 611 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 612 | if (Tok.isNot(tok::equal)) { |
| 613 | Diag(Tok, diag::err_expected_equal_after) |
| 614 | << Keyword; |
| 615 | SkipUntil(tok::r_paren); |
| 616 | return; |
| 617 | } |
| 618 | ConsumeToken(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 619 | if (Keyword == Ident_message) { |
| 620 | if (!isTokenStringLiteral()) { |
| 621 | Diag(Tok, diag::err_expected_string_literal); |
| 622 | SkipUntil(tok::r_paren); |
| 623 | return; |
| 624 | } |
| 625 | MessageExpr = ParseStringLiteralExpression(); |
| 626 | break; |
| 627 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 628 | |
| 629 | SourceRange VersionRange; |
| 630 | VersionTuple Version = ParseVersionTuple(VersionRange); |
| 631 | |
| 632 | if (Version.empty()) { |
| 633 | SkipUntil(tok::r_paren); |
| 634 | return; |
| 635 | } |
| 636 | |
| 637 | unsigned Index; |
| 638 | if (Keyword == Ident_introduced) |
| 639 | Index = Introduced; |
| 640 | else if (Keyword == Ident_deprecated) |
| 641 | Index = Deprecated; |
| 642 | else if (Keyword == Ident_obsoleted) |
| 643 | Index = Obsoleted; |
| 644 | else |
| 645 | Index = Unknown; |
| 646 | |
| 647 | if (Index < Unknown) { |
| 648 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 649 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 650 | << Keyword |
| 651 | << SourceRange(Changes[Index].KeywordLoc, |
| 652 | Changes[Index].VersionRange.getEnd()); |
| 653 | } |
| 654 | |
| 655 | Changes[Index].KeywordLoc = KeywordLoc; |
| 656 | Changes[Index].Version = Version; |
| 657 | Changes[Index].VersionRange = VersionRange; |
| 658 | } else { |
| 659 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 660 | << Keyword << VersionRange; |
| 661 | } |
| 662 | |
| 663 | if (Tok.isNot(tok::comma)) |
| 664 | break; |
| 665 | |
| 666 | ConsumeToken(); |
| 667 | } while (true); |
| 668 | |
| 669 | // Closing ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 670 | if (T.consumeClose()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 671 | return; |
| 672 | |
| 673 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 674 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 675 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 676 | // The 'unavailable' availability cannot be combined with any other |
| 677 | // availability changes. Make sure that hasn't happened. |
| 678 | if (UnavailableLoc.isValid()) { |
| 679 | bool Complained = false; |
| 680 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 681 | if (Changes[Index].KeywordLoc.isValid()) { |
| 682 | if (!Complained) { |
| 683 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 684 | << SourceRange(Changes[Index].KeywordLoc, |
| 685 | Changes[Index].VersionRange.getEnd()); |
| 686 | Complained = true; |
| 687 | } |
| 688 | |
| 689 | // Clear out the availability. |
| 690 | Changes[Index] = AvailabilityChange(); |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 695 | // Record this attribute |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 696 | attrs.addNew(&Availability, |
| 697 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Fariborz Jahanian | f96708d | 2012-01-23 23:38:32 +0000 | [diff] [blame] | 698 | 0, AvailabilityLoc, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 699 | Platform, PlatformLoc, |
| 700 | Changes[Introduced], |
| 701 | Changes[Deprecated], |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 702 | Changes[Obsoleted], |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 703 | UnavailableLoc, MessageExpr.take(), |
| 704 | false, false); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 705 | } |
| 706 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 707 | |
| 708 | // Late Parsed Attributes: |
| 709 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 710 | |
| 711 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 712 | |
| 713 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 714 | Self->ParseLexedAttributes(*Class); |
| 715 | } |
| 716 | |
| 717 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 718 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 722 | /// scope appropriately. |
| 723 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 724 | // Deal with templates |
| 725 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 726 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 727 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 728 | HasTemplateScope); |
| 729 | if (HasTemplateScope) |
| 730 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 731 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 732 | // Set or update the scope flags. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 733 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 734 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 735 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 736 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 737 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 738 | // Enter the scope of nested classes |
| 739 | if (!AlreadyHasClassScope) |
| 740 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 741 | Class.TagOrTemplate); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 742 | { |
| 743 | // Allow 'this' within late-parsed attributes. |
| 744 | Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate, |
| 745 | /*TypeQuals=*/0); |
| 746 | |
| 747 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 748 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 749 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 750 | } |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 751 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 752 | if (!AlreadyHasClassScope) |
| 753 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 754 | Class.TagOrTemplate); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 755 | } |
| 756 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 757 | |
| 758 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 759 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 760 | bool EnterScope, bool OnDefinition) { |
| 761 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 762 | LAs[i]->addDecl(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 763 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | d306cf7 | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 764 | delete LAs[i]; |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 765 | } |
| 766 | LAs.clear(); |
| 767 | } |
| 768 | |
| 769 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 770 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 771 | /// This will be called at the end of parsing a class declaration |
| 772 | /// for each LateParsedAttribute. We consume the saved tokens and |
| 773 | /// create an attribute with the arguments filled in. We add this |
| 774 | /// to the Attribute list for the decl. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 775 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 776 | bool EnterScope, bool OnDefinition) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 777 | // Save the current token position. |
| 778 | SourceLocation OrigLoc = Tok.getLocation(); |
| 779 | |
| 780 | // Append the current token at the end of the new token stream so that it |
| 781 | // doesn't get lost. |
| 782 | LA.Toks.push_back(Tok); |
| 783 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 784 | // Consume the previously pushed token. |
| 785 | ConsumeAnyToken(); |
| 786 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 787 | if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) { |
| 788 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 789 | << LA.AttrName.getName(); |
| 790 | } |
| 791 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 792 | ParsedAttributes Attrs(AttrFactory); |
| 793 | SourceLocation endLoc; |
| 794 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 795 | if (LA.Decls.size() == 1) { |
| 796 | Decl *D = LA.Decls[0]; |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 797 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 798 | // If the Decl is templatized, add template parameters to scope. |
| 799 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 800 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 801 | if (HasTemplateScope) |
| 802 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 803 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 804 | // If the Decl is on a function, add function parameters to the scope. |
| 805 | bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
| 806 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope); |
| 807 | if (HasFunctionScope) |
| 808 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
| 809 | |
| 810 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc); |
| 811 | |
| 812 | if (HasFunctionScope) { |
| 813 | Actions.ActOnExitFunctionContext(); |
| 814 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 815 | } |
| 816 | if (HasTemplateScope) { |
| 817 | TempScope.Exit(); |
| 818 | } |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 819 | } else if (LA.Decls.size() > 0) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 820 | // If there are multiple decls, then the decl cannot be within the |
| 821 | // function scope. |
| 822 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc); |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 823 | } else { |
| 824 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 825 | } |
| 826 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 827 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) { |
| 828 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
| 829 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 830 | |
| 831 | if (Tok.getLocation() != OrigLoc) { |
| 832 | // Due to a parsing error, we either went over the cached tokens or |
| 833 | // there are still cached tokens left, so we skip the leftover tokens. |
| 834 | // Since this is an uncommon situation that should be avoided, use the |
| 835 | // expensive isBeforeInTranslationUnit call. |
| 836 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 837 | OrigLoc)) |
| 838 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
Douglas Gregor | d78ef5b | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 839 | ConsumeAnyToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 843 | /// \brief Wrapper around a case statement checking if AttrName is |
| 844 | /// one of the thread safety attributes |
| 845 | bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){ |
| 846 | return llvm::StringSwitch<bool>(AttrName) |
| 847 | .Case("guarded_by", true) |
| 848 | .Case("guarded_var", true) |
| 849 | .Case("pt_guarded_by", true) |
| 850 | .Case("pt_guarded_var", true) |
| 851 | .Case("lockable", true) |
| 852 | .Case("scoped_lockable", true) |
| 853 | .Case("no_thread_safety_analysis", true) |
| 854 | .Case("acquired_after", true) |
| 855 | .Case("acquired_before", true) |
| 856 | .Case("exclusive_lock_function", true) |
| 857 | .Case("shared_lock_function", true) |
| 858 | .Case("exclusive_trylock_function", true) |
| 859 | .Case("shared_trylock_function", true) |
| 860 | .Case("unlock_function", true) |
| 861 | .Case("lock_returned", true) |
| 862 | .Case("locks_excluded", true) |
| 863 | .Case("exclusive_locks_required", true) |
| 864 | .Case("shared_locks_required", true) |
| 865 | .Default(false); |
| 866 | } |
| 867 | |
| 868 | /// \brief Parse the contents of thread safety attributes. These |
| 869 | /// should always be parsed as an expression list. |
| 870 | /// |
| 871 | /// We need to special case the parsing due to the fact that if the first token |
| 872 | /// of the first argument is an identifier, the main parse loop will store |
| 873 | /// that token as a "parameter" and the rest of |
| 874 | /// the arguments will be added to a list of "arguments". However, |
| 875 | /// subsequent tokens in the first argument are lost. We instead parse each |
| 876 | /// argument as an expression and add all arguments to the list of "arguments". |
| 877 | /// In future, we will take advantage of this special case to also |
| 878 | /// deal with some argument scoping issues here (for example, referring to a |
| 879 | /// function parameter in the attribute on that function). |
| 880 | void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName, |
| 881 | SourceLocation AttrNameLoc, |
| 882 | ParsedAttributes &Attrs, |
| 883 | SourceLocation *EndLoc) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 884 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 885 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 886 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 887 | T.consumeOpen(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 888 | |
| 889 | ExprVector ArgExprs(Actions); |
| 890 | bool ArgExprsOk = true; |
| 891 | |
| 892 | // now parse the list of expressions |
DeLesley Hutchins | 4805f15 | 2011-12-14 19:36:06 +0000 | [diff] [blame] | 893 | while (Tok.isNot(tok::r_paren)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 894 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 895 | if (ArgExpr.isInvalid()) { |
| 896 | ArgExprsOk = false; |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 897 | T.consumeClose(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 898 | break; |
| 899 | } else { |
| 900 | ArgExprs.push_back(ArgExpr.release()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 901 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 902 | if (Tok.isNot(tok::comma)) |
| 903 | break; |
| 904 | ConsumeToken(); // Eat the comma, move to the next argument |
| 905 | } |
| 906 | // Match the ')'. |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 907 | if (ArgExprsOk && !T.consumeClose()) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 908 | Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
| 909 | ArgExprs.take(), ArgExprs.size()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 910 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 911 | if (EndLoc) |
| 912 | *EndLoc = T.getCloseLocation(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 913 | } |
| 914 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 915 | /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets |
| 916 | /// of a C++11 attribute-specifier in a location where an attribute is not |
| 917 | /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this |
| 918 | /// situation. |
| 919 | /// |
| 920 | /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if |
| 921 | /// this doesn't appear to actually be an attribute-specifier, and the caller |
| 922 | /// should try to parse it. |
| 923 | bool Parser::DiagnoseProhibitedCXX11Attribute() { |
| 924 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)); |
| 925 | |
| 926 | switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) { |
| 927 | case CAK_NotAttributeSpecifier: |
| 928 | // No diagnostic: we're in Obj-C++11 and this is not actually an attribute. |
| 929 | return false; |
| 930 | |
| 931 | case CAK_InvalidAttributeSpecifier: |
| 932 | Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute); |
| 933 | return false; |
| 934 | |
| 935 | case CAK_AttributeSpecifier: |
| 936 | // Parse and discard the attributes. |
| 937 | SourceLocation BeginLoc = ConsumeBracket(); |
| 938 | ConsumeBracket(); |
| 939 | SkipUntil(tok::r_square, /*StopAtSemi*/ false); |
| 940 | assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied"); |
| 941 | SourceLocation EndLoc = ConsumeBracket(); |
| 942 | Diag(BeginLoc, diag::err_attributes_not_allowed) |
| 943 | << SourceRange(BeginLoc, EndLoc); |
| 944 | return true; |
| 945 | } |
Chandler Carruth | 2c6dbd7 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 946 | llvm_unreachable("All cases handled above."); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 947 | } |
| 948 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 949 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 950 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 951 | << attrs.Range; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 952 | } |
| 953 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 954 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 955 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 956 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 957 | /// location of the semicolon in DeclEnd. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 958 | /// |
| 959 | /// declaration: [C99 6.7] |
| 960 | /// block-declaration -> |
| 961 | /// simple-declaration |
| 962 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 963 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 964 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 965 | /// [C++] using-directive |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 966 | /// [C++] using-declaration |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 967 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 968 | /// others... [FIXME] |
| 969 | /// |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 970 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 971 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 972 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 973 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 974 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | e8cff36 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 975 | // Must temporarily exit the objective-c container scope for |
| 976 | // parsing c none objective-c decls. |
| 977 | ObjCDeclContextSwitch ObjCDC(*this); |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 978 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 979 | Decl *SingleDecl = 0; |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 980 | Decl *OwnedType = 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 981 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 982 | case tok::kw_template: |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 983 | case tok::kw_export: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 984 | ProhibitAttributes(attrs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 985 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 986 | break; |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 987 | case tok::kw_inline: |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 988 | // 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] | 989 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 990 | ProhibitAttributes(attrs); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 991 | SourceLocation InlineLoc = ConsumeToken(); |
| 992 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 993 | break; |
| 994 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 995 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 996 | true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 997 | case tok::kw_namespace: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 998 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 999 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1000 | break; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1001 | case tok::kw_using: |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1002 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1003 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1004 | break; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1005 | case tok::kw_static_assert: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1006 | case tok::kw__Static_assert: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1007 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1008 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1009 | break; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1010 | default: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1011 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1012 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1013 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1014 | // 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] | 1015 | // single decl, convert it now. Alias declarations can also declare a type; |
| 1016 | // include that too if it is present. |
| 1017 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
| 1020 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1021 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 1022 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 1023 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1024 | /// |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1025 | /// for-range-declaration: [C++0x 6.5p1: stmt.ranged] |
| 1026 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1027 | /// |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1028 | /// 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] | 1029 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1030 | /// |
| 1031 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 1032 | /// of a simple-declaration. If we find that we are, we also parse the |
| 1033 | /// for-range-initializer, and place it here. |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1034 | Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts, |
| 1035 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1036 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1037 | ParsedAttributes &attrs, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1038 | bool RequireSemi, |
| 1039 | ForRangeInit *FRI) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1040 | // Parse the common declaration-specifiers piece. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1041 | ParsingDeclSpec DS(*this); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1042 | DS.takeAttributesFrom(attrs); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1043 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1044 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1045 | getDeclSpecContextFromDeclaratorContext(Context)); |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1046 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1047 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1048 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1049 | if (Tok.is(tok::semi)) { |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1050 | if (RequireSemi) ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1051 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1052 | DS); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1053 | DS.complete(TheDecl); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1054 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1055 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1056 | |
| 1057 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1058 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1059 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1060 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1061 | /// for a declarator. |
| 1062 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1063 | switch (Tok.getKind()) { |
| 1064 | case tok::annot_cxxscope: |
| 1065 | case tok::annot_template_id: |
| 1066 | case tok::caret: |
| 1067 | case tok::code_completion: |
| 1068 | case tok::coloncolon: |
| 1069 | case tok::ellipsis: |
| 1070 | case tok::kw___attribute: |
| 1071 | case tok::kw_operator: |
| 1072 | case tok::l_paren: |
| 1073 | case tok::star: |
| 1074 | return true; |
| 1075 | |
| 1076 | case tok::amp: |
| 1077 | case tok::ampamp: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1078 | return getLangOpts().CPlusPlus; |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1079 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1080 | case tok::l_square: // Might be an attribute on an unnamed bit-field. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1081 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x && |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1082 | NextToken().is(tok::l_square); |
| 1083 | |
| 1084 | 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] | 1085 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1086 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1087 | case tok::identifier: |
| 1088 | switch (NextToken().getKind()) { |
| 1089 | case tok::code_completion: |
| 1090 | case tok::coloncolon: |
| 1091 | case tok::comma: |
| 1092 | case tok::equal: |
| 1093 | case tok::equalequal: // Might be a typo for '='. |
| 1094 | case tok::kw_alignas: |
| 1095 | case tok::kw_asm: |
| 1096 | case tok::kw___attribute: |
| 1097 | case tok::l_brace: |
| 1098 | case tok::l_paren: |
| 1099 | case tok::l_square: |
| 1100 | case tok::less: |
| 1101 | case tok::r_brace: |
| 1102 | case tok::r_paren: |
| 1103 | case tok::r_square: |
| 1104 | case tok::semi: |
| 1105 | return true; |
| 1106 | |
| 1107 | case tok::colon: |
| 1108 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1109 | // and in block scope it's probably a label. Inside a class definition, |
| 1110 | // this is a bit-field. |
| 1111 | return Context == Declarator::MemberContext || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1112 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1113 | |
| 1114 | case tok::identifier: // Possible virt-specifier. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1115 | return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken()); |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1116 | |
| 1117 | default: |
| 1118 | return false; |
| 1119 | } |
| 1120 | |
| 1121 | default: |
| 1122 | return false; |
| 1123 | } |
| 1124 | } |
| 1125 | |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1126 | /// Skip until we reach something which seems like a sensible place to pick |
| 1127 | /// up parsing after a malformed declaration. This will sometimes stop sooner |
| 1128 | /// than SkipUntil(tok::r_brace) would, but will never stop later. |
| 1129 | void Parser::SkipMalformedDecl() { |
| 1130 | while (true) { |
| 1131 | switch (Tok.getKind()) { |
| 1132 | case tok::l_brace: |
| 1133 | // Skip until matching }, then stop. We've probably skipped over |
| 1134 | // a malformed class or function definition or similar. |
| 1135 | ConsumeBrace(); |
| 1136 | SkipUntil(tok::r_brace, /*StopAtSemi*/false); |
| 1137 | if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) { |
| 1138 | // This declaration isn't over yet. Keep skipping. |
| 1139 | continue; |
| 1140 | } |
| 1141 | if (Tok.is(tok::semi)) |
| 1142 | ConsumeToken(); |
| 1143 | return; |
| 1144 | |
| 1145 | case tok::l_square: |
| 1146 | ConsumeBracket(); |
| 1147 | SkipUntil(tok::r_square, /*StopAtSemi*/false); |
| 1148 | continue; |
| 1149 | |
| 1150 | case tok::l_paren: |
| 1151 | ConsumeParen(); |
| 1152 | SkipUntil(tok::r_paren, /*StopAtSemi*/false); |
| 1153 | continue; |
| 1154 | |
| 1155 | case tok::r_brace: |
| 1156 | return; |
| 1157 | |
| 1158 | case tok::semi: |
| 1159 | ConsumeToken(); |
| 1160 | return; |
| 1161 | |
| 1162 | case tok::kw_inline: |
| 1163 | // 'inline namespace' at the start of a line is almost certainly |
| 1164 | // a good place to pick back up parsing. |
| 1165 | if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace)) |
| 1166 | return; |
| 1167 | break; |
| 1168 | |
| 1169 | case tok::kw_namespace: |
| 1170 | // 'namespace' at the start of a line is almost certainly a good |
| 1171 | // place to pick back up parsing. |
| 1172 | if (Tok.isAtStartOfLine()) |
| 1173 | return; |
| 1174 | break; |
| 1175 | |
| 1176 | case tok::eof: |
| 1177 | return; |
| 1178 | |
| 1179 | default: |
| 1180 | break; |
| 1181 | } |
| 1182 | |
| 1183 | ConsumeAnyToken(); |
| 1184 | } |
| 1185 | } |
| 1186 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1187 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1188 | /// definition or a group of object declarations, actually parse the |
| 1189 | /// result. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1190 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1191 | unsigned Context, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1192 | bool AllowFunctionDefinitions, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1193 | SourceLocation *DeclEnd, |
| 1194 | ForRangeInit *FRI) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1195 | // Parse the first declarator. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1196 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1197 | ParseDeclarator(D); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1198 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1199 | // Bail out if the first declarator didn't seem well-formed. |
| 1200 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1201 | SkipMalformedDecl(); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1202 | return DeclGroupPtrTy(); |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1203 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1205 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1206 | // appropriate function scope after the function Decl has been constructed. |
| 1207 | LateParsedAttrList LateParsedAttrs; |
| 1208 | if (D.isFunctionDeclarator()) |
| 1209 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1210 | |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1211 | // Check to see if we have a function *definition* which must have a body. |
| 1212 | if (AllowFunctionDefinitions && D.isFunctionDeclarator() && |
| 1213 | // Look at the next token to make sure that this isn't a function |
| 1214 | // declaration. We have to check this because __attribute__ might be the |
| 1215 | // start of a function definition in GCC-extended K&R C. |
| 1216 | !isDeclarationAfterDeclarator()) { |
Richard Smith | 58196dc | 2011-11-30 23:45:35 +0000 | [diff] [blame] | 1217 | |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1218 | if (isStartOfFunctionDefinition(D)) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1219 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1220 | Diag(Tok, diag::err_function_declared_typedef); |
| 1221 | |
| 1222 | // Recover by treating the 'typedef' as spurious. |
| 1223 | DS.ClearStorageClassSpecs(); |
| 1224 | } |
| 1225 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1226 | Decl *TheDecl = |
| 1227 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1228 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | if (isDeclarationSpecifier()) { |
| 1232 | // If there is an invalid declaration specifier right after the function |
| 1233 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1234 | // actually a body. Just fall through into the code that handles it as a |
| 1235 | // prototype, and let the top-level code handle the erroneous declspec |
| 1236 | // where it would otherwise expect a comma or semicolon. |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1237 | } else { |
| 1238 | Diag(Tok, diag::err_expected_fn_body); |
| 1239 | SkipUntil(tok::semi); |
| 1240 | return DeclGroupPtrTy(); |
| 1241 | } |
| 1242 | } |
| 1243 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1244 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1245 | return DeclGroupPtrTy(); |
| 1246 | |
| 1247 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1248 | // must parse and analyze the for-range-initializer before the declaration is |
| 1249 | // analyzed. |
| 1250 | if (FRI && Tok.is(tok::colon)) { |
| 1251 | FRI->ColonLoc = ConsumeToken(); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1252 | if (Tok.is(tok::l_brace)) |
| 1253 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1254 | else |
| 1255 | FRI->RangeExpr = ParseExpression(); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1256 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1257 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
| 1258 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | 6895a64 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 1259 | D.complete(ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1260 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1); |
| 1261 | } |
| 1262 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1263 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1264 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1265 | if (LateParsedAttrs.size() > 0) |
| 1266 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1267 | D.complete(FirstDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1268 | if (FirstDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1269 | DeclsInGroup.push_back(FirstDecl); |
| 1270 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1271 | bool ExpectSemi = Context != Declarator::ForContext; |
| 1272 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1273 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1274 | // error, bail out. |
| 1275 | while (Tok.is(tok::comma)) { |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1276 | SourceLocation CommaLoc = ConsumeToken(); |
| 1277 | |
| 1278 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1279 | // This comma was followed by a line-break and something which can't be |
| 1280 | // the start of a declarator. The comma was probably a typo for a |
| 1281 | // semicolon. |
| 1282 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1283 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1284 | ExpectSemi = false; |
| 1285 | break; |
| 1286 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1287 | |
| 1288 | // Parse the next declarator. |
| 1289 | D.clear(); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1290 | D.setCommaLoc(CommaLoc); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1291 | |
| 1292 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1293 | // declaration, these would be part of the declspec. In subsequent |
| 1294 | // declarators, they become part of the declarator itself, so that they |
| 1295 | // don't apply to declarators after *this* one. Examples: |
| 1296 | // short __attribute__((common)) var; -> declspec |
| 1297 | // short var __attribute__((common)); -> declarator |
| 1298 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1299 | MaybeParseGNUAttributes(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1300 | |
| 1301 | ParseDeclarator(D); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1302 | if (!D.isInvalidType()) { |
| 1303 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1304 | D.complete(ThisDecl); |
| 1305 | if (ThisDecl) |
| 1306 | DeclsInGroup.push_back(ThisDecl); |
| 1307 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | if (DeclEnd) |
| 1311 | *DeclEnd = Tok.getLocation(); |
| 1312 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1313 | if (ExpectSemi && |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1314 | ExpectAndConsume(tok::semi, |
| 1315 | Context == Declarator::FileContext |
| 1316 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1317 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1318 | // Okay, there was no semicolon and one was expected. If we see a |
| 1319 | // declaration specifier, just assume it was missing and continue parsing. |
| 1320 | // Otherwise things are very confused and we skip to recover. |
| 1321 | if (!isDeclarationSpecifier()) { |
| 1322 | SkipUntil(tok::r_brace, true, true); |
| 1323 | if (Tok.is(tok::semi)) |
| 1324 | ConsumeToken(); |
| 1325 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1328 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1329 | DeclsInGroup.data(), |
| 1330 | DeclsInGroup.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1333 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1334 | /// declarator. Returns true on an error. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1335 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1336 | // If a simple-asm-expr is present, parse it. |
| 1337 | if (Tok.is(tok::kw_asm)) { |
| 1338 | SourceLocation Loc; |
| 1339 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1340 | if (AsmLabel.isInvalid()) { |
| 1341 | SkipUntil(tok::semi, true, true); |
| 1342 | return true; |
| 1343 | } |
| 1344 | |
| 1345 | D.setAsmLabel(AsmLabel.release()); |
| 1346 | D.SetRangeEnd(Loc); |
| 1347 | } |
| 1348 | |
| 1349 | MaybeParseGNUAttributes(D); |
| 1350 | return false; |
| 1351 | } |
| 1352 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1353 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1354 | /// declarator'. This method parses the remainder of the declaration |
| 1355 | /// (including any attributes or initializer, among other things) and |
| 1356 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1357 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1358 | /// init-declarator: [C99 6.7] |
| 1359 | /// declarator |
| 1360 | /// declarator '=' initializer |
| 1361 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1362 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1363 | /// [C++] declarator initializer[opt] |
| 1364 | /// |
| 1365 | /// [C++] initializer: |
| 1366 | /// [C++] '=' initializer-clause |
| 1367 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1368 | /// [C++0x] '=' 'default' [TODO] |
| 1369 | /// [C++0x] '=' 'delete' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1370 | /// [C++0x] braced-init-list |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1371 | /// |
| 1372 | /// According to the standard grammar, =default and =delete are function |
| 1373 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1374 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1375 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1376 | const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1377 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1378 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1379 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1380 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1381 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1382 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1383 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1384 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1385 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1386 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1387 | switch (TemplateInfo.Kind) { |
| 1388 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1389 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1390 | break; |
| 1391 | |
| 1392 | case ParsedTemplateInfo::Template: |
| 1393 | case ParsedTemplateInfo::ExplicitSpecialization: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1394 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1395 | MultiTemplateParamsArg(Actions, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1396 | TemplateInfo.TemplateParams->data(), |
| 1397 | TemplateInfo.TemplateParams->size()), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1398 | D); |
| 1399 | break; |
| 1400 | |
| 1401 | case ParsedTemplateInfo::ExplicitInstantiation: { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1402 | DeclResult ThisRes |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1403 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1404 | TemplateInfo.ExternLoc, |
| 1405 | TemplateInfo.TemplateLoc, |
| 1406 | D); |
| 1407 | if (ThisRes.isInvalid()) { |
| 1408 | SkipUntil(tok::semi, true, true); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1409 | return 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | ThisDecl = ThisRes.get(); |
| 1413 | break; |
| 1414 | } |
| 1415 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1416 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1417 | bool TypeContainsAuto = |
| 1418 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
| 1419 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1420 | // Parse declarator '=' initializer. |
Richard Trieu | d6c7c67 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1421 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | fcaf27e | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1422 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1423 | ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1424 | if (Tok.is(tok::kw_delete)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1425 | if (D.isFunctionDeclarator()) |
| 1426 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1427 | << 1 /* delete */; |
| 1428 | else |
| 1429 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Sean Hunt | fe2695e | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1430 | } else if (Tok.is(tok::kw_default)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1431 | if (D.isFunctionDeclarator()) |
Sebastian Redl | ecfcd56 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1432 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1433 | << 0 /* default */; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1434 | else |
| 1435 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1436 | } else { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1437 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1438 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1439 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1440 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1441 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1442 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1443 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1444 | cutOffParsing(); |
| 1445 | return 0; |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1448 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1449 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1450 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1451 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1452 | ExitScope(); |
| 1453 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1454 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1455 | if (Init.isInvalid()) { |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1456 | SkipUntil(tok::comma, true, true); |
| 1457 | Actions.ActOnInitializerError(ThisDecl); |
| 1458 | } else |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1459 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1460 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1461 | } |
| 1462 | } else if (Tok.is(tok::l_paren)) { |
| 1463 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1464 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1465 | T.consumeOpen(); |
| 1466 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1467 | ExprVector Exprs(Actions); |
| 1468 | CommaLocsTy CommaLocs; |
| 1469 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1470 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1471 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1472 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1475 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 1476 | SkipUntil(tok::r_paren); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1477 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1478 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1479 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1480 | ExitScope(); |
| 1481 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1482 | } else { |
| 1483 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1484 | T.consumeClose(); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1485 | |
| 1486 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1487 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1488 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1489 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1490 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1491 | ExitScope(); |
| 1492 | } |
| 1493 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1494 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 1495 | T.getCloseLocation(), |
| 1496 | move_arg(Exprs)); |
| 1497 | Actions.AddInitializerToDecl(ThisDecl, Initializer.take(), |
| 1498 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1499 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1500 | } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) { |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1501 | // Parse C++0x braced-init-list. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1502 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1503 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1504 | if (D.getCXXScopeSpec().isSet()) { |
| 1505 | EnterScope(0); |
| 1506 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1507 | } |
| 1508 | |
| 1509 | ExprResult Init(ParseBraceInitializer()); |
| 1510 | |
| 1511 | if (D.getCXXScopeSpec().isSet()) { |
| 1512 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1513 | ExitScope(); |
| 1514 | } |
| 1515 | |
| 1516 | if (Init.isInvalid()) { |
| 1517 | Actions.ActOnInitializerError(ThisDecl); |
| 1518 | } else |
| 1519 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1520 | /*DirectInit=*/true, TypeContainsAuto); |
| 1521 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1522 | } else { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1523 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1526 | Actions.FinalizeDeclaration(ThisDecl); |
| 1527 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1528 | return ThisDecl; |
| 1529 | } |
| 1530 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1531 | /// ParseSpecifierQualifierList |
| 1532 | /// specifier-qualifier-list: |
| 1533 | /// type-specifier specifier-qualifier-list[opt] |
| 1534 | /// type-qualifier specifier-qualifier-list[opt] |
| 1535 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 1536 | /// |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1537 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 1538 | DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1539 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1540 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1541 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1542 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1543 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1544 | // Validate declspec for type-name. |
| 1545 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1546 | if (DSC == DSC_type_specifier && !DS.hasTypeSpecifier()) { |
| 1547 | Diag(Tok, diag::err_expected_type); |
| 1548 | DS.SetTypeSpecError(); |
| 1549 | } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 1550 | !DS.hasAttributes()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1551 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1552 | if (!DS.hasTypeSpecifier()) |
| 1553 | DS.SetTypeSpecError(); |
| 1554 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1556 | // Issue diagnostic and remove storage class if present. |
| 1557 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 1558 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1559 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 1560 | else |
| 1561 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
| 1562 | DS.ClearStorageClassSpecs(); |
| 1563 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1564 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1565 | // Issue diagnostic and remove function specfier if present. |
| 1566 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1567 | if (DS.isInlineSpecified()) |
| 1568 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1569 | if (DS.isVirtualSpecified()) |
| 1570 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1571 | if (DS.isExplicitSpecified()) |
| 1572 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1573 | DS.ClearFunctionSpecs(); |
| 1574 | } |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1575 | |
| 1576 | // Issue diagnostic and remove constexpr specfier if present. |
| 1577 | if (DS.isConstexprSpecified()) { |
| 1578 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 1579 | DS.ClearConstexprSpec(); |
| 1580 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1583 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 1584 | /// specified token is valid after the identifier in a declarator which |
| 1585 | /// immediately follows the declspec. For example, these things are valid: |
| 1586 | /// |
| 1587 | /// int x [ 4]; // direct-declarator |
| 1588 | /// int x ( int y); // direct-declarator |
| 1589 | /// int(int x ) // direct-declarator |
| 1590 | /// int x ; // simple-declaration |
| 1591 | /// int x = 17; // init-declarator-list |
| 1592 | /// int x , y; // init-declarator-list |
| 1593 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1594 | /// int x : 4; // struct-declarator |
Chris Lattner | c83c27a | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 1595 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1596 | /// |
| 1597 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 1598 | /// ')' happens to be valid anyway). |
| 1599 | /// int (x) |
| 1600 | /// |
| 1601 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 1602 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 1603 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1604 | 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] | 1605 | } |
| 1606 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1607 | |
| 1608 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 1609 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 1610 | /// the declspec has no type specifier. In this case, the declspec is either |
| 1611 | /// malformed or is "implicit int" (in K&R and C89). |
| 1612 | /// |
| 1613 | /// This method handles diagnosing this prettily and returns false if the |
| 1614 | /// declspec is done being processed. If it recovers and thinks there may be |
| 1615 | /// other pieces of declspec after it, it returns true. |
| 1616 | /// |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1617 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1618 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1619 | AccessSpecifier AS, DeclSpecContext DSC) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1620 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1621 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1622 | SourceLocation Loc = Tok.getLocation(); |
| 1623 | // If we see an identifier that is not a type name, we normally would |
| 1624 | // parse it as the identifer being declared. However, when a typename |
| 1625 | // is typo'd or the definition is not included, this will incorrectly |
| 1626 | // parse the typename as the identifier name and fall over misparsing |
| 1627 | // later parts of the diagnostic. |
| 1628 | // |
| 1629 | // As such, we try to do some look-ahead in cases where this would |
| 1630 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 1631 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 1632 | // an identifier with implicit int, we'd get a parse error because the |
| 1633 | // next token is obviously invalid for a type. Parse these as a case |
| 1634 | // with an invalid type specifier. |
| 1635 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1636 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1637 | // Since we know that this either implicit int (which is rare) or an |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1638 | // error, do lookahead to try to do better recovery. This never applies within |
| 1639 | // a type specifier. |
| 1640 | // FIXME: Don't bail out here in languages with no implicit int (like |
| 1641 | // C++ with no -fms-extensions). This is much more likely to be an undeclared |
| 1642 | // type or typo than a use of implicit int. |
| 1643 | if (DSC != DSC_type_specifier && |
| 1644 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1645 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 1646 | // we just avoid eating the identifier, so it will be parsed as the |
| 1647 | // identifier in the declarator. |
| 1648 | return false; |
| 1649 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1650 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1651 | // Otherwise, if we don't consume this token, we are going to emit an |
| 1652 | // error anyway. Try to recover from various common problems. Check |
| 1653 | // to see if this was a reference to a tag name without a tag specified. |
| 1654 | // 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] | 1655 | // |
| 1656 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 1657 | if (SS == 0) { |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1658 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1659 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1660 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1661 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1662 | default: break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1663 | case DeclSpec::TST_enum: |
| 1664 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 1665 | case DeclSpec::TST_union: |
| 1666 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 1667 | case DeclSpec::TST_struct: |
| 1668 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
| 1669 | case DeclSpec::TST_class: |
| 1670 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1671 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1672 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1673 | if (TagName) { |
| 1674 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1675 | << Tok.getIdentifierInfo() << TagName << getLangOpts().CPlusPlus |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1676 | << FixItHint::CreateInsertion(Tok.getLocation(),FixitTagName); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1677 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1678 | // Parse this as a tag as if the missing tag were present. |
| 1679 | if (TagKind == tok::kw_enum) |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1680 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1681 | else |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1682 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
| 1683 | /*EnteringContext*/ false, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1684 | return true; |
| 1685 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1686 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1687 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1688 | // This is almost certainly an invalid type name. Let the action emit a |
| 1689 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1690 | ParsedType T; |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1691 | if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc, |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1692 | getCurScope(), SS, T)) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1693 | // The action emitted a diagnostic, so we don't have to. |
| 1694 | if (T) { |
| 1695 | // The action has suggested that the type T could be used. Set that as |
| 1696 | // the type in the declaration specifiers, consume the would-be type |
| 1697 | // name token, and we're done. |
| 1698 | const char *PrevSpec; |
| 1699 | unsigned DiagID; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1700 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1701 | DS.SetRangeEnd(Tok.getLocation()); |
| 1702 | ConsumeToken(); |
| 1703 | |
| 1704 | // There may be other declaration specifiers after this. |
| 1705 | return true; |
| 1706 | } |
| 1707 | |
| 1708 | // Fall through; the action had no suggestion for us. |
| 1709 | } else { |
| 1710 | // The action did not emit a diagnostic, so emit one now. |
| 1711 | SourceRange R; |
| 1712 | if (SS) R = SS->getRange(); |
| 1713 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 1714 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1715 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1716 | // Mark this as an error. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1717 | DS.SetTypeSpecError(); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1718 | DS.SetRangeEnd(Tok.getLocation()); |
| 1719 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1720 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1721 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 1722 | // avoid rippling error messages on subsequent uses of the same type, |
| 1723 | // could be useful if #include was forgotten. |
| 1724 | return false; |
| 1725 | } |
| 1726 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1727 | /// \brief Determine the declaration specifier context from the declarator |
| 1728 | /// context. |
| 1729 | /// |
| 1730 | /// \param Context the declarator context, which is one of the |
| 1731 | /// Declarator::TheContext enumerator values. |
| 1732 | Parser::DeclSpecContext |
| 1733 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 1734 | if (Context == Declarator::MemberContext) |
| 1735 | return DSC_class; |
| 1736 | if (Context == Declarator::FileContext) |
| 1737 | return DSC_top_level; |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 1738 | if (Context == Declarator::TrailingReturnContext) |
| 1739 | return DSC_trailing; |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1740 | return DSC_normal; |
| 1741 | } |
| 1742 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1743 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 1744 | /// |
| 1745 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 1746 | /// type. Ideally, the type should be propagated directly into Sema. |
| 1747 | /// |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1748 | /// [C11] type-id |
| 1749 | /// [C11] constant-expression |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1750 | /// [C++0x] type-id ...[opt] |
| 1751 | /// [C++0x] assignment-expression ...[opt] |
| 1752 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 1753 | SourceLocation &EllipsisLoc) { |
| 1754 | ExprResult ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1755 | if (isTypeIdInParens()) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1756 | SourceLocation TypeLoc = Tok.getLocation(); |
| 1757 | ParsedType Ty = ParseTypeName().get(); |
| 1758 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1759 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 1760 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1761 | } else |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1762 | ER = ParseConstantExpression(); |
| 1763 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1764 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis)) |
Peter Collingbourne | fe9b2a8 | 2011-10-24 17:56:00 +0000 | [diff] [blame] | 1765 | EllipsisLoc = ConsumeToken(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1766 | |
| 1767 | return ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 1771 | /// attribute to Attrs. |
| 1772 | /// |
| 1773 | /// alignment-specifier: |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1774 | /// [C11] '_Alignas' '(' type-id ')' |
| 1775 | /// [C11] '_Alignas' '(' constant-expression ')' |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1776 | /// [C++0x] 'alignas' '(' type-id ...[opt] ')' |
| 1777 | /// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1778 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
| 1779 | SourceLocation *endLoc) { |
| 1780 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 1781 | "Not an alignment-specifier!"); |
| 1782 | |
| 1783 | SourceLocation KWLoc = Tok.getLocation(); |
| 1784 | ConsumeToken(); |
| 1785 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1786 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1787 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1788 | return; |
| 1789 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1790 | SourceLocation EllipsisLoc; |
| 1791 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1792 | if (ArgExpr.isInvalid()) { |
| 1793 | SkipUntil(tok::r_paren); |
| 1794 | return; |
| 1795 | } |
| 1796 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1797 | T.consumeClose(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1798 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1799 | *endLoc = T.getCloseLocation(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1800 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1801 | // FIXME: Handle pack-expansions here. |
| 1802 | if (EllipsisLoc.isValid()) { |
| 1803 | Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported); |
| 1804 | return; |
| 1805 | } |
| 1806 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1807 | ExprVector ArgExprs(Actions); |
| 1808 | ArgExprs.push_back(ArgExpr.release()); |
| 1809 | Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1810 | 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1813 | /// ParseDeclarationSpecifiers |
| 1814 | /// declaration-specifiers: [C99 6.7] |
| 1815 | /// storage-class-specifier declaration-specifiers[opt] |
| 1816 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1817 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1818 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1819 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1820 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1821 | /// |
| 1822 | /// storage-class-specifier: [C99 6.7.1] |
| 1823 | /// 'typedef' |
| 1824 | /// 'extern' |
| 1825 | /// 'static' |
| 1826 | /// 'auto' |
| 1827 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1828 | /// [C++] 'mutable' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1829 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1830 | /// function-specifier: [C99 6.7.4] |
| 1831 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1832 | /// [C++] 'virtual' |
| 1833 | /// [C++] 'explicit' |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1834 | /// [OpenCL] '__kernel' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1835 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 1836 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1837 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1838 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1839 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1840 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1841 | AccessSpecifier AS, |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1842 | DeclSpecContext DSContext, |
| 1843 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1844 | if (DS.getSourceRange().isInvalid()) { |
| 1845 | DS.SetRangeStart(Tok.getLocation()); |
| 1846 | DS.SetRangeEnd(Tok.getLocation()); |
| 1847 | } |
| 1848 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 1849 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1850 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1851 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1852 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1853 | unsigned DiagID = 0; |
| 1854 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1855 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1856 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1857 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1858 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 1859 | DoneWithDeclSpec: |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 1860 | // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt] |
| 1861 | MaybeParseCXX0XAttributes(DS.getAttributes()); |
| 1862 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1863 | // If this is not a declaration specifier token, we're done reading decl |
| 1864 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 1865 | DS.Finish(Diags, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1866 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1867 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1868 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1869 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1870 | if (DS.hasTypeSpecifier()) { |
| 1871 | bool AllowNonIdentifiers |
| 1872 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 1873 | Scope::BlockScope | |
| 1874 | Scope::TemplateParamScope | |
| 1875 | Scope::FunctionPrototypeScope | |
| 1876 | Scope::AtCatchScope)) == 0; |
| 1877 | bool AllowNestedNameSpecifiers |
| 1878 | = DSContext == DSC_top_level || |
| 1879 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 1880 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 1881 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
| 1882 | AllowNonIdentifiers, |
| 1883 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1884 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1887 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 1888 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 1889 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1890 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
| 1891 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1892 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1893 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1894 | else if (CurParsedObjCImpl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1895 | CCC = Sema::PCC_ObjCImplementation; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1896 | |
| 1897 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1898 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 1901 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1902 | // C++ scope specifier. Annotate and loop, or bail out on error. |
| 1903 | if (TryAnnotateCXXScopeToken(true)) { |
| 1904 | if (!DS.hasTypeSpecifier()) |
| 1905 | DS.SetTypeSpecError(); |
| 1906 | goto DoneWithDeclSpec; |
| 1907 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 1908 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 1909 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1910 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1911 | |
| 1912 | case tok::annot_cxxscope: { |
| 1913 | if (DS.hasTypeSpecifier()) |
| 1914 | goto DoneWithDeclSpec; |
| 1915 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1916 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 1917 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 1918 | Tok.getAnnotationRange(), |
| 1919 | SS); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1920 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1921 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1922 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1923 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1924 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1925 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1926 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1927 | |
| 1928 | // C++ [class.qual]p2: |
| 1929 | // In a lookup in which the constructor is an acceptable lookup |
| 1930 | // result and the nested-name-specifier nominates a class C: |
| 1931 | // |
| 1932 | // - if the name specified after the |
| 1933 | // nested-name-specifier, when looked up in C, is the |
| 1934 | // injected-class-name of C (Clause 9), or |
| 1935 | // |
| 1936 | // - if the name specified after the nested-name-specifier |
| 1937 | // is the same as the identifier or the |
| 1938 | // simple-template-id's template-name in the last |
| 1939 | // component of the nested-name-specifier, |
| 1940 | // |
| 1941 | // the name is instead considered to name the constructor of |
| 1942 | // class C. |
| 1943 | // |
| 1944 | // Thus, if the template-name is actually the constructor |
| 1945 | // name, then the code is ill-formed; this interpretation is |
| 1946 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 1947 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 1948 | if ((DSContext == DSC_top_level || |
| 1949 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
| 1950 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1951 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1952 | if (isConstructorDeclarator()) { |
| 1953 | // The user meant this to be an out-of-line constructor |
| 1954 | // definition, but template arguments are not allowed |
| 1955 | // there. Just allow this as a constructor; we'll |
| 1956 | // complain about it later. |
| 1957 | goto DoneWithDeclSpec; |
| 1958 | } |
| 1959 | |
| 1960 | // The user meant this to name a type, but it actually names |
| 1961 | // a constructor with some extraneous template |
| 1962 | // arguments. Complain, then parse it as a type as the user |
| 1963 | // intended. |
| 1964 | Diag(TemplateId->TemplateNameLoc, |
| 1965 | diag::err_out_of_line_template_id_names_constructor) |
| 1966 | << TemplateId->Name; |
| 1967 | } |
| 1968 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1969 | DS.getTypeSpecScope() = SS; |
| 1970 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1971 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1972 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1973 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1974 | continue; |
| 1975 | } |
| 1976 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1977 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1978 | DS.getTypeSpecScope() = SS; |
| 1979 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1980 | if (Tok.getAnnotationValue()) { |
| 1981 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 1982 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
| 1983 | Tok.getAnnotationEndLoc(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1984 | PrevSpec, DiagID, T); |
| 1985 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1986 | else |
| 1987 | DS.SetTypeSpecError(); |
| 1988 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 1989 | ConsumeToken(); // The typename |
| 1990 | } |
| 1991 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1992 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1993 | goto DoneWithDeclSpec; |
| 1994 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1995 | // If we're in a context where the identifier could be a class name, |
| 1996 | // check whether this is a constructor declaration. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 1997 | if ((DSContext == DSC_top_level || |
| 1998 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1999 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2000 | &SS)) { |
| 2001 | if (isConstructorDeclarator()) |
| 2002 | goto DoneWithDeclSpec; |
| 2003 | |
| 2004 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 2005 | // of the class is qualified in a context where it could name |
| 2006 | // a constructor, its a constructor name. However, we've |
| 2007 | // looked at the declarator, and the user probably meant this |
| 2008 | // to be a type. Complain that it isn't supposed to be treated |
| 2009 | // as a type, then proceed to parse it as a type. |
| 2010 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 2011 | << Next.getIdentifierInfo(); |
| 2012 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2013 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2014 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 2015 | Next.getLocation(), |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2016 | getCurScope(), &SS, |
| 2017 | false, false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2018 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2019 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2020 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2021 | // If the referenced identifier is not a type, then this declspec is |
| 2022 | // erroneous: We already checked about that it has no type specifier, and |
| 2023 | // 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] | 2024 | // typename. |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2025 | if (TypeRep == 0) { |
| 2026 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2027 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2028 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2029 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2030 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2031 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2032 | ConsumeToken(); // The C++ scope. |
| 2033 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2034 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2035 | DiagID, TypeRep); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2036 | if (isInvalid) |
| 2037 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2038 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2039 | DS.SetRangeEnd(Tok.getLocation()); |
| 2040 | ConsumeToken(); // The typename. |
| 2041 | |
| 2042 | continue; |
| 2043 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2044 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2045 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2046 | if (Tok.getAnnotationValue()) { |
| 2047 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | c43271e | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 2048 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2049 | DiagID, T); |
| 2050 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2051 | DS.SetTypeSpecError(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 2052 | |
| 2053 | if (isInvalid) |
| 2054 | break; |
| 2055 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2056 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2057 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2058 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2059 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2060 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2061 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2062 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2063 | ParseObjCProtocolQualifiers(DS); |
| 2064 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2065 | continue; |
| 2066 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2067 | |
Douglas Gregor | bfad915 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2068 | case tok::kw___is_signed: |
| 2069 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 2070 | // typically treats it as a trait. If we see __is_signed as it appears |
| 2071 | // in libstdc++, e.g., |
| 2072 | // |
| 2073 | // static const bool __is_signed; |
| 2074 | // |
| 2075 | // then treat __is_signed as an identifier rather than as a keyword. |
| 2076 | if (DS.getTypeSpecType() == TST_bool && |
| 2077 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
| 2078 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 2079 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
| 2080 | Tok.setKind(tok::identifier); |
| 2081 | } |
| 2082 | |
| 2083 | // We're done with the declaration-specifiers. |
| 2084 | goto DoneWithDeclSpec; |
| 2085 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2086 | // typedef-name |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2087 | case tok::kw_decltype: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2088 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2089 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 2090 | // so handle it as such. This is important for ctor parsing. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2091 | if (getLangOpts().CPlusPlus) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2092 | if (TryAnnotateCXXScopeToken(true)) { |
| 2093 | if (!DS.hasTypeSpecifier()) |
| 2094 | DS.SetTypeSpecError(); |
| 2095 | goto DoneWithDeclSpec; |
| 2096 | } |
| 2097 | if (!Tok.is(tok::identifier)) |
| 2098 | continue; |
| 2099 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2100 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2101 | // This identifier can only be a typedef name if we haven't already seen |
| 2102 | // a type-specifier. Without this check we misparse: |
| 2103 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 2104 | if (DS.hasTypeSpecifier()) |
| 2105 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2106 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2107 | // Check for need to substitute AltiVec keyword tokens. |
| 2108 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2109 | break; |
| 2110 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2111 | ParsedType TypeRep = |
| 2112 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 2113 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2114 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2115 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 2116 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2117 | if (!TypeRep) { |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2118 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2119 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2120 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2121 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2122 | // If we're in a context where the identifier could be a class name, |
| 2123 | // check whether this is a constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2124 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2125 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2126 | isConstructorDeclarator()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2127 | goto DoneWithDeclSpec; |
| 2128 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2129 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2130 | DiagID, TypeRep); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2131 | if (isInvalid) |
| 2132 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2133 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2134 | DS.SetRangeEnd(Tok.getLocation()); |
| 2135 | ConsumeToken(); // The identifier |
| 2136 | |
| 2137 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2138 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2139 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2140 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2141 | ParseObjCProtocolQualifiers(DS); |
| 2142 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2143 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2144 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2145 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2146 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2147 | |
| 2148 | // type-name |
| 2149 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2150 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2151 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2152 | // This template-id does not refer to a type name, so we're |
| 2153 | // done with the type-specifiers. |
| 2154 | goto DoneWithDeclSpec; |
| 2155 | } |
| 2156 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2157 | // If we're in a context where the template-id could be a |
| 2158 | // constructor name or specialization, check whether this is a |
| 2159 | // constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2160 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2161 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2162 | isConstructorDeclarator()) |
| 2163 | goto DoneWithDeclSpec; |
| 2164 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2165 | // Turn the template-id annotation token into a type annotation |
| 2166 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2167 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2168 | continue; |
| 2169 | } |
| 2170 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2171 | // GNU attributes support. |
| 2172 | case tok::kw___attribute: |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2173 | ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2174 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2175 | |
| 2176 | // Microsoft declspec support. |
| 2177 | case tok::kw___declspec: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2178 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2179 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2180 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2181 | // Microsoft single token adornments. |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2182 | case tok::kw___forceinline: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2183 | // FIXME: Add handling here! |
| 2184 | break; |
| 2185 | |
| 2186 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2187 | case tok::kw___ptr32: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2188 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2189 | case tok::kw___cdecl: |
| 2190 | case tok::kw___stdcall: |
| 2191 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2192 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2193 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2194 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2195 | continue; |
| 2196 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2197 | // Borland single token adornments. |
| 2198 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2199 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2200 | continue; |
| 2201 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2202 | // OpenCL single token adornments. |
| 2203 | case tok::kw___kernel: |
| 2204 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2205 | continue; |
| 2206 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2207 | // storage-class-specifier |
| 2208 | case tok::kw_typedef: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2209 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
| 2210 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2211 | break; |
| 2212 | case tok::kw_extern: |
| 2213 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2214 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2215 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
| 2216 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2217 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2218 | case tok::kw___private_extern__: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2219 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
| 2220 | Loc, PrevSpec, DiagID); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2221 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2222 | case tok::kw_static: |
| 2223 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2224 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2225 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
| 2226 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2227 | break; |
| 2228 | case tok::kw_auto: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2229 | if (getLangOpts().CPlusPlus0x) { |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2230 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2231 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2232 | PrevSpec, DiagID); |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2233 | if (!isInvalid) |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2234 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2235 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2236 | } else |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2237 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 2238 | DiagID); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2239 | } else |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2240 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2241 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2242 | break; |
| 2243 | case tok::kw_register: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2244 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
| 2245 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2246 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2247 | case tok::kw_mutable: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2248 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
| 2249 | PrevSpec, DiagID); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2250 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2251 | case tok::kw___thread: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2252 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2253 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2254 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2255 | // function-specifier |
| 2256 | case tok::kw_inline: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2257 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2258 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2259 | case tok::kw_virtual: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2260 | isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2261 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2262 | case tok::kw_explicit: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2263 | isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2264 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2265 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2266 | // alignment-specifier |
| 2267 | case tok::kw__Alignas: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2268 | if (!getLangOpts().C11) |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2269 | Diag(Tok, diag::ext_c11_alignas); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2270 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2271 | continue; |
| 2272 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2273 | // friend |
| 2274 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2275 | if (DSContext == DSC_class) |
| 2276 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 2277 | else { |
| 2278 | PrevSpec = ""; // not actually used by the diagnostic |
| 2279 | DiagID = diag::err_friend_invalid_in_context; |
| 2280 | isInvalid = true; |
| 2281 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2282 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2283 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2284 | // Modules |
| 2285 | case tok::kw___module_private__: |
| 2286 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 2287 | break; |
| 2288 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2289 | // constexpr |
| 2290 | case tok::kw_constexpr: |
| 2291 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 2292 | break; |
| 2293 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2294 | // type-specifier |
| 2295 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2296 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 2297 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2298 | break; |
| 2299 | case tok::kw_long: |
| 2300 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2301 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2302 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2303 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2304 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2305 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2306 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2307 | case tok::kw___int64: |
| 2308 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2309 | DiagID); |
| 2310 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2311 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2312 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 2313 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2314 | break; |
| 2315 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2316 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2317 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2318 | break; |
| 2319 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2320 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2321 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2322 | break; |
| 2323 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2324 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2325 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2326 | break; |
| 2327 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2328 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 2329 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2330 | break; |
| 2331 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2332 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 2333 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2334 | break; |
| 2335 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2336 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 2337 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2338 | break; |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 2339 | case tok::kw___int128: |
| 2340 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
| 2341 | DiagID); |
| 2342 | break; |
| 2343 | case tok::kw_half: |
| 2344 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
| 2345 | DiagID); |
| 2346 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2347 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2348 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 2349 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2350 | break; |
| 2351 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2352 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 2353 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2354 | break; |
| 2355 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2356 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 2357 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2358 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2359 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2360 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 2361 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2362 | break; |
| 2363 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2364 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 2365 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2366 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2367 | case tok::kw_bool: |
| 2368 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2369 | if (Tok.is(tok::kw_bool) && |
| 2370 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 2371 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 2372 | PrevSpec = ""; // Not used by the diagnostic. |
| 2373 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2374 | // For better error recovery. |
| 2375 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2376 | isInvalid = true; |
| 2377 | } else { |
| 2378 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 2379 | DiagID); |
| 2380 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2381 | break; |
| 2382 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2383 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2384 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2385 | break; |
| 2386 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2387 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2388 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2389 | break; |
| 2390 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2391 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2392 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2393 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2394 | case tok::kw___vector: |
| 2395 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2396 | break; |
| 2397 | case tok::kw___pixel: |
| 2398 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2399 | break; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 2400 | case tok::kw___unknown_anytype: |
| 2401 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 2402 | PrevSpec, DiagID); |
| 2403 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2404 | |
| 2405 | // class-specifier: |
| 2406 | case tok::kw_class: |
| 2407 | case tok::kw_struct: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2408 | case tok::kw_union: { |
| 2409 | tok::TokenKind Kind = Tok.getKind(); |
| 2410 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2411 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
| 2412 | EnteringContext, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2413 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2414 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2415 | |
| 2416 | // enum-specifier: |
| 2417 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2418 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2419 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2420 | continue; |
| 2421 | |
| 2422 | // cv-qualifier: |
| 2423 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2424 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2425 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2426 | break; |
| 2427 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2428 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2429 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2430 | break; |
| 2431 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2432 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2433 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2434 | break; |
| 2435 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2436 | // C++ typename-specifier: |
| 2437 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2438 | if (TryAnnotateTypeOrScopeToken()) { |
| 2439 | DS.SetTypeSpecError(); |
| 2440 | goto DoneWithDeclSpec; |
| 2441 | } |
| 2442 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2443 | continue; |
| 2444 | break; |
| 2445 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2446 | // GNU typeof support. |
| 2447 | case tok::kw_typeof: |
| 2448 | ParseTypeofSpecifier(DS); |
| 2449 | continue; |
| 2450 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2451 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2452 | ParseDecltypeSpecifier(DS); |
| 2453 | continue; |
| 2454 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2455 | case tok::kw___underlying_type: |
| 2456 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2457 | continue; |
| 2458 | |
| 2459 | case tok::kw__Atomic: |
| 2460 | ParseAtomicSpecifier(DS); |
| 2461 | continue; |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2462 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2463 | // OpenCL qualifiers: |
| 2464 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2465 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2466 | goto DoneWithDeclSpec; |
| 2467 | case tok::kw___private: |
| 2468 | case tok::kw___global: |
| 2469 | case tok::kw___local: |
| 2470 | case tok::kw___constant: |
| 2471 | case tok::kw___read_only: |
| 2472 | case tok::kw___write_only: |
| 2473 | case tok::kw___read_write: |
| 2474 | ParseOpenCLQualifiers(DS); |
| 2475 | break; |
| 2476 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 2477 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2478 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2479 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 2480 | // but we support it. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2481 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2482 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2483 | |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 2484 | if (!ParseObjCProtocolQualifiers(DS)) |
| 2485 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 2486 | << FixItHint::CreateInsertion(Loc, "id") |
| 2487 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2488 | |
| 2489 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2490 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2491 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2492 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2493 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2494 | if (isInvalid) { |
| 2495 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2496 | assert(DiagID); |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 2497 | |
| 2498 | if (DiagID == diag::ext_duplicate_declspec) |
| 2499 | Diag(Tok, DiagID) |
| 2500 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 2501 | else |
| 2502 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2503 | } |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2504 | |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 2505 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2506 | if (DiagID != diag::err_bool_redeclaration) |
| 2507 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2508 | } |
| 2509 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 2510 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2511 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 2512 | /// semicolon. |
| 2513 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2514 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2515 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2516 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2517 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2518 | /// struct-declarator-list: |
| 2519 | /// struct-declarator |
| 2520 | /// struct-declarator-list ',' struct-declarator |
| 2521 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 2522 | /// struct-declarator: |
| 2523 | /// declarator |
| 2524 | /// [GNU] declarator attributes[opt] |
| 2525 | /// declarator[opt] ':' constant-expression |
| 2526 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 2527 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2528 | void Parser:: |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2529 | ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 2530 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2531 | if (Tok.is(tok::kw___extension__)) { |
| 2532 | // __extension__ silences extension warnings in the subexpression. |
| 2533 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2534 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2535 | return ParseStructDeclaration(DS, Fields); |
| 2536 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2537 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2538 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2539 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2540 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 2541 | // If there are no declarators, this is a free-standing declaration |
| 2542 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2543 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2544 | Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2545 | return; |
| 2546 | } |
| 2547 | |
| 2548 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2549 | bool FirstDeclarator = true; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2550 | SourceLocation CommaLoc; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2551 | while (1) { |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2552 | ParsingDeclRAIIObject PD(*this); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2553 | FieldDeclarator DeclaratorInfo(DS); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2554 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2555 | |
| 2556 | // Attributes are only allowed here on successive declarators. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2557 | if (!FirstDeclarator) |
| 2558 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2559 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2560 | /// struct-declarator: declarator |
| 2561 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2562 | if (Tok.isNot(tok::colon)) { |
| 2563 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 2564 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2565 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2566 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2567 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2568 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2569 | ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2570 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2571 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2572 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 2573 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2574 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2575 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2576 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2577 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2578 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2579 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2580 | // We're done with this declarator; invoke the callback. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2581 | Decl *D = Fields.invoke(DeclaratorInfo); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2582 | PD.complete(D); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2583 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2584 | // If we don't have a comma, it is either the end of the list (a ';') |
| 2585 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2586 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2587 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2588 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2589 | // Consume the comma. |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2590 | CommaLoc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2591 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2592 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2593 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
| 2596 | /// ParseStructUnionBody |
| 2597 | /// struct-contents: |
| 2598 | /// struct-declaration-list |
| 2599 | /// [EXT] empty |
| 2600 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 2601 | /// struct-declaration-list: |
| 2602 | /// struct-declaration |
| 2603 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2604 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2605 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2606 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2607 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2608 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 2609 | "parsing struct/union body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2610 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2611 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 2612 | if (T.consumeOpen()) |
| 2613 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2614 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 2615 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2616 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2617 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2618 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 2619 | // C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2620 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) { |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 2621 | Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union); |
| 2622 | Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union); |
| 2623 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2624 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2625 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2626 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2627 | // 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] | 2628 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2629 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2630 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2631 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2632 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2633 | Diag(Tok, diag::ext_extra_struct_semi) |
Douglas Gregor | f13ca06 | 2010-06-16 23:08:59 +0000 | [diff] [blame] | 2634 | << DeclSpec::getSpecifierName((DeclSpec::TST)TagType) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2635 | << FixItHint::CreateRemoval(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2636 | ConsumeToken(); |
| 2637 | continue; |
| 2638 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2639 | |
| 2640 | // Parse all the comma separated declarators. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2641 | DeclSpec DS(AttrFactory); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2642 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2643 | if (!Tok.is(tok::at)) { |
| 2644 | struct CFieldCallback : FieldCallback { |
| 2645 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2646 | Decl *TagDecl; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2647 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2648 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2649 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2650 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2651 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 2652 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2653 | virtual Decl *invoke(FieldDeclarator &FD) { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2654 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2655 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 2656 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 2657 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2658 | FieldDecls.push_back(Field); |
| 2659 | return Field; |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 2660 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2661 | } Callback(*this, TagDecl, FieldDecls); |
| 2662 | |
| 2663 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2664 | } else { // Handle @defs |
| 2665 | ConsumeToken(); |
| 2666 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 2667 | Diag(Tok, diag::err_unexpected_at); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2668 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2669 | continue; |
| 2670 | } |
| 2671 | ConsumeToken(); |
| 2672 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 2673 | if (!Tok.is(tok::identifier)) { |
| 2674 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2675 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2676 | continue; |
| 2677 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2678 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2679 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2680 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2681 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 2682 | ConsumeToken(); |
| 2683 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2684 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2685 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2686 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2687 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2688 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2689 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2690 | break; |
| 2691 | } else { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2692 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 2693 | // 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] | 2694 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2695 | // If we stopped at a ';', eat it. |
| 2696 | if (Tok.is(tok::semi)) ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2697 | } |
| 2698 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2699 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2700 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2701 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2702 | ParsedAttributes attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2703 | // If attributes exist after struct contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2704 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 2705 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2706 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 2707 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2708 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2709 | attrs.getList()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2710 | StructScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2711 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 2712 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2713 | } |
| 2714 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2715 | /// ParseEnumSpecifier |
| 2716 | /// enum-specifier: [C99 6.7.2.2] |
| 2717 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2718 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2719 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 2720 | /// '}' attributes[opt] |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 2721 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 2722 | /// '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2723 | /// 'enum' identifier |
| 2724 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2725 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2726 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 2727 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2728 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2729 | /// enum-head: [C++11] |
| 2730 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 2731 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 2732 | /// identifier enum-base[opt] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2733 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2734 | /// enum-key: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2735 | /// 'enum' |
| 2736 | /// 'enum' 'class' |
| 2737 | /// 'enum' 'struct' |
| 2738 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2739 | /// enum-base: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2740 | /// ':' type-specifier-seq |
| 2741 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2742 | /// [C++] elaborated-type-specifier: |
| 2743 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 2744 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2745 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 2746 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2747 | AccessSpecifier AS, DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2748 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2749 | if (Tok.is(tok::code_completion)) { |
| 2750 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2751 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2752 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2753 | } |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2754 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2755 | SourceLocation ScopedEnumKWLoc; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2756 | bool IsScopedUsingClassTag = false; |
| 2757 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2758 | if (getLangOpts().CPlusPlus0x && |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2759 | (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2760 | Diag(Tok, diag::warn_cxx98_compat_scoped_enum); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2761 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2762 | ScopedEnumKWLoc = ConsumeToken(); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2763 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2764 | |
| 2765 | // C++11 [temp.explicit]p12: The usual access controls do not apply to names |
| 2766 | // used to specify explicit instantiations. We extend this to also cover |
| 2767 | // explicit specializations. |
| 2768 | Sema::SuppressAccessChecksRAII SuppressAccess(Actions, |
| 2769 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 2770 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 2771 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2772 | // If attributes exist after tag, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2773 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2774 | MaybeParseGNUAttributes(attrs); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2775 | |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 2776 | // If declspecs exist after tag, parse them. |
| 2777 | while (Tok.is(tok::kw___declspec)) |
| 2778 | ParseMicrosoftDeclSpec(attrs); |
| 2779 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2780 | // Enum definitions should not be parsed in a trailing-return-type. |
| 2781 | bool AllowDeclaration = DSC != DSC_trailing; |
| 2782 | |
| 2783 | bool AllowFixedUnderlyingType = AllowDeclaration && |
| 2784 | (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt || |
| 2785 | getLangOpts().ObjC2); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2786 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2787 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2788 | if (getLangOpts().CPlusPlus) { |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2789 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 2790 | // if a fixed underlying type is allowed. |
| 2791 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
| 2792 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2793 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
| 2794 | /*EnteringContext=*/false)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2795 | return; |
| 2796 | |
| 2797 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2798 | Diag(Tok, diag::err_expected_ident); |
| 2799 | if (Tok.isNot(tok::l_brace)) { |
| 2800 | // Has no name and is not a definition. |
| 2801 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2802 | SkipUntil(tok::comma, true); |
| 2803 | return; |
| 2804 | } |
| 2805 | } |
| 2806 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2807 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2808 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2809 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2810 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2811 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2812 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2813 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2814 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2815 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2816 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2817 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2818 | // If an identifier is present, consume and remember it. |
| 2819 | IdentifierInfo *Name = 0; |
| 2820 | SourceLocation NameLoc; |
| 2821 | if (Tok.is(tok::identifier)) { |
| 2822 | Name = Tok.getIdentifierInfo(); |
| 2823 | NameLoc = ConsumeToken(); |
| 2824 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2825 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2826 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2827 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 2828 | // declaration of a scoped enumeration. |
| 2829 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2830 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2831 | IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2832 | } |
| 2833 | |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2834 | // Stop suppressing access control now we've parsed the enum name. |
| 2835 | SuppressAccess.done(); |
| 2836 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2837 | TypeResult BaseType; |
| 2838 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2839 | // Parse the fixed underlying type. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2840 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2841 | bool PossibleBitfield = false; |
| 2842 | if (getCurScope()->getFlags() & Scope::ClassScope) { |
| 2843 | // If we're in class scope, this can either be an enum declaration with |
| 2844 | // an underlying type, or a declaration of a bitfield member. We try to |
| 2845 | // use a simple disambiguation scheme first to catch the common cases |
| 2846 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 2847 | // anything that's a simple-type-specifier followed by '(' as an |
| 2848 | // expression. This suffices because function types are not valid |
| 2849 | // underlying types anyway. |
| 2850 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
| 2851 | // If the next token starts an expression, we know we're parsing a |
| 2852 | // bit-field. This is the common case. |
| 2853 | if (TPR == TPResult::True()) |
| 2854 | PossibleBitfield = true; |
| 2855 | // If the next token starts a type-specifier-seq, it may be either a |
| 2856 | // a fixed underlying type or the start of a function-style cast in C++; |
| 2857 | // lookahead one more token to see if it's obvious that we have a |
| 2858 | // fixed underlying type. |
| 2859 | else if (TPR == TPResult::False() && |
| 2860 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 2861 | // Consume the ':'. |
| 2862 | ConsumeToken(); |
| 2863 | } else { |
| 2864 | // We have the start of a type-specifier-seq, so we have to perform |
| 2865 | // tentative parsing to determine whether we have an expression or a |
| 2866 | // type. |
| 2867 | TentativeParsingAction TPA(*this); |
| 2868 | |
| 2869 | // Consume the ':'. |
| 2870 | ConsumeToken(); |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 2871 | |
| 2872 | // If we see a type specifier followed by an open-brace, we have an |
| 2873 | // ambiguity between an underlying type and a C++11 braced |
| 2874 | // function-style cast. Resolve this by always treating it as an |
| 2875 | // underlying type. |
| 2876 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 2877 | // this case. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2878 | if ((getLangOpts().CPlusPlus && |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 2879 | isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2880 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2881 | // We'll parse this as a bitfield later. |
| 2882 | PossibleBitfield = true; |
| 2883 | TPA.Revert(); |
| 2884 | } else { |
| 2885 | // We have a type-specifier-seq. |
| 2886 | TPA.Commit(); |
| 2887 | } |
| 2888 | } |
| 2889 | } else { |
| 2890 | // Consume the ':'. |
| 2891 | ConsumeToken(); |
| 2892 | } |
| 2893 | |
| 2894 | if (!PossibleBitfield) { |
| 2895 | SourceRange Range; |
| 2896 | BaseType = ParseTypeName(&Range); |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 2897 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2898 | if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2) |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 2899 | Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type) |
| 2900 | << Range; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2901 | if (getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2902 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2903 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2904 | } |
| 2905 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2906 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 2907 | // friend declaration, and cannot have an accompanying definition. If we have |
| 2908 | // 'enum foo;', then this is a forward declaration. If we have |
| 2909 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 2910 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2911 | // |
| 2912 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 2913 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 2914 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 2915 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2916 | Sema::TagUseKind TUK; |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2917 | if (DS.isFriendSpecified()) |
| 2918 | TUK = Sema::TUK_Friend; |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2919 | else if (!AllowDeclaration) |
| 2920 | TUK = Sema::TUK_Reference; |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2921 | else if (Tok.is(tok::l_brace)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2922 | TUK = Sema::TUK_Definition; |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2923 | else if (Tok.is(tok::semi) && DSC != DSC_type_specifier) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2924 | TUK = Sema::TUK_Declaration; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2925 | else |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2926 | TUK = Sema::TUK_Reference; |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2927 | |
| 2928 | MultiTemplateParamsArg TParams; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 2929 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2930 | TUK != Sema::TUK_Reference) { |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2931 | if (!getLangOpts().CPlusPlus0x || !SS.isSet()) { |
| 2932 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2933 | Diag(Tok, diag::err_enum_template); |
| 2934 | SkipUntil(tok::comma, true); |
| 2935 | return; |
| 2936 | } |
| 2937 | |
| 2938 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 2939 | // Enumerations can't be explicitly instantiated. |
| 2940 | DS.SetTypeSpecError(); |
| 2941 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 2942 | return; |
| 2943 | } |
| 2944 | |
| 2945 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 2946 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 2947 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 2948 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2949 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2950 | if (!Name && TUK != Sema::TUK_Definition) { |
| 2951 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2952 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2953 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2954 | SkipUntil(tok::comma, true); |
| 2955 | return; |
| 2956 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2957 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 2958 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2959 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2960 | const char *PrevSpec = 0; |
| 2961 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2962 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2963 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2964 | AS, DS.getModulePrivateSpecLoc(), TParams, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2965 | Owned, IsDependent, ScopedEnumKWLoc, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2966 | IsScopedUsingClassTag, BaseType); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2967 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2968 | if (IsDependent) { |
| 2969 | // This enum has a dependent nested-name-specifier. Handle it as a |
| 2970 | // dependent tag. |
| 2971 | if (!Name) { |
| 2972 | DS.SetTypeSpecError(); |
| 2973 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 2974 | return; |
| 2975 | } |
| 2976 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2977 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2978 | TUK, SS, Name, StartLoc, |
| 2979 | NameLoc); |
| 2980 | if (Type.isInvalid()) { |
| 2981 | DS.SetTypeSpecError(); |
| 2982 | return; |
| 2983 | } |
| 2984 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 2985 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 2986 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 2987 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2988 | Diag(StartLoc, DiagID) << PrevSpec; |
| 2989 | |
| 2990 | return; |
| 2991 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2992 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2993 | if (!TagDecl) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2994 | // The action failed to produce an enumeration tag. If this is a |
| 2995 | // definition, consume the entire definition. |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2996 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2997 | ConsumeBrace(); |
| 2998 | SkipUntil(tok::r_brace); |
| 2999 | } |
| 3000 | |
| 3001 | DS.SetTypeSpecError(); |
| 3002 | return; |
| 3003 | } |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3004 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3005 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3006 | if (TUK == Sema::TUK_Friend) { |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3007 | Diag(Tok, diag::err_friend_decl_defines_type) |
| 3008 | << SourceRange(DS.getFriendSpecLoc()); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3009 | ConsumeBrace(); |
| 3010 | SkipUntil(tok::r_brace); |
| 3011 | } else { |
| 3012 | ParseEnumBody(StartLoc, TagDecl); |
| 3013 | } |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3014 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3015 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3016 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3017 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3018 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3019 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3020 | } |
| 3021 | |
| 3022 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3023 | /// enumerator-list: |
| 3024 | /// enumerator |
| 3025 | /// enumerator-list ',' enumerator |
| 3026 | /// enumerator: |
| 3027 | /// enumeration-constant |
| 3028 | /// enumeration-constant '=' constant-expression |
| 3029 | /// enumeration-constant: |
| 3030 | /// identifier |
| 3031 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3032 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3033 | // Enter the scope of the enum body and start the definition. |
| 3034 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3035 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3036 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3037 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3038 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3039 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3040 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3041 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3042 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3043 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3044 | SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3045 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3046 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3047 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3048 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3049 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3050 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3051 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3052 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3053 | // If attributes exist after the enumerator, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3054 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3055 | MaybeParseGNUAttributes(attrs); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3056 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3057 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3058 | ExprResult AssignedVal; |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3059 | ParsingDeclRAIIObject PD(*this); |
| 3060 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3061 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3062 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3063 | AssignedVal = ParseConstantExpression(); |
| 3064 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3065 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3066 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3067 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3068 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3069 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3070 | LastEnumConstDecl, |
| 3071 | IdentLoc, Ident, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3072 | attrs.getList(), EqualLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3073 | AssignedVal.release()); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3074 | PD.complete(EnumConstDecl); |
| 3075 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3076 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3077 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3078 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3079 | if (Tok.is(tok::identifier)) { |
| 3080 | // We're missing a comma between enumerators. |
| 3081 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
| 3082 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
| 3083 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3084 | continue; |
| 3085 | } |
| 3086 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3087 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3088 | break; |
| 3089 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3090 | |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3091 | if (Tok.isNot(tok::identifier)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3092 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3093 | Diag(CommaLoc, diag::ext_enumerator_list_comma) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3094 | << getLangOpts().CPlusPlus |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3095 | << FixItHint::CreateRemoval(CommaLoc); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3096 | else if (getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3097 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3098 | << FixItHint::CreateRemoval(CommaLoc); |
| 3099 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3100 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3101 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3102 | // Eat the }. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3103 | T.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3104 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3105 | // If attributes exist after the identifier list, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3106 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3107 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3108 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3109 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
| 3110 | EnumDecl, EnumConstantDecls.data(), |
| 3111 | EnumConstantDecls.size(), getCurScope(), |
| 3112 | attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3113 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3114 | EnumScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3115 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3116 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3117 | } |
| 3118 | |
| 3119 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3120 | /// start of a type-qualifier-list. |
| 3121 | bool Parser::isTypeQualifier() const { |
| 3122 | switch (Tok.getKind()) { |
| 3123 | default: return false; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3124 | |
| 3125 | // type-qualifier only in OpenCL |
| 3126 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3127 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3128 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3129 | // type-qualifier |
| 3130 | case tok::kw_const: |
| 3131 | case tok::kw_volatile: |
| 3132 | case tok::kw_restrict: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3133 | case tok::kw___private: |
| 3134 | case tok::kw___local: |
| 3135 | case tok::kw___global: |
| 3136 | case tok::kw___constant: |
| 3137 | case tok::kw___read_only: |
| 3138 | case tok::kw___read_write: |
| 3139 | case tok::kw___write_only: |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3140 | return true; |
| 3141 | } |
| 3142 | } |
| 3143 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3144 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3145 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3146 | /// specifier or if we're not sure. |
| 3147 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3148 | switch (Tok.getKind()) { |
| 3149 | default: return false; |
| 3150 | // type-specifiers |
| 3151 | case tok::kw_short: |
| 3152 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3153 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3154 | case tok::kw___int128: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3155 | case tok::kw_signed: |
| 3156 | case tok::kw_unsigned: |
| 3157 | case tok::kw__Complex: |
| 3158 | case tok::kw__Imaginary: |
| 3159 | case tok::kw_void: |
| 3160 | case tok::kw_char: |
| 3161 | case tok::kw_wchar_t: |
| 3162 | case tok::kw_char16_t: |
| 3163 | case tok::kw_char32_t: |
| 3164 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3165 | case tok::kw_half: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3166 | case tok::kw_float: |
| 3167 | case tok::kw_double: |
| 3168 | case tok::kw_bool: |
| 3169 | case tok::kw__Bool: |
| 3170 | case tok::kw__Decimal32: |
| 3171 | case tok::kw__Decimal64: |
| 3172 | case tok::kw__Decimal128: |
| 3173 | case tok::kw___vector: |
| 3174 | |
| 3175 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3176 | case tok::kw_class: |
| 3177 | case tok::kw_struct: |
| 3178 | case tok::kw_union: |
| 3179 | // enum-specifier |
| 3180 | case tok::kw_enum: |
| 3181 | |
| 3182 | // typedef-name |
| 3183 | case tok::annot_typename: |
| 3184 | return true; |
| 3185 | } |
| 3186 | } |
| 3187 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3188 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3189 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3190 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3191 | switch (Tok.getKind()) { |
| 3192 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3193 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3194 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3195 | if (TryAltiVecVectorToken()) |
| 3196 | return true; |
| 3197 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3198 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3199 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3200 | // recurse to handle whatever we get. |
| 3201 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3202 | return true; |
| 3203 | if (Tok.is(tok::identifier)) |
| 3204 | return false; |
| 3205 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3206 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3207 | case tok::coloncolon: // ::foo::bar |
| 3208 | if (NextToken().is(tok::kw_new) || // ::new |
| 3209 | NextToken().is(tok::kw_delete)) // ::delete |
| 3210 | return false; |
| 3211 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3212 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3213 | return true; |
| 3214 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3215 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3216 | // GNU attributes support. |
| 3217 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3218 | // GNU typeof support. |
| 3219 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3220 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3221 | // type-specifiers |
| 3222 | case tok::kw_short: |
| 3223 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3224 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3225 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3226 | case tok::kw_signed: |
| 3227 | case tok::kw_unsigned: |
| 3228 | case tok::kw__Complex: |
| 3229 | case tok::kw__Imaginary: |
| 3230 | case tok::kw_void: |
| 3231 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3232 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3233 | case tok::kw_char16_t: |
| 3234 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3235 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3236 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3237 | case tok::kw_float: |
| 3238 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3239 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3240 | case tok::kw__Bool: |
| 3241 | case tok::kw__Decimal32: |
| 3242 | case tok::kw__Decimal64: |
| 3243 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3244 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3245 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3246 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3247 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3248 | case tok::kw_struct: |
| 3249 | case tok::kw_union: |
| 3250 | // enum-specifier |
| 3251 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3252 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3253 | // type-qualifier |
| 3254 | case tok::kw_const: |
| 3255 | case tok::kw_volatile: |
| 3256 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3257 | |
| 3258 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 3259 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3260 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3261 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 3262 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3263 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3264 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3265 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3266 | case tok::kw___cdecl: |
| 3267 | case tok::kw___stdcall: |
| 3268 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3269 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3270 | case tok::kw___w64: |
| 3271 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3272 | case tok::kw___ptr32: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3273 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3274 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3275 | |
| 3276 | case tok::kw___private: |
| 3277 | case tok::kw___local: |
| 3278 | case tok::kw___global: |
| 3279 | case tok::kw___constant: |
| 3280 | case tok::kw___read_only: |
| 3281 | case tok::kw___read_write: |
| 3282 | case tok::kw___write_only: |
| 3283 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3284 | return true; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3285 | |
| 3286 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3287 | return getLangOpts().OpenCL; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3288 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3289 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3290 | case tok::kw__Atomic: |
| 3291 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3292 | } |
| 3293 | } |
| 3294 | |
| 3295 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 3296 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3297 | /// |
| 3298 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 3299 | /// this check is to disambiguate between an expression and a declaration. |
| 3300 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3301 | switch (Tok.getKind()) { |
| 3302 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3303 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3304 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3305 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3306 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3307 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3308 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3309 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3310 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3311 | if (TryAltiVecVectorToken()) |
| 3312 | return true; |
| 3313 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3314 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3315 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3316 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3317 | // recurse to handle whatever we get. |
| 3318 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3319 | return true; |
| 3320 | if (Tok.is(tok::identifier)) |
| 3321 | return false; |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3322 | |
| 3323 | // If we're in Objective-C and we have an Objective-C class type followed |
| 3324 | // by an identifier and then either ':' or ']', in a place where an |
| 3325 | // expression is permitted, then this is probably a class message send |
| 3326 | // missing the initial '['. In this case, we won't consider this to be |
| 3327 | // the start of a declaration. |
| 3328 | if (DisambiguatingWithExpression && |
| 3329 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 3330 | return false; |
| 3331 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3332 | return isDeclarationSpecifier(); |
| 3333 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3334 | case tok::coloncolon: // ::foo::bar |
| 3335 | if (NextToken().is(tok::kw_new) || // ::new |
| 3336 | NextToken().is(tok::kw_delete)) // ::delete |
| 3337 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3338 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3339 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3340 | // recurse to handle whatever we get. |
| 3341 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3342 | return true; |
| 3343 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3344 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3345 | // storage-class-specifier |
| 3346 | case tok::kw_typedef: |
| 3347 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 3348 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3349 | case tok::kw_static: |
| 3350 | case tok::kw_auto: |
| 3351 | case tok::kw_register: |
| 3352 | case tok::kw___thread: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3353 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 3354 | // Modules |
| 3355 | case tok::kw___module_private__: |
| 3356 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3357 | // type-specifiers |
| 3358 | case tok::kw_short: |
| 3359 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3360 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3361 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3362 | case tok::kw_signed: |
| 3363 | case tok::kw_unsigned: |
| 3364 | case tok::kw__Complex: |
| 3365 | case tok::kw__Imaginary: |
| 3366 | case tok::kw_void: |
| 3367 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3368 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3369 | case tok::kw_char16_t: |
| 3370 | case tok::kw_char32_t: |
| 3371 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3372 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3373 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3374 | case tok::kw_float: |
| 3375 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3376 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3377 | case tok::kw__Bool: |
| 3378 | case tok::kw__Decimal32: |
| 3379 | case tok::kw__Decimal64: |
| 3380 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3381 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3382 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3383 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3384 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3385 | case tok::kw_struct: |
| 3386 | case tok::kw_union: |
| 3387 | // enum-specifier |
| 3388 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3389 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3390 | // type-qualifier |
| 3391 | case tok::kw_const: |
| 3392 | case tok::kw_volatile: |
| 3393 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3394 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3395 | // function-specifier |
| 3396 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3397 | case tok::kw_virtual: |
| 3398 | case tok::kw_explicit: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3399 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 3400 | // static_assert-declaration |
| 3401 | case tok::kw__Static_assert: |
| 3402 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3403 | // GNU typeof support. |
| 3404 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3405 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3406 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3407 | case tok::kw___attribute: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3408 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3409 | |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3410 | // C++0x decltype. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3411 | case tok::annot_decltype: |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3412 | return true; |
| 3413 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3414 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3415 | case tok::kw__Atomic: |
| 3416 | return true; |
| 3417 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 3418 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3419 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3420 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3421 | |
Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 3422 | // typedef-name |
| 3423 | case tok::annot_typename: |
| 3424 | return !DisambiguatingWithExpression || |
| 3425 | !isStartOfObjCClassMessageMissingOpenBracket(); |
| 3426 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 3427 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3428 | case tok::kw___cdecl: |
| 3429 | case tok::kw___stdcall: |
| 3430 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3431 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3432 | case tok::kw___w64: |
| 3433 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3434 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3435 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3436 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3437 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3438 | |
| 3439 | case tok::kw___private: |
| 3440 | case tok::kw___local: |
| 3441 | case tok::kw___global: |
| 3442 | case tok::kw___constant: |
| 3443 | case tok::kw___read_only: |
| 3444 | case tok::kw___read_write: |
| 3445 | case tok::kw___write_only: |
| 3446 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3447 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3448 | } |
| 3449 | } |
| 3450 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3451 | bool Parser::isConstructorDeclarator() { |
| 3452 | TentativeParsingAction TPA(*this); |
| 3453 | |
| 3454 | // Parse the C++ scope specifier. |
| 3455 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3456 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
| 3457 | /*EnteringContext=*/true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3458 | TPA.Revert(); |
| 3459 | return false; |
| 3460 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3461 | |
| 3462 | // Parse the constructor name. |
| 3463 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 3464 | // We already know that we have a constructor name; just consume |
| 3465 | // the token. |
| 3466 | ConsumeToken(); |
| 3467 | } else { |
| 3468 | TPA.Revert(); |
| 3469 | return false; |
| 3470 | } |
| 3471 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 3472 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3473 | if (Tok.isNot(tok::l_paren)) { |
| 3474 | TPA.Revert(); |
| 3475 | return false; |
| 3476 | } |
| 3477 | ConsumeParen(); |
| 3478 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 3479 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 3480 | // that we have a constructor. |
| 3481 | if (Tok.is(tok::r_paren) || |
| 3482 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3483 | TPA.Revert(); |
| 3484 | return true; |
| 3485 | } |
| 3486 | |
| 3487 | // If we need to, enter the specified scope. |
| 3488 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3489 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3490 | DeclScopeObj.EnterDeclaratorScope(); |
| 3491 | |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3492 | // Optionally skip Microsoft attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3493 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3494 | MaybeParseMicrosoftAttributes(Attrs); |
| 3495 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3496 | // Check whether the next token(s) are part of a declaration |
| 3497 | // specifier, in which case we have the start of a parameter and, |
| 3498 | // therefore, we know that this is a constructor. |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 3499 | bool IsConstructor = false; |
| 3500 | if (isDeclarationSpecifier()) |
| 3501 | IsConstructor = true; |
| 3502 | else if (Tok.is(tok::identifier) || |
| 3503 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 3504 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 3505 | // This might be a parenthesized member name, but is more likely to |
| 3506 | // be a constructor declaration with an invalid argument type. Keep |
| 3507 | // looking. |
| 3508 | if (Tok.is(tok::annot_cxxscope)) |
| 3509 | ConsumeToken(); |
| 3510 | ConsumeToken(); |
| 3511 | |
| 3512 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 3513 | // which must have one of the following syntactic forms (see the |
| 3514 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 3515 | switch (Tok.getKind()) { |
| 3516 | case tok::l_paren: |
| 3517 | // C(X ( int)); |
| 3518 | case tok::l_square: |
| 3519 | // C(X [ 5]); |
| 3520 | // C(X [ [attribute]]); |
| 3521 | case tok::coloncolon: |
| 3522 | // C(X :: Y); |
| 3523 | // C(X :: *p); |
| 3524 | case tok::r_paren: |
| 3525 | // C(X ) |
| 3526 | // Assume this isn't a constructor, rather than assuming it's a |
| 3527 | // constructor with an unnamed parameter of an ill-formed type. |
| 3528 | break; |
| 3529 | |
| 3530 | default: |
| 3531 | IsConstructor = true; |
| 3532 | break; |
| 3533 | } |
| 3534 | } |
| 3535 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3536 | TPA.Revert(); |
| 3537 | return IsConstructor; |
| 3538 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3539 | |
| 3540 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3541 | /// type-qualifier-list: [C99 6.7.5] |
| 3542 | /// type-qualifier |
| 3543 | /// [vendor] attributes |
| 3544 | /// [ only if VendorAttributesAllowed=true ] |
| 3545 | /// type-qualifier-list type-qualifier |
| 3546 | /// [vendor] type-qualifier-list attributes |
| 3547 | /// [ only if VendorAttributesAllowed=true ] |
| 3548 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
| 3549 | /// [ only if CXX0XAttributesAllowed=true ] |
| 3550 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3551 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3552 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 3553 | bool VendorAttributesAllowed, |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3554 | bool CXX11AttributesAllowed) { |
| 3555 | if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed && |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3556 | isCXX11AttributeSpecifier()) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3557 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3558 | ParseCXX11Attributes(attrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3559 | DS.takeAttributesFrom(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3560 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3561 | |
| 3562 | SourceLocation EndLoc; |
| 3563 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3564 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3565 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3566 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3567 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3568 | SourceLocation Loc = Tok.getLocation(); |
| 3569 | |
| 3570 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3571 | case tok::code_completion: |
| 3572 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3573 | return cutOffParsing(); |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3574 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3575 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3576 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3577 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3578 | break; |
| 3579 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3580 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3581 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3582 | break; |
| 3583 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3584 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3585 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3586 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3587 | |
| 3588 | // OpenCL qualifiers: |
| 3589 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3590 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3591 | goto DoneWithTypeQuals; |
| 3592 | case tok::kw___private: |
| 3593 | case tok::kw___global: |
| 3594 | case tok::kw___local: |
| 3595 | case tok::kw___constant: |
| 3596 | case tok::kw___read_only: |
| 3597 | case tok::kw___write_only: |
| 3598 | case tok::kw___read_write: |
| 3599 | ParseOpenCLQualifiers(DS); |
| 3600 | break; |
| 3601 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3602 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 3603 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3604 | case tok::kw___ptr32: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3605 | case tok::kw___cdecl: |
| 3606 | case tok::kw___stdcall: |
| 3607 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3608 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3609 | case tok::kw___unaligned: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3610 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3611 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3612 | continue; |
| 3613 | } |
| 3614 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3615 | case tok::kw___pascal: |
| 3616 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3617 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3618 | continue; |
| 3619 | } |
| 3620 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3621 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3622 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3623 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3624 | continue; // do *not* consume the next token! |
| 3625 | } |
| 3626 | // otherwise, FALL THROUGH! |
| 3627 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3628 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3629 | // If this is not a type-qualifier token, we're done reading type |
| 3630 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 3631 | DS.Finish(Diags, PP); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3632 | if (EndLoc.isValid()) |
| 3633 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3634 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3635 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 3636 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3637 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 3638 | if (isInvalid) { |
| 3639 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3640 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3641 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3642 | EndLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3643 | } |
| 3644 | } |
| 3645 | |
| 3646 | |
| 3647 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 3648 | /// |
| 3649 | void Parser::ParseDeclarator(Declarator &D) { |
| 3650 | /// This implements the 'declarator' production in the C grammar, then checks |
| 3651 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3652 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3653 | } |
| 3654 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3655 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) { |
| 3656 | if (Kind == tok::star || Kind == tok::caret) |
| 3657 | return true; |
| 3658 | |
| 3659 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 3660 | if (!Lang.CPlusPlus) |
| 3661 | return false; |
| 3662 | |
| 3663 | return Kind == tok::amp || Kind == tok::ampamp; |
| 3664 | } |
| 3665 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3666 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 3667 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 3668 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3669 | /// ptr-operator production. |
| 3670 | /// |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 3671 | /// 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] | 3672 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 3673 | /// isConstructorDeclarator. |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 3674 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3675 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 3676 | /// [C] pointer[opt] direct-declarator |
| 3677 | /// [C++] direct-declarator |
| 3678 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3679 | /// |
| 3680 | /// pointer: [C99 6.7.5] |
| 3681 | /// '*' type-qualifier-list[opt] |
| 3682 | /// '*' type-qualifier-list[opt] pointer |
| 3683 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3684 | /// ptr-operator: |
| 3685 | /// '*' cv-qualifier-seq[opt] |
| 3686 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3687 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3688 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3689 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3690 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3691 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 3692 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3693 | if (Diags.hasAllExtensionsSilenced()) |
| 3694 | D.setExtension(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3695 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3696 | // C++ member pointers start with a '::' or a nested-name. |
| 3697 | // Member pointers get special handling, since there's no place for the |
| 3698 | // scope spec in the generic path below. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3699 | if (getLangOpts().CPlusPlus && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 3700 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 3701 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3702 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 3703 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3704 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3705 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3706 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 3707 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3708 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3709 | // The scope spec really belongs to the direct-declarator. |
| 3710 | D.getCXXScopeSpec() = SS; |
| 3711 | if (DirectDeclParser) |
| 3712 | (this->*DirectDeclParser)(D); |
| 3713 | return; |
| 3714 | } |
| 3715 | |
| 3716 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3717 | D.SetRangeEnd(Loc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3718 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3719 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3720 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3721 | |
| 3722 | // Recurse to parse whatever is left. |
| 3723 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 3724 | |
| 3725 | // Sema will have to catch (syntactically invalid) pointers into global |
| 3726 | // scope. It has to catch pointers into namespace scope anyway. |
| 3727 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3728 | Loc), |
| 3729 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3730 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3731 | return; |
| 3732 | } |
| 3733 | } |
| 3734 | |
| 3735 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3736 | // Not a pointer, C++ reference, or block. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3737 | if (!isPtrOperatorToken(Kind, getLangOpts())) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3738 | if (DirectDeclParser) |
| 3739 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3740 | return; |
| 3741 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3742 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3743 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 3744 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3745 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3746 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3747 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 3748 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 3749 | // Is a pointer. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3750 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3751 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3752 | // FIXME: GNU attributes are not allowed here in a new-type-id. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3753 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3754 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3755 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3756 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3757 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3758 | if (Kind == tok::star) |
| 3759 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 3760 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 3761 | DS.getConstSpecLoc(), |
| 3762 | DS.getVolatileSpecLoc(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3763 | DS.getRestrictSpecLoc()), |
| 3764 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3765 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3766 | else |
| 3767 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3768 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3769 | Loc), |
| 3770 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3771 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3772 | } else { |
| 3773 | // Is a reference |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3774 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3775 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3776 | // Complain about rvalue references in C++03, but then go on and build |
| 3777 | // the declarator. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3778 | if (Kind == tok::ampamp) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3779 | Diag(Loc, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3780 | diag::warn_cxx98_compat_rvalue_reference : |
| 3781 | diag::ext_rvalue_reference); |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3782 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3783 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 3784 | ParseTypeQualifierListOpt(DS); |
| 3785 | D.ExtendWithDeclSpec(DS); |
| 3786 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3787 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 3788 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 3789 | // template type argument, in which case the cv-qualifiers are ignored. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3790 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 3791 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 3792 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3793 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3794 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 3795 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3796 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3797 | } |
| 3798 | |
| 3799 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3800 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3801 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3802 | if (D.getNumTypeObjects() > 0) { |
| 3803 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 3804 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 3805 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 3806 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 3807 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 3808 | << II; |
| 3809 | else |
| 3810 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 3811 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3812 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3813 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3814 | // can go ahead and build the (technically ill-formed) |
| 3815 | // declarator: reference collapsing will take care of it. |
| 3816 | } |
| 3817 | } |
| 3818 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3819 | // 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] | 3820 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3821 | Kind == tok::amp), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3822 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3823 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3824 | } |
| 3825 | } |
| 3826 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3827 | static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D, |
| 3828 | SourceLocation EllipsisLoc) { |
| 3829 | if (EllipsisLoc.isValid()) { |
| 3830 | FixItHint Insertion; |
| 3831 | if (!D.getEllipsisLoc().isValid()) { |
| 3832 | Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "..."); |
| 3833 | D.setEllipsisLoc(EllipsisLoc); |
| 3834 | } |
| 3835 | P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) |
| 3836 | << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName(); |
| 3837 | } |
| 3838 | } |
| 3839 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3840 | /// ParseDirectDeclarator |
| 3841 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3842 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3843 | /// '(' declarator ')' |
| 3844 | /// [GNU] '(' attributes declarator ')' |
| 3845 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 3846 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 3847 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 3848 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 3849 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3850 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 3851 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3852 | /// direct-declarator '(' parameter-type-list ')' |
| 3853 | /// direct-declarator '(' identifier-list[opt] ')' |
| 3854 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 3855 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3856 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 3857 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3858 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 3859 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 3860 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3861 | /// [C++] declarator-id |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3862 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3863 | /// |
| 3864 | /// declarator-id: [C++ 8] |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3865 | /// '...'[opt] id-expression |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3866 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 3867 | /// |
| 3868 | /// id-expression: [C++ 5.1] |
| 3869 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3870 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3871 | /// |
| 3872 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3873 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3874 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3875 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3876 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3877 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 3878 | /// |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 3879 | /// Note, any additional constructs added here may need corresponding changes |
| 3880 | /// in isConstructorDeclarator. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3881 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3882 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3883 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3884 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3885 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3886 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3887 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 3888 | D.getContext() == Declarator::MemberContext; |
| 3889 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
| 3890 | EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3891 | } |
| 3892 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3893 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3894 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 3895 | // Change the declaration context for name lookup, until this function |
| 3896 | // is exited (and the declarator has been parsed). |
| 3897 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3898 | } |
| 3899 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3900 | // C++0x [dcl.fct]p14: |
| 3901 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
| 3902 | // of a parameter-declaration-clause without a preceding comma. In |
| 3903 | // this case, the ellipsis is parsed as part of the |
| 3904 | // abstract-declarator if the type of the parameter names a template |
| 3905 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 3906 | // as part of the parameter-declaration-clause. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3907 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3908 | !((D.getContext() == Declarator::PrototypeContext || |
| 3909 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3910 | NextToken().is(tok::r_paren) && |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3911 | !Actions.containsUnexpandedParameterPacks(D))) { |
| 3912 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 3913 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) { |
| 3914 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 3915 | // the user what they should have done. |
| 3916 | ParseDeclarator(D); |
| 3917 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 3918 | return; |
| 3919 | } else |
| 3920 | D.setEllipsisLoc(EllipsisLoc); |
| 3921 | |
| 3922 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 3923 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 3924 | // the l_paren token. |
| 3925 | } |
| 3926 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3927 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 3928 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 3929 | // We found something that indicates the start of an unqualified-id. |
| 3930 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 3931 | bool AllowConstructorName; |
| 3932 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 3933 | AllowConstructorName = false; |
| 3934 | else if (D.getCXXScopeSpec().isSet()) |
| 3935 | AllowConstructorName = |
| 3936 | (D.getContext() == Declarator::FileContext || |
| 3937 | (D.getContext() == Declarator::MemberContext && |
| 3938 | D.getDeclSpec().isFriendSpecified())); |
| 3939 | else |
| 3940 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 3941 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3942 | SourceLocation TemplateKWLoc; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3943 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 3944 | /*EnteringContext=*/true, |
| 3945 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3946 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3947 | ParsedType(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3948 | TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3949 | D.getName()) || |
| 3950 | // Once we're past the identifier, if the scope was bad, mark the |
| 3951 | // whole declarator bad. |
| 3952 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3953 | D.SetIdentifier(0, Tok.getLocation()); |
| 3954 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3955 | } else { |
| 3956 | // Parsed the unqualified-id; update range information and move along. |
| 3957 | if (D.getSourceRange().getBegin().isInvalid()) |
| 3958 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 3959 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3960 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3961 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3962 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3963 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3964 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3965 | "There's a C++-specific check for tok::identifier above"); |
| 3966 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 3967 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 3968 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3969 | goto PastIdentifier; |
| 3970 | } |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3971 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3972 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3973 | // direct-declarator: '(' declarator ')' |
| 3974 | // direct-declarator: '(' attributes declarator ')' |
| 3975 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 3976 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3977 | |
| 3978 | // If the declarator was parenthesized, we entered the declarator |
| 3979 | // scope when parsing the parenthesized declarator, then exited |
| 3980 | // the scope already. Re-enter the scope, if we need to. |
| 3981 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 3982 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3983 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 3984 | if (!D.isInvalidType() && |
| 3985 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3986 | // Change the declaration context for name lookup, until this function |
| 3987 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 3988 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3989 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3990 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3991 | // This could be something simple like "int" (in which case the declarator |
| 3992 | // portion is empty), if an abstract-declarator is allowed. |
| 3993 | D.SetIdentifier(0, Tok.getLocation()); |
| 3994 | } else { |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 3995 | if (D.getContext() == Declarator::MemberContext) |
| 3996 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 3997 | << D.getDeclSpec().getSourceRange(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3998 | else if (getLangOpts().CPlusPlus) |
| 3999 | Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4000 | else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4001 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4002 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 4003 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4004 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4005 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4006 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4007 | assert(D.isPastIdentifier() && |
| 4008 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4009 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4010 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 4011 | if (D.hasName() && !D.getNumTypeObjects()) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4012 | MaybeParseCXX0XAttributes(D); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4013 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4014 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4015 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4016 | // Enter function-declaration scope, limiting any declarators to the |
| 4017 | // function prototype scope, including parameter declarators. |
| 4018 | ParseScope PrototypeScope(this, |
| 4019 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4020 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 4021 | // In such a case, check if we actually have a function declarator; if it |
| 4022 | // is not, the declarator has been fully parsed. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4023 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4024 | // When not in file scope, warn for ambiguous function declarators, just |
| 4025 | // in case the author intended it as a variable definition. |
| 4026 | bool warnIfAmbiguous = D.getContext() != Declarator::FileContext; |
| 4027 | if (!isCXXFunctionDeclarator(warnIfAmbiguous)) |
| 4028 | break; |
| 4029 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4030 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4031 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4032 | T.consumeOpen(); |
| 4033 | ParseFunctionDeclarator(D, attrs, T); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4034 | PrototypeScope.Exit(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4035 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4036 | ParseBracketDeclarator(D); |
| 4037 | } else { |
| 4038 | break; |
| 4039 | } |
| 4040 | } |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4041 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4042 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4043 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 4044 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4045 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4046 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 4047 | /// |
| 4048 | /// direct-declarator: |
| 4049 | /// '(' declarator ')' |
| 4050 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4051 | /// direct-declarator '(' parameter-type-list ')' |
| 4052 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4053 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4054 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4055 | /// |
| 4056 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4057 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4058 | T.consumeOpen(); |
| 4059 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4060 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4061 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4062 | // Eat any attributes before we look at whether this is a grouping or function |
| 4063 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 4064 | // the type being built up, for example: |
| 4065 | // int (__attribute__(()) *x)(long y) |
| 4066 | // If this ends up not being a grouping paren, the attribute applies to the |
| 4067 | // first argument, for example: |
| 4068 | // int (__attribute__(()) int x) |
| 4069 | // In either case, we need to eat any attributes to be able to determine what |
| 4070 | // sort of paren this is. |
| 4071 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4072 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4073 | bool RequiresArg = false; |
| 4074 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4075 | ParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4076 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4077 | // We require that the argument list (if this is a non-grouping paren) be |
| 4078 | // present even if the attribute list was empty. |
| 4079 | RequiresArg = true; |
| 4080 | } |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4081 | // Eat any Microsoft extensions. |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4082 | if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4083 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4084 | Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4085 | Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4086 | ParseMicrosoftTypeAttributes(attrs); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4087 | } |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4088 | // Eat any Borland extensions. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 4089 | if (Tok.is(tok::kw___pascal)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4090 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4091 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4092 | // If we haven't past the identifier yet (or where the identifier would be |
| 4093 | // stored, if this is an abstract declarator), then this is probably just |
| 4094 | // grouping parens. However, if this could be an abstract-declarator, then |
| 4095 | // this could also be the start of function arguments (consider 'void()'). |
| 4096 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4097 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4098 | if (!D.mayOmitIdentifier()) { |
| 4099 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 4100 | // paren, because we haven't seen the identifier yet. |
| 4101 | isGrouping = true; |
| 4102 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4103 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 4104 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4105 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 4106 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4107 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 4108 | // considered to be a type, not a K&R identifier-list. |
| 4109 | isGrouping = false; |
| 4110 | } else { |
| 4111 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 4112 | isGrouping = true; |
| 4113 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4114 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4115 | // If this is a grouping paren, handle: |
| 4116 | // direct-declarator: '(' declarator ')' |
| 4117 | // direct-declarator: '(' attributes declarator ')' |
| 4118 | if (isGrouping) { |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4119 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 4120 | D.setEllipsisLoc(SourceLocation()); |
| 4121 | |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4122 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4123 | D.setGroupingParens(true); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4124 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4125 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4126 | T.consumeClose(); |
| 4127 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
| 4128 | T.getCloseLocation()), |
| 4129 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4130 | |
| 4131 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4132 | |
| 4133 | // An ellipsis cannot be placed outside parentheses. |
| 4134 | if (EllipsisLoc.isValid()) |
| 4135 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4136 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4137 | return; |
| 4138 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4139 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4140 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 4141 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4142 | // identifier (and remember where it would have been), then call into |
| 4143 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4144 | D.SetIdentifier(0, Tok.getLocation()); |
| 4145 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4146 | // Enter function-declaration scope, limiting any declarators to the |
| 4147 | // function prototype scope, including parameter declarators. |
| 4148 | ParseScope PrototypeScope(this, |
| 4149 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4150 | ParseFunctionDeclarator(D, attrs, T, RequiresArg); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4151 | PrototypeScope.Exit(); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4152 | } |
| 4153 | |
| 4154 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 4155 | /// declarator D up to a paren, which indicates that we are parsing function |
| 4156 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4157 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4158 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 4159 | /// immediately after the open paren - they should be considered to be the |
| 4160 | /// first argument of a parameter. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4161 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4162 | /// If RequiresArg is true, then the first argument of the function is required |
| 4163 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4164 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4165 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 4166 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 4167 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 4168 | /// |
| 4169 | /// [C++11] exception-specification: |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4170 | /// dynamic-exception-specification |
| 4171 | /// noexcept-specification |
| 4172 | /// |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4173 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4174 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4175 | BalancedDelimiterTracker &Tracker, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4176 | bool RequiresArg) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4177 | assert(getCurScope()->isFunctionPrototypeScope() && |
| 4178 | "Should call from a Function scope"); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4179 | // lparen is already consumed! |
| 4180 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 4181 | |
| 4182 | // This should be true when the function has typed arguments. |
| 4183 | // Otherwise, it is treated as a K&R-style function. |
| 4184 | bool HasProto = false; |
| 4185 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4186 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4187 | // Remember where we see an ellipsis, if any. |
| 4188 | SourceLocation EllipsisLoc; |
| 4189 | |
| 4190 | DeclSpec DS(AttrFactory); |
| 4191 | bool RefQualifierIsLValueRef = true; |
| 4192 | SourceLocation RefQualifierLoc; |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4193 | SourceLocation ConstQualifierLoc; |
| 4194 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4195 | ExceptionSpecificationType ESpecType = EST_None; |
| 4196 | SourceRange ESpecRange; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4197 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 4198 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4199 | ExprResult NoexceptExpr; |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 4200 | CachedTokens *ExceptionSpecTokens = 0; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4201 | ParsedAttributes FnAttrs(AttrFactory); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4202 | ParsedType TrailingReturnType; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4203 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4204 | Actions.ActOnStartFunctionDeclarator(); |
| 4205 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4206 | SourceLocation EndLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4207 | if (isFunctionDeclaratorIdentifierList()) { |
| 4208 | if (RequiresArg) |
| 4209 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4210 | |
| 4211 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 4212 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4213 | Tracker.consumeClose(); |
| 4214 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4215 | } else { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4216 | if (Tok.isNot(tok::r_paren)) |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4217 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4218 | else if (RequiresArg) |
| 4219 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4220 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4221 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4222 | |
| 4223 | // If we have the closing ')', eat it. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4224 | Tracker.consumeClose(); |
| 4225 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4226 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4227 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4228 | // FIXME: Accept these components in any order, and produce fixits to |
| 4229 | // correct the order if the user gets it wrong. Ideally we should deal |
| 4230 | // with the virt-specifier-seq and pure-specifier in the same way. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4231 | |
| 4232 | // Parse cv-qualifier-seq[opt]. |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4233 | ParseTypeQualifierListOpt(DS, false /*no attributes*/, false); |
| 4234 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 4235 | EndLoc = DS.getSourceRange().getEnd(); |
| 4236 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 4237 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 4238 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4239 | |
| 4240 | // Parse ref-qualifier[opt]. |
| 4241 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4242 | Diag(Tok, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4243 | diag::warn_cxx98_compat_ref_qualifier : |
| 4244 | diag::ext_ref_qualifier); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4245 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4246 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 4247 | RefQualifierLoc = ConsumeToken(); |
| 4248 | EndLoc = RefQualifierLoc; |
| 4249 | } |
| 4250 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4251 | // C++11 [expr.prim.general]p3: |
| 4252 | // If a declaration declares a member function or member function |
| 4253 | // template of a class X, the expression this is a prvalue of type |
| 4254 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
| 4255 | // and the end of the function-definition, member-declarator, or |
| 4256 | // declarator. |
| 4257 | bool IsCXX11MemberFunction = |
| 4258 | getLangOpts().CPlusPlus0x && |
| 4259 | (D.getContext() == Declarator::MemberContext || |
| 4260 | (D.getContext() == Declarator::FileContext && |
| 4261 | D.getCXXScopeSpec().isValid() && |
| 4262 | Actions.CurContext->isRecord())); |
| 4263 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 4264 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
| 4265 | DS.getTypeQualifiers(), |
| 4266 | IsCXX11MemberFunction); |
| 4267 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4268 | // Parse exception-specification[opt]. |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 4269 | bool Delayed = (D.getContext() == Declarator::MemberContext && |
| 4270 | D.getDeclSpec().getStorageClassSpec() |
| 4271 | != DeclSpec::SCS_typedef && |
| 4272 | !D.getDeclSpec().isFriendSpecified()); |
| 4273 | ESpecType = tryParseExceptionSpecification(Delayed, |
| 4274 | ESpecRange, |
| 4275 | DynamicExceptions, |
| 4276 | DynamicExceptionRanges, |
| 4277 | NoexceptExpr, |
| 4278 | ExceptionSpecTokens); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4279 | if (ESpecType != EST_None) |
| 4280 | EndLoc = ESpecRange.getEnd(); |
| 4281 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4282 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 4283 | // after the exception-specification. |
| 4284 | MaybeParseCXX0XAttributes(FnAttrs); |
| 4285 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4286 | // Parse trailing-return-type[opt]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4287 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4288 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 4289 | SourceRange Range; |
| 4290 | TrailingReturnType = ParseTrailingReturnType(Range).get(); |
| 4291 | if (Range.getEnd().isValid()) |
| 4292 | EndLoc = Range.getEnd(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4293 | } |
| 4294 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4295 | } |
| 4296 | |
| 4297 | // Remember that we parsed a function type, and remember the attributes. |
| 4298 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
| 4299 | /*isVariadic=*/EllipsisLoc.isValid(), |
| 4300 | EllipsisLoc, |
| 4301 | ParamInfo.data(), ParamInfo.size(), |
| 4302 | DS.getTypeQualifiers(), |
| 4303 | RefQualifierIsLValueRef, |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4304 | RefQualifierLoc, ConstQualifierLoc, |
| 4305 | VolatileQualifierLoc, |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 4306 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4307 | ESpecType, ESpecRange.getBegin(), |
| 4308 | DynamicExceptions.data(), |
| 4309 | DynamicExceptionRanges.data(), |
| 4310 | DynamicExceptions.size(), |
| 4311 | NoexceptExpr.isUsable() ? |
| 4312 | NoexceptExpr.get() : 0, |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 4313 | ExceptionSpecTokens, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4314 | Tracker.getOpenLocation(), |
| 4315 | EndLoc, D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4316 | TrailingReturnType), |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4317 | FnAttrs, EndLoc); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4318 | |
| 4319 | Actions.ActOnEndFunctionDeclarator(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4320 | } |
| 4321 | |
| 4322 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 4323 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 4324 | /// |
| 4325 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 4326 | /// abstract-declarators. |
| 4327 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4328 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4329 | && Tok.is(tok::identifier) |
| 4330 | && !TryAltiVecVectorToken() |
| 4331 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 4332 | // 6.7.5.3p11. |
| 4333 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 4334 | // Identifier lists follow a really simple grammar: the identifiers can |
| 4335 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 4336 | // identifier lists are really rare in the brave new modern world, and |
| 4337 | // it is very common for someone to typo a type in a non-K&R style |
| 4338 | // list. If we are presented with something like: "void foo(intptr x, |
| 4339 | // float y)", we don't want to start parsing the function declarator as |
| 4340 | // though it is a K&R style declarator just because intptr is an |
| 4341 | // invalid type. |
| 4342 | // |
| 4343 | // To handle this, we check to see if the token after the first |
| 4344 | // identifier is a "," or ")". Only then do we parse it as an |
| 4345 | // identifier list. |
| 4346 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 4347 | } |
| 4348 | |
| 4349 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 4350 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 4351 | /// |
| 4352 | /// After returning, ParamInfo will hold the parsed parameters. |
| 4353 | /// |
| 4354 | /// identifier-list: [C99 6.7.5] |
| 4355 | /// identifier |
| 4356 | /// identifier-list ',' identifier |
| 4357 | /// |
| 4358 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 4359 | Declarator &D, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4360 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4361 | // If there was no identifier specified for the declarator, either we are in |
| 4362 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 4363 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 4364 | // diagnose this. |
| 4365 | if (!D.getIdentifier()) |
| 4366 | Diag(Tok, diag::ext_ident_list_in_param); |
| 4367 | |
| 4368 | // Maintain an efficient lookup of params we have seen so far. |
| 4369 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 4370 | |
| 4371 | while (1) { |
| 4372 | // If this isn't an identifier, report the error and skip until ')'. |
| 4373 | if (Tok.isNot(tok::identifier)) { |
| 4374 | Diag(Tok, diag::err_expected_ident); |
| 4375 | SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 4376 | // Forget we parsed anything. |
| 4377 | ParamInfo.clear(); |
| 4378 | return; |
| 4379 | } |
| 4380 | |
| 4381 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 4382 | |
| 4383 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 4384 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 4385 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 4386 | |
| 4387 | // Verify that the argument identifier has not already been mentioned. |
| 4388 | if (!ParamsSoFar.insert(ParmII)) { |
| 4389 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 4390 | } else { |
| 4391 | // Remember this identifier in ParamInfo. |
| 4392 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4393 | Tok.getLocation(), |
| 4394 | 0)); |
| 4395 | } |
| 4396 | |
| 4397 | // Eat the identifier. |
| 4398 | ConsumeToken(); |
| 4399 | |
| 4400 | // The list continues if we see a comma. |
| 4401 | if (Tok.isNot(tok::comma)) |
| 4402 | break; |
| 4403 | ConsumeToken(); |
| 4404 | } |
| 4405 | } |
| 4406 | |
| 4407 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 4408 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 4409 | /// identifier list. |
| 4410 | /// |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4411 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 4412 | /// caller parsed those arguments immediately after the open paren - they should |
| 4413 | /// be considered to be part of the first parameter. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4414 | /// |
| 4415 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 4416 | /// be the location of the ellipsis, if any was parsed. |
| 4417 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4418 | /// parameter-type-list: [C99 6.7.5] |
| 4419 | /// parameter-list |
| 4420 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4421 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4422 | /// |
| 4423 | /// parameter-list: [C99 6.7.5] |
| 4424 | /// parameter-declaration |
| 4425 | /// parameter-list ',' parameter-declaration |
| 4426 | /// |
| 4427 | /// parameter-declaration: [C99 6.7.5] |
| 4428 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4429 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4430 | /// [C++11] initializer-clause |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4431 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 4432 | /// declaration-specifiers abstract-declarator[opt] |
| 4433 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 4434 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4435 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4436 | /// [C++11] attribute-specifier-seq parameter-declaration |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4437 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4438 | void Parser::ParseParameterDeclarationClause( |
| 4439 | Declarator &D, |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4440 | ParsedAttributes &FirstArgAttrs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4441 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4442 | SourceLocation &EllipsisLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4443 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4444 | while (1) { |
| 4445 | if (Tok.is(tok::ellipsis)) { |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4446 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 4447 | // before deciding this was a parameter-declaration-clause. |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 4448 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4449 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4450 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4451 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4452 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4453 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4454 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4455 | |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4456 | // Parse any C++11 attributes. |
| 4457 | MaybeParseCXX0XAttributes(DS.getAttributes()); |
| 4458 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4459 | // Skip any Microsoft attributes before a param. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4460 | if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4461 | ParseMicrosoftAttributes(DS.getAttributes()); |
| 4462 | |
| 4463 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4464 | |
| 4465 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4466 | // 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] | 4467 | // 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] | 4468 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 4469 | // too much hassle. |
| 4470 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4471 | |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4472 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4473 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4474 | // Parse the declarator. This is "PrototypeContext", because we must |
| 4475 | // accept either 'declarator' or 'abstract-declarator' here. |
| 4476 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 4477 | ParseDeclarator(ParmDecl); |
| 4478 | |
| 4479 | // Parse GNU attributes, if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4480 | MaybeParseGNUAttributes(ParmDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4481 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4482 | // Remember this parsed parameter in ParamInfo. |
| 4483 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4484 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4485 | // DefArgToks is used when the parsing of default arguments needs |
| 4486 | // to be delayed. |
| 4487 | CachedTokens *DefArgToks = 0; |
| 4488 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4489 | // If no parameter was specified, verify that *something* was specified, |
| 4490 | // otherwise we have a missing type and identifier. |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4491 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 4492 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4493 | // Completely missing, emit error. |
| 4494 | Diag(DSStart, diag::err_missing_param); |
| 4495 | } else { |
| 4496 | // Otherwise, we have something. Add it and let semantic analysis try |
| 4497 | // 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] | 4498 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4499 | // Inform the actions module about the parameter declarator, so it gets |
| 4500 | // added to the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4501 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4502 | |
| 4503 | // Parse the default argument, if any. We parse the default |
| 4504 | // arguments in all dialects; the semantic analysis in |
| 4505 | // ActOnParamDefaultArgument will reject the default argument in |
| 4506 | // C. |
| 4507 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4508 | SourceLocation EqualLoc = Tok.getLocation(); |
| 4509 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4510 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4511 | if (D.getContext() == Declarator::MemberContext) { |
| 4512 | // If we're inside a class definition, cache the tokens |
| 4513 | // corresponding to the default argument. We'll actually parse |
| 4514 | // them when we see the end of the class definition. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4515 | // FIXME: Can we use a smart pointer for Toks? |
| 4516 | DefArgToks = new CachedTokens; |
| 4517 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4518 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 4519 | /*StopAtSemi=*/true, |
| 4520 | /*ConsumeFinalToken=*/false)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4521 | delete DefArgToks; |
| 4522 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4523 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4524 | } else { |
| 4525 | // Mark the end of the default argument so that we know when to |
| 4526 | // stop when we parse it later on. |
| 4527 | Token DefArgEnd; |
| 4528 | DefArgEnd.startToken(); |
| 4529 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 4530 | DefArgEnd.setLocation(Tok.getLocation()); |
| 4531 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4532 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4533 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4534 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4535 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4536 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4537 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4538 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4539 | // The argument isn't actually potentially evaluated unless it is |
| 4540 | // used. |
| 4541 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 4542 | Sema::PotentiallyEvaluatedIfUsed, |
| 4543 | Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4544 | |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4545 | ExprResult DefArgResult; |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 4546 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) { |
| 4547 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4548 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 4549 | } else |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4550 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4551 | if (DefArgResult.isInvalid()) { |
| 4552 | Actions.ActOnParamDefaultArgumentError(Param); |
| 4553 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 4554 | } else { |
| 4555 | // Inform the actions module about the default argument |
| 4556 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4557 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4558 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4559 | } |
| 4560 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4561 | |
| 4562 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4563 | ParmDecl.getIdentifierLoc(), Param, |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4564 | DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4565 | } |
| 4566 | |
| 4567 | // 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] | 4568 | if (Tok.isNot(tok::comma)) { |
| 4569 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4570 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
| 4571 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4572 | if (!getLangOpts().CPlusPlus) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4573 | // We have ellipsis without a preceding ',', which is ill-formed |
| 4574 | // in C. Complain and provide the fix. |
| 4575 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4576 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4577 | } |
| 4578 | } |
| 4579 | |
| 4580 | break; |
| 4581 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4582 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4583 | // Consume the comma. |
| 4584 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4585 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4586 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 4587 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4588 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4589 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4590 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4591 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4592 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4593 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4594 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 4595 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4596 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4597 | if (CheckProhibitedCXX11Attribute()) |
| 4598 | return; |
| 4599 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4600 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 4601 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4602 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4603 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 4604 | // This code does a fast path to handle some of the most obvious cases. |
| 4605 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4606 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4607 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4608 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4609 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4610 | // Remember that we parsed the empty array type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4611 | ExprResult NumElements; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4612 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4613 | T.getOpenLocation(), |
| 4614 | T.getCloseLocation()), |
| 4615 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4616 | return; |
| 4617 | } else if (Tok.getKind() == tok::numeric_constant && |
| 4618 | GetLookAheadToken(1).is(tok::r_square)) { |
| 4619 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 4620 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4621 | ConsumeToken(); |
| 4622 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4623 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4624 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4625 | MaybeParseCXX0XAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4626 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4627 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4628 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4629 | ExprRes.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4630 | T.getOpenLocation(), |
| 4631 | T.getCloseLocation()), |
| 4632 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4633 | return; |
| 4634 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4635 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4636 | // If valid, this location is the position where we read the 'static' keyword. |
| 4637 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4638 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4639 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4640 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4641 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4642 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4643 | DeclSpec DS(AttrFactory); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4644 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4645 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4646 | // If we haven't already read 'static', check to see if there is one after the |
| 4647 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4648 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4649 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4650 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4651 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 4652 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4653 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4654 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4655 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 4656 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
| 4657 | // the the token after the star is a ']'. Since stars in arrays are |
| 4658 | // infrequent, use of lookahead is not costly here. |
| 4659 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 4660 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4661 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4662 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4663 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4664 | StaticLoc = SourceLocation(); // Drop the static. |
| 4665 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4666 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4667 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4668 | // Note, in C89, this production uses the constant-expr production instead |
| 4669 | // of assignment-expr. The only difference is that assignment-expr allows |
| 4670 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 4671 | // 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] | 4672 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4673 | // Parse the constant-expression or assignment-expression now (depending |
| 4674 | // on dialect). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4675 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4676 | NumElements = ParseConstantExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4677 | } else { |
| 4678 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 4679 | Sema::ConstantEvaluated); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4680 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4681 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4682 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4683 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4684 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 4685 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 4686 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4687 | // If the expression was invalid, skip it. |
| 4688 | SkipUntil(tok::r_square); |
| 4689 | return; |
| 4690 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4691 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4692 | T.consumeClose(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4693 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4694 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4695 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4696 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4697 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4698 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4699 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4700 | NumElements.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4701 | T.getOpenLocation(), |
| 4702 | T.getCloseLocation()), |
| 4703 | attrs, T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4704 | } |
| 4705 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4706 | /// [GNU] typeof-specifier: |
| 4707 | /// typeof ( expressions ) |
| 4708 | /// typeof ( type-name ) |
| 4709 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4710 | /// |
| 4711 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4712 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4713 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4714 | SourceLocation StartLoc = ConsumeToken(); |
| 4715 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 4716 | const bool hasParens = Tok.is(tok::l_paren); |
| 4717 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4718 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
| 4719 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4720 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4721 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4722 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4723 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 4724 | CastTy, CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 4725 | if (hasParens) |
| 4726 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4727 | |
| 4728 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4729 | // FIXME: Not accurate, the range gets one token more than it should. |
| 4730 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4731 | else |
| 4732 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4733 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4734 | if (isCastExpr) { |
| 4735 | if (!CastTy) { |
| 4736 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4737 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 4738 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4739 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4740 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4741 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4742 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 4743 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4744 | DiagID, CastTy)) |
| 4745 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4746 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4747 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4748 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4749 | // If we get here, the operand to the typeof was an expresion. |
| 4750 | if (Operand.isInvalid()) { |
| 4751 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 4752 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4753 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4754 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4755 | // We might need to transform the operand if it is potentially evaluated. |
| 4756 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 4757 | if (Operand.isInvalid()) { |
| 4758 | DS.SetTypeSpecError(); |
| 4759 | return; |
| 4760 | } |
| 4761 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4762 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4763 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4764 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 4765 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4766 | DiagID, Operand.get())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4767 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4768 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4769 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 4770 | /// [C11] atomic-specifier: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4771 | /// _Atomic ( type-name ) |
| 4772 | /// |
| 4773 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
| 4774 | assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier"); |
| 4775 | |
| 4776 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4777 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4778 | if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) { |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4779 | SkipUntil(tok::r_paren); |
| 4780 | return; |
| 4781 | } |
| 4782 | |
| 4783 | TypeResult Result = ParseTypeName(); |
| 4784 | if (Result.isInvalid()) { |
| 4785 | SkipUntil(tok::r_paren); |
| 4786 | return; |
| 4787 | } |
| 4788 | |
| 4789 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4790 | T.consumeClose(); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4791 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4792 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4793 | return; |
| 4794 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4795 | DS.setTypeofParensRange(T.getRange()); |
| 4796 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4797 | |
| 4798 | const char *PrevSpec = 0; |
| 4799 | unsigned DiagID; |
| 4800 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
| 4801 | DiagID, Result.release())) |
| 4802 | Diag(StartLoc, DiagID) << PrevSpec; |
| 4803 | } |
| 4804 | |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4805 | |
| 4806 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 4807 | /// from TryAltiVecVectorToken. |
| 4808 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 4809 | Token Next = NextToken(); |
| 4810 | switch (Next.getKind()) { |
| 4811 | default: return false; |
| 4812 | case tok::kw_short: |
| 4813 | case tok::kw_long: |
| 4814 | case tok::kw_signed: |
| 4815 | case tok::kw_unsigned: |
| 4816 | case tok::kw_void: |
| 4817 | case tok::kw_char: |
| 4818 | case tok::kw_int: |
| 4819 | case tok::kw_float: |
| 4820 | case tok::kw_double: |
| 4821 | case tok::kw_bool: |
| 4822 | case tok::kw___pixel: |
| 4823 | Tok.setKind(tok::kw___vector); |
| 4824 | return true; |
| 4825 | case tok::identifier: |
| 4826 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 4827 | Tok.setKind(tok::kw___vector); |
| 4828 | return true; |
| 4829 | } |
| 4830 | return false; |
| 4831 | } |
| 4832 | } |
| 4833 | |
| 4834 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 4835 | const char *&PrevSpec, unsigned &DiagID, |
| 4836 | bool &isInvalid) { |
| 4837 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 4838 | Token Next = NextToken(); |
| 4839 | switch (Next.getKind()) { |
| 4840 | case tok::kw_short: |
| 4841 | case tok::kw_long: |
| 4842 | case tok::kw_signed: |
| 4843 | case tok::kw_unsigned: |
| 4844 | case tok::kw_void: |
| 4845 | case tok::kw_char: |
| 4846 | case tok::kw_int: |
| 4847 | case tok::kw_float: |
| 4848 | case tok::kw_double: |
| 4849 | case tok::kw_bool: |
| 4850 | case tok::kw___pixel: |
| 4851 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 4852 | return true; |
| 4853 | case tok::identifier: |
| 4854 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 4855 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 4856 | return true; |
| 4857 | } |
| 4858 | break; |
| 4859 | default: |
| 4860 | break; |
| 4861 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 4862 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4863 | DS.isTypeAltiVecVector()) { |
| 4864 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 4865 | return true; |
| 4866 | } |
| 4867 | return false; |
| 4868 | } |