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" |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Lookup.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Scope.h" |
| 19 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 20 | #include "clang/Sema/PrettyDeclStackTrace.h" |
Chris Lattner | d167ca0 | 2009-12-10 00:21:05 +0000 | [diff] [blame] | 21 | #include "RAIIObjectsForParser.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallSet.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallString.h" |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringSwitch.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | // C99 6.7: Declarations. |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
| 31 | /// ParseTypeName |
| 32 | /// type-name: [C99 6.7.6] |
| 33 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 34 | /// |
| 35 | /// Called type-id in C++. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 36 | TypeResult Parser::ParseTypeName(SourceRange *Range, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 37 | Declarator::TheContext Context, |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 38 | AccessSpecifier AS, |
| 39 | Decl **OwnedType) { |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 40 | DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context); |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 41 | if (DSC == DSC_normal) |
| 42 | DSC = DSC_type_specifier; |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 43 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | // Parse the common declaration-specifiers piece. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 45 | DeclSpec DS(AttrFactory); |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 46 | ParseSpecifierQualifierList(DS, AS, DSC); |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 47 | if (OwnedType) |
| 48 | *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0; |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 49 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 50 | // Parse the abstract-declarator, if present. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 51 | Declarator DeclaratorInfo(DS, Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 53 | if (Range) |
| 54 | *Range = DeclaratorInfo.getSourceRange(); |
| 55 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 56 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 57 | return true; |
| 58 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 59 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 62 | |
| 63 | /// isAttributeLateParsed - Return true if the attribute has arguments that |
| 64 | /// require late parsing. |
| 65 | static bool isAttributeLateParsed(const IdentifierInfo &II) { |
| 66 | return llvm::StringSwitch<bool>(II.getName()) |
| 67 | #include "clang/Parse/AttrLateParsed.inc" |
| 68 | .Default(false); |
| 69 | } |
| 70 | |
| 71 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 72 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 73 | /// |
| 74 | /// [GNU] attributes: |
| 75 | /// attribute |
| 76 | /// attributes attribute |
| 77 | /// |
| 78 | /// [GNU] attribute: |
| 79 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 80 | /// |
| 81 | /// [GNU] attribute-list: |
| 82 | /// attrib |
| 83 | /// attribute_list ',' attrib |
| 84 | /// |
| 85 | /// [GNU] attrib: |
| 86 | /// empty |
| 87 | /// attrib-name |
| 88 | /// attrib-name '(' identifier ')' |
| 89 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 90 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 91 | /// |
| 92 | /// [GNU] attrib-name: |
| 93 | /// identifier |
| 94 | /// typespec |
| 95 | /// typequal |
| 96 | /// storageclass |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 98 | /// FIXME: The GCC grammar/code for this construct implies we need two |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 100 | /// which is followed by a comma or close parenthesis, then the arguments |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 101 | /// start with that identifier; otherwise they are an expression list." |
| 102 | /// |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 103 | /// GCC does not require the ',' between attribs in an attribute-list. |
| 104 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 105 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 106 | /// any attributes that don't work (based on my limited testing). Most |
| 107 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 108 | /// a pressing need to implement the 2 token lookahead. |
| 109 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 110 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 111 | SourceLocation *endLoc, |
| 112 | LateParsedAttrList *LateAttrs) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 113 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 115 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 116 | ConsumeToken(); |
| 117 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 118 | "attribute")) { |
| 119 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 120 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 121 | } |
| 122 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 123 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 124 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 125 | } |
| 126 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 127 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 128 | Tok.is(tok::comma)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 130 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 131 | ConsumeToken(); |
| 132 | continue; |
| 133 | } |
| 134 | // we have an identifier or declaration specifier (const, int, etc.) |
| 135 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 136 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 138 | if (Tok.is(tok::l_paren)) { |
| 139 | // handle "parameterized" attributes |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 140 | if (LateAttrs && isAttributeLateParsed(*AttrName)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 141 | LateParsedAttribute *LA = |
| 142 | new LateParsedAttribute(this, *AttrName, AttrNameLoc); |
| 143 | LateAttrs->push_back(LA); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 144 | |
| 145 | // Attributes in a class are parsed at the end of the class, along |
| 146 | // with other late-parsed declarations. |
| 147 | if (!ClassStack.empty()) |
| 148 | getCurrentClass().LateParsedDeclarations.push_back(LA); |
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 | // consume everything up to and including the matching right parens |
| 151 | ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 153 | Token Eof; |
| 154 | Eof.startToken(); |
| 155 | Eof.setLocation(Tok.getLocation()); |
| 156 | LA->Toks.push_back(Eof); |
| 157 | } else { |
| 158 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 159 | } |
| 160 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 161 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 162 | 0, SourceLocation(), 0, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 166 | SkipUntil(tok::r_paren, false); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 167 | SourceLocation Loc = Tok.getLocation(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 168 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 169 | SkipUntil(tok::r_paren, false); |
| 170 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 171 | if (endLoc) |
| 172 | *endLoc = Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 173 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 176 | |
| 177 | /// Parse the arguments to a parameterized GNU attribute |
| 178 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 179 | SourceLocation AttrNameLoc, |
| 180 | ParsedAttributes &Attrs, |
| 181 | SourceLocation *EndLoc) { |
| 182 | |
| 183 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 184 | |
| 185 | // Availability attributes have their own grammar. |
| 186 | if (AttrName->isStr("availability")) { |
| 187 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 188 | return; |
| 189 | } |
| 190 | // Thread safety attributes fit into the FIXME case above, so we |
| 191 | // just parse the arguments as a list of expressions |
| 192 | if (IsThreadSafetyAttribute(AttrName->getName())) { |
| 193 | ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | ConsumeParen(); // ignore the left paren loc for now |
| 198 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 199 | IdentifierInfo *ParmName = 0; |
| 200 | SourceLocation ParmLoc; |
| 201 | bool BuiltinType = false; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 202 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 203 | switch (Tok.getKind()) { |
| 204 | case tok::kw_char: |
| 205 | case tok::kw_wchar_t: |
| 206 | case tok::kw_char16_t: |
| 207 | case tok::kw_char32_t: |
| 208 | case tok::kw_bool: |
| 209 | case tok::kw_short: |
| 210 | case tok::kw_int: |
| 211 | case tok::kw_long: |
| 212 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 213 | case tok::kw___int128: |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 214 | case tok::kw_signed: |
| 215 | case tok::kw_unsigned: |
| 216 | case tok::kw_float: |
| 217 | case tok::kw_double: |
| 218 | case tok::kw_void: |
| 219 | case tok::kw_typeof: |
| 220 | // __attribute__(( vec_type_hint(char) )) |
| 221 | // FIXME: Don't just discard the builtin type token. |
| 222 | ConsumeToken(); |
| 223 | BuiltinType = true; |
| 224 | break; |
| 225 | |
| 226 | case tok::identifier: |
| 227 | ParmName = Tok.getIdentifierInfo(); |
| 228 | ParmLoc = ConsumeToken(); |
| 229 | break; |
| 230 | |
| 231 | default: |
| 232 | break; |
| 233 | } |
| 234 | |
| 235 | ExprVector ArgExprs(Actions); |
| 236 | |
| 237 | if (!BuiltinType && |
| 238 | (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) { |
| 239 | // Eat the comma. |
| 240 | if (ParmLoc.isValid()) |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 241 | ConsumeToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 242 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 243 | // Parse the non-empty comma-separated list of expressions. |
| 244 | while (1) { |
| 245 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 246 | if (ArgExpr.isInvalid()) { |
| 247 | SkipUntil(tok::r_paren); |
| 248 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 249 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 250 | ArgExprs.push_back(ArgExpr.release()); |
| 251 | if (Tok.isNot(tok::comma)) |
| 252 | break; |
| 253 | ConsumeToken(); // Eat the comma, move to the next argument |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 254 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 255 | } |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 256 | else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) { |
| 257 | if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<", |
| 258 | tok::greater)) { |
Fariborz Jahanian | b224343 | 2011-10-18 23:13:50 +0000 | [diff] [blame] | 259 | while (Tok.is(tok::identifier)) { |
| 260 | ConsumeToken(); |
| 261 | if (Tok.is(tok::greater)) |
| 262 | break; |
| 263 | if (Tok.is(tok::comma)) { |
| 264 | ConsumeToken(); |
| 265 | continue; |
| 266 | } |
| 267 | } |
| 268 | if (Tok.isNot(tok::greater)) |
| 269 | Diag(Tok, diag::err_iboutletcollection_with_protocol); |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 270 | SkipUntil(tok::r_paren, false, true); // skip until ')' |
| 271 | } |
| 272 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 273 | |
| 274 | SourceLocation RParen = Tok.getLocation(); |
| 275 | if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 276 | AttributeList *attr = |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 277 | Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc, |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 278 | ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size()); |
Michael Han | e53ac8a | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 279 | if (BuiltinType && attr->getKind() == AttributeList::AT_iboutletcollection) |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 280 | Diag(Tok, diag::err_iboutletcollection_builtintype); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
| 284 | |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 285 | /// ParseMicrosoftDeclSpec - Parse an __declspec construct |
| 286 | /// |
| 287 | /// [MS] decl-specifier: |
| 288 | /// __declspec ( extended-decl-modifier-seq ) |
| 289 | /// |
| 290 | /// [MS] extended-decl-modifier-seq: |
| 291 | /// extended-decl-modifier[opt] |
| 292 | /// extended-decl-modifier extended-decl-modifier-seq |
| 293 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 294 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) { |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 295 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 296 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 297 | ConsumeToken(); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 298 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 299 | "declspec")) { |
| 300 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 301 | return; |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 302 | } |
Francois Pichet | 373197b | 2011-05-07 19:04:49 +0000 | [diff] [blame] | 303 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 304 | while (Tok.getIdentifierInfo()) { |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 305 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 306 | SourceLocation AttrNameLoc = ConsumeToken(); |
Francois Pichet | 373197b | 2011-05-07 19:04:49 +0000 | [diff] [blame] | 307 | |
| 308 | // FIXME: Remove this when we have proper __declspec(property()) support. |
| 309 | // Just skip everything inside property(). |
| 310 | if (AttrName->getName() == "property") { |
| 311 | ConsumeParen(); |
| 312 | SkipUntil(tok::r_paren); |
| 313 | } |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 314 | if (Tok.is(tok::l_paren)) { |
| 315 | ConsumeParen(); |
| 316 | // FIXME: This doesn't parse __declspec(property(get=get_func_name)) |
| 317 | // correctly. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 318 | ExprResult ArgExpr(ParseAssignmentExpression()); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 319 | if (!ArgExpr.isInvalid()) { |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 320 | Expr *ExprList = ArgExpr.take(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 321 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 322 | SourceLocation(), &ExprList, 1, true); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 323 | } |
| 324 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 325 | SkipUntil(tok::r_paren, false); |
| 326 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 327 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 328 | 0, SourceLocation(), 0, 0, true); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 332 | SkipUntil(tok::r_paren, false); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 333 | return; |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 334 | } |
| 335 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 336 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 337 | // Treat these like attributes |
| 338 | // FIXME: Allow Sema to distinguish between these and real attributes! |
| 339 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 340 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 341 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 342 | Tok.is(tok::kw___ptr32) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 343 | Tok.is(tok::kw___unaligned)) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 344 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 345 | SourceLocation AttrNameLoc = ConsumeToken(); |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 346 | if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
| 347 | Tok.is(tok::kw___ptr32)) |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 348 | // FIXME: Support these properly! |
| 349 | continue; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 350 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 351 | SourceLocation(), 0, 0, true); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 352 | } |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 353 | } |
| 354 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 355 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 356 | // Treat these like attributes |
| 357 | while (Tok.is(tok::kw___pascal)) { |
| 358 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 359 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 360 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 361 | SourceLocation(), 0, 0, true); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 362 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 365 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 366 | // Treat these like attributes |
| 367 | while (Tok.is(tok::kw___kernel)) { |
| 368 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 369 | attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"), |
| 370 | AttrNameLoc, 0, AttrNameLoc, 0, |
| 371 | SourceLocation(), 0, 0, false); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 375 | void Parser::ParseOpenCLQualifiers(DeclSpec &DS) { |
| 376 | SourceLocation Loc = Tok.getLocation(); |
| 377 | switch(Tok.getKind()) { |
| 378 | // OpenCL qualifiers: |
| 379 | case tok::kw___private: |
| 380 | case tok::kw_private: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 381 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 382 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 383 | PP.getIdentifierInfo("address_space"), Loc, 0); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 384 | break; |
| 385 | |
| 386 | case tok::kw___global: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 387 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 388 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 389 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 390 | break; |
| 391 | |
| 392 | case tok::kw___local: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 393 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 394 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 395 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 396 | break; |
| 397 | |
| 398 | case tok::kw___constant: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 399 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 400 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 401 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 402 | break; |
| 403 | |
| 404 | case tok::kw___read_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 405 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 406 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 407 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 408 | break; |
| 409 | |
| 410 | case tok::kw___write_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 411 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 412 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 413 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 414 | break; |
| 415 | |
| 416 | case tok::kw___read_write: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 417 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 418 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 419 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 420 | break; |
| 421 | default: break; |
| 422 | } |
| 423 | } |
| 424 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 425 | /// \brief Parse a version number. |
| 426 | /// |
| 427 | /// version: |
| 428 | /// simple-integer |
| 429 | /// simple-integer ',' simple-integer |
| 430 | /// simple-integer ',' simple-integer ',' simple-integer |
| 431 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 432 | Range = Tok.getLocation(); |
| 433 | |
| 434 | if (!Tok.is(tok::numeric_constant)) { |
| 435 | Diag(Tok, diag::err_expected_version); |
| 436 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 437 | return VersionTuple(); |
| 438 | } |
| 439 | |
| 440 | // Parse the major (and possibly minor and subminor) versions, which |
| 441 | // are stored in the numeric constant. We utilize a quirk of the |
| 442 | // lexer, which is that it handles something like 1.2.3 as a single |
| 443 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 444 | SmallString<512> Buffer; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 445 | Buffer.resize(Tok.getLength()+1); |
| 446 | const char *ThisTokBegin = &Buffer[0]; |
| 447 | |
| 448 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 449 | bool Invalid = false; |
| 450 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 451 | if (Invalid) |
| 452 | return VersionTuple(); |
| 453 | |
| 454 | // Parse the major version. |
| 455 | unsigned AfterMajor = 0; |
| 456 | unsigned Major = 0; |
| 457 | while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) { |
| 458 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 459 | ++AfterMajor; |
| 460 | } |
| 461 | |
| 462 | if (AfterMajor == 0) { |
| 463 | Diag(Tok, diag::err_expected_version); |
| 464 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 465 | return VersionTuple(); |
| 466 | } |
| 467 | |
| 468 | if (AfterMajor == ActualLength) { |
| 469 | ConsumeToken(); |
| 470 | |
| 471 | // We only had a single version component. |
| 472 | if (Major == 0) { |
| 473 | Diag(Tok, diag::err_zero_version); |
| 474 | return VersionTuple(); |
| 475 | } |
| 476 | |
| 477 | return VersionTuple(Major); |
| 478 | } |
| 479 | |
| 480 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 481 | Diag(Tok, diag::err_expected_version); |
| 482 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 483 | return VersionTuple(); |
| 484 | } |
| 485 | |
| 486 | // Parse the minor version. |
| 487 | unsigned AfterMinor = AfterMajor + 1; |
| 488 | unsigned Minor = 0; |
| 489 | while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) { |
| 490 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 491 | ++AfterMinor; |
| 492 | } |
| 493 | |
| 494 | if (AfterMinor == ActualLength) { |
| 495 | ConsumeToken(); |
| 496 | |
| 497 | // We had major.minor. |
| 498 | if (Major == 0 && Minor == 0) { |
| 499 | Diag(Tok, diag::err_zero_version); |
| 500 | return VersionTuple(); |
| 501 | } |
| 502 | |
| 503 | return VersionTuple(Major, Minor); |
| 504 | } |
| 505 | |
| 506 | // If what follows is not a '.', we have a problem. |
| 507 | if (ThisTokBegin[AfterMinor] != '.') { |
| 508 | Diag(Tok, diag::err_expected_version); |
| 509 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 510 | return VersionTuple(); |
| 511 | } |
| 512 | |
| 513 | // Parse the subminor version. |
| 514 | unsigned AfterSubminor = AfterMinor + 1; |
| 515 | unsigned Subminor = 0; |
| 516 | while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) { |
| 517 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 518 | ++AfterSubminor; |
| 519 | } |
| 520 | |
| 521 | if (AfterSubminor != ActualLength) { |
| 522 | Diag(Tok, diag::err_expected_version); |
| 523 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 524 | return VersionTuple(); |
| 525 | } |
| 526 | ConsumeToken(); |
| 527 | return VersionTuple(Major, Minor, Subminor); |
| 528 | } |
| 529 | |
| 530 | /// \brief Parse the contents of the "availability" attribute. |
| 531 | /// |
| 532 | /// availability-attribute: |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 533 | /// 'availability' '(' platform ',' version-arg-list, opt-message')' |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 534 | /// |
| 535 | /// platform: |
| 536 | /// identifier |
| 537 | /// |
| 538 | /// version-arg-list: |
| 539 | /// version-arg |
| 540 | /// version-arg ',' version-arg-list |
| 541 | /// |
| 542 | /// version-arg: |
| 543 | /// 'introduced' '=' version |
| 544 | /// 'deprecated' '=' version |
Douglas Gregor | 93a7067 | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 545 | /// 'obsoleted' = version |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 546 | /// 'unavailable' |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 547 | /// opt-message: |
| 548 | /// 'message' '=' <string> |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 549 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 550 | SourceLocation AvailabilityLoc, |
| 551 | ParsedAttributes &attrs, |
| 552 | SourceLocation *endLoc) { |
| 553 | SourceLocation PlatformLoc; |
| 554 | IdentifierInfo *Platform = 0; |
| 555 | |
| 556 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 557 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 558 | ExprResult MessageExpr; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 559 | |
| 560 | // Opening '('. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 561 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 562 | if (T.consumeOpen()) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 563 | Diag(Tok, diag::err_expected_lparen); |
| 564 | return; |
| 565 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 566 | |
| 567 | // Parse the platform name, |
| 568 | if (Tok.isNot(tok::identifier)) { |
| 569 | Diag(Tok, diag::err_availability_expected_platform); |
| 570 | SkipUntil(tok::r_paren); |
| 571 | return; |
| 572 | } |
| 573 | Platform = Tok.getIdentifierInfo(); |
| 574 | PlatformLoc = ConsumeToken(); |
| 575 | |
| 576 | // Parse the ',' following the platform name. |
| 577 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren)) |
| 578 | return; |
| 579 | |
| 580 | // If we haven't grabbed the pointers for the identifiers |
| 581 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 582 | if (!Ident_introduced) { |
| 583 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 584 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 585 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 586 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 587 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 591 | SourceLocation UnavailableLoc; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 592 | do { |
| 593 | if (Tok.isNot(tok::identifier)) { |
| 594 | Diag(Tok, diag::err_availability_expected_change); |
| 595 | SkipUntil(tok::r_paren); |
| 596 | return; |
| 597 | } |
| 598 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 599 | SourceLocation KeywordLoc = ConsumeToken(); |
| 600 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 601 | if (Keyword == Ident_unavailable) { |
| 602 | if (UnavailableLoc.isValid()) { |
| 603 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 604 | << Keyword << SourceRange(UnavailableLoc); |
| 605 | } |
| 606 | UnavailableLoc = KeywordLoc; |
| 607 | |
| 608 | if (Tok.isNot(tok::comma)) |
| 609 | break; |
| 610 | |
| 611 | ConsumeToken(); |
| 612 | continue; |
| 613 | } |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 614 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 615 | if (Tok.isNot(tok::equal)) { |
| 616 | Diag(Tok, diag::err_expected_equal_after) |
| 617 | << Keyword; |
| 618 | SkipUntil(tok::r_paren); |
| 619 | return; |
| 620 | } |
| 621 | ConsumeToken(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 622 | if (Keyword == Ident_message) { |
| 623 | if (!isTokenStringLiteral()) { |
| 624 | Diag(Tok, diag::err_expected_string_literal); |
| 625 | SkipUntil(tok::r_paren); |
| 626 | return; |
| 627 | } |
| 628 | MessageExpr = ParseStringLiteralExpression(); |
| 629 | break; |
| 630 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 631 | |
| 632 | SourceRange VersionRange; |
| 633 | VersionTuple Version = ParseVersionTuple(VersionRange); |
| 634 | |
| 635 | if (Version.empty()) { |
| 636 | SkipUntil(tok::r_paren); |
| 637 | return; |
| 638 | } |
| 639 | |
| 640 | unsigned Index; |
| 641 | if (Keyword == Ident_introduced) |
| 642 | Index = Introduced; |
| 643 | else if (Keyword == Ident_deprecated) |
| 644 | Index = Deprecated; |
| 645 | else if (Keyword == Ident_obsoleted) |
| 646 | Index = Obsoleted; |
| 647 | else |
| 648 | Index = Unknown; |
| 649 | |
| 650 | if (Index < Unknown) { |
| 651 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 652 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 653 | << Keyword |
| 654 | << SourceRange(Changes[Index].KeywordLoc, |
| 655 | Changes[Index].VersionRange.getEnd()); |
| 656 | } |
| 657 | |
| 658 | Changes[Index].KeywordLoc = KeywordLoc; |
| 659 | Changes[Index].Version = Version; |
| 660 | Changes[Index].VersionRange = VersionRange; |
| 661 | } else { |
| 662 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 663 | << Keyword << VersionRange; |
| 664 | } |
| 665 | |
| 666 | if (Tok.isNot(tok::comma)) |
| 667 | break; |
| 668 | |
| 669 | ConsumeToken(); |
| 670 | } while (true); |
| 671 | |
| 672 | // Closing ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 673 | if (T.consumeClose()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 674 | return; |
| 675 | |
| 676 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 677 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 678 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 679 | // The 'unavailable' availability cannot be combined with any other |
| 680 | // availability changes. Make sure that hasn't happened. |
| 681 | if (UnavailableLoc.isValid()) { |
| 682 | bool Complained = false; |
| 683 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 684 | if (Changes[Index].KeywordLoc.isValid()) { |
| 685 | if (!Complained) { |
| 686 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 687 | << SourceRange(Changes[Index].KeywordLoc, |
| 688 | Changes[Index].VersionRange.getEnd()); |
| 689 | Complained = true; |
| 690 | } |
| 691 | |
| 692 | // Clear out the availability. |
| 693 | Changes[Index] = AvailabilityChange(); |
| 694 | } |
| 695 | } |
| 696 | } |
| 697 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 698 | // Record this attribute |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 699 | attrs.addNew(&Availability, |
| 700 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Fariborz Jahanian | f96708d | 2012-01-23 23:38:32 +0000 | [diff] [blame] | 701 | 0, AvailabilityLoc, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 702 | Platform, PlatformLoc, |
| 703 | Changes[Introduced], |
| 704 | Changes[Deprecated], |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 705 | Changes[Obsoleted], |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 706 | UnavailableLoc, MessageExpr.take(), |
| 707 | false, false); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 708 | } |
| 709 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 710 | |
| 711 | // Late Parsed Attributes: |
| 712 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 713 | |
| 714 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 715 | |
| 716 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 717 | Self->ParseLexedAttributes(*Class); |
| 718 | } |
| 719 | |
| 720 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 721 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 725 | /// scope appropriately. |
| 726 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 727 | // Deal with templates |
| 728 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 729 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 730 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 731 | HasTemplateScope); |
| 732 | if (HasTemplateScope) |
| 733 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 734 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 735 | // Set or update the scope flags. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 736 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 737 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 738 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 739 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 740 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 741 | // Enter the scope of nested classes |
| 742 | if (!AlreadyHasClassScope) |
| 743 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 744 | Class.TagOrTemplate); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 745 | { |
| 746 | // Allow 'this' within late-parsed attributes. |
| 747 | Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate, |
| 748 | /*TypeQuals=*/0); |
| 749 | |
| 750 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 751 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 752 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 753 | } |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 754 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 755 | if (!AlreadyHasClassScope) |
| 756 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 757 | Class.TagOrTemplate); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 758 | } |
| 759 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 760 | |
| 761 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 762 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 763 | bool EnterScope, bool OnDefinition) { |
| 764 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 765 | LAs[i]->addDecl(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 766 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | d306cf7 | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 767 | delete LAs[i]; |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 768 | } |
| 769 | LAs.clear(); |
| 770 | } |
| 771 | |
| 772 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 773 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 774 | /// This will be called at the end of parsing a class declaration |
| 775 | /// for each LateParsedAttribute. We consume the saved tokens and |
| 776 | /// create an attribute with the arguments filled in. We add this |
| 777 | /// to the Attribute list for the decl. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 778 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 779 | bool EnterScope, bool OnDefinition) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 780 | // Save the current token position. |
| 781 | SourceLocation OrigLoc = Tok.getLocation(); |
| 782 | |
| 783 | // Append the current token at the end of the new token stream so that it |
| 784 | // doesn't get lost. |
| 785 | LA.Toks.push_back(Tok); |
| 786 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 787 | // Consume the previously pushed token. |
| 788 | ConsumeAnyToken(); |
| 789 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 790 | if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) { |
| 791 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 792 | << LA.AttrName.getName(); |
| 793 | } |
| 794 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 795 | ParsedAttributes Attrs(AttrFactory); |
| 796 | SourceLocation endLoc; |
| 797 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 798 | if (LA.Decls.size() == 1) { |
| 799 | Decl *D = LA.Decls[0]; |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 800 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 801 | // If the Decl is templatized, add template parameters to scope. |
| 802 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 803 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 804 | if (HasTemplateScope) |
| 805 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 806 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 807 | // If the Decl is on a function, add function parameters to the scope. |
| 808 | bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
| 809 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope); |
| 810 | if (HasFunctionScope) |
| 811 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
| 812 | |
| 813 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc); |
| 814 | |
| 815 | if (HasFunctionScope) { |
| 816 | Actions.ActOnExitFunctionContext(); |
| 817 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 818 | } |
| 819 | if (HasTemplateScope) { |
| 820 | TempScope.Exit(); |
| 821 | } |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 822 | } else if (LA.Decls.size() > 0) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 823 | // If there are multiple decls, then the decl cannot be within the |
| 824 | // function scope. |
| 825 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc); |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 826 | } else { |
| 827 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 828 | } |
| 829 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 830 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) { |
| 831 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
| 832 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 833 | |
| 834 | if (Tok.getLocation() != OrigLoc) { |
| 835 | // Due to a parsing error, we either went over the cached tokens or |
| 836 | // there are still cached tokens left, so we skip the leftover tokens. |
| 837 | // Since this is an uncommon situation that should be avoided, use the |
| 838 | // expensive isBeforeInTranslationUnit call. |
| 839 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 840 | OrigLoc)) |
| 841 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
Douglas Gregor | d78ef5b | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 842 | ConsumeAnyToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 843 | } |
| 844 | } |
| 845 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 846 | /// \brief Wrapper around a case statement checking if AttrName is |
| 847 | /// one of the thread safety attributes |
| 848 | bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){ |
| 849 | return llvm::StringSwitch<bool>(AttrName) |
| 850 | .Case("guarded_by", true) |
| 851 | .Case("guarded_var", true) |
| 852 | .Case("pt_guarded_by", true) |
| 853 | .Case("pt_guarded_var", true) |
| 854 | .Case("lockable", true) |
| 855 | .Case("scoped_lockable", true) |
| 856 | .Case("no_thread_safety_analysis", true) |
| 857 | .Case("acquired_after", true) |
| 858 | .Case("acquired_before", true) |
| 859 | .Case("exclusive_lock_function", true) |
| 860 | .Case("shared_lock_function", true) |
| 861 | .Case("exclusive_trylock_function", true) |
| 862 | .Case("shared_trylock_function", true) |
| 863 | .Case("unlock_function", true) |
| 864 | .Case("lock_returned", true) |
| 865 | .Case("locks_excluded", true) |
| 866 | .Case("exclusive_locks_required", true) |
| 867 | .Case("shared_locks_required", true) |
| 868 | .Default(false); |
| 869 | } |
| 870 | |
| 871 | /// \brief Parse the contents of thread safety attributes. These |
| 872 | /// should always be parsed as an expression list. |
| 873 | /// |
| 874 | /// We need to special case the parsing due to the fact that if the first token |
| 875 | /// of the first argument is an identifier, the main parse loop will store |
| 876 | /// that token as a "parameter" and the rest of |
| 877 | /// the arguments will be added to a list of "arguments". However, |
| 878 | /// subsequent tokens in the first argument are lost. We instead parse each |
| 879 | /// argument as an expression and add all arguments to the list of "arguments". |
| 880 | /// In future, we will take advantage of this special case to also |
| 881 | /// deal with some argument scoping issues here (for example, referring to a |
| 882 | /// function parameter in the attribute on that function). |
| 883 | void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName, |
| 884 | SourceLocation AttrNameLoc, |
| 885 | ParsedAttributes &Attrs, |
| 886 | SourceLocation *EndLoc) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 887 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 888 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 889 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 890 | T.consumeOpen(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 891 | |
| 892 | ExprVector ArgExprs(Actions); |
| 893 | bool ArgExprsOk = true; |
| 894 | |
| 895 | // now parse the list of expressions |
DeLesley Hutchins | 4805f15 | 2011-12-14 19:36:06 +0000 | [diff] [blame] | 896 | while (Tok.isNot(tok::r_paren)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 897 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 898 | if (ArgExpr.isInvalid()) { |
| 899 | ArgExprsOk = false; |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 900 | T.consumeClose(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 901 | break; |
| 902 | } else { |
| 903 | ArgExprs.push_back(ArgExpr.release()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 904 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 905 | if (Tok.isNot(tok::comma)) |
| 906 | break; |
| 907 | ConsumeToken(); // Eat the comma, move to the next argument |
| 908 | } |
| 909 | // Match the ')'. |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 910 | if (ArgExprsOk && !T.consumeClose()) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 911 | Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
| 912 | ArgExprs.take(), ArgExprs.size()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 913 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 914 | if (EndLoc) |
| 915 | *EndLoc = T.getCloseLocation(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 916 | } |
| 917 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 918 | /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets |
| 919 | /// of a C++11 attribute-specifier in a location where an attribute is not |
| 920 | /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this |
| 921 | /// situation. |
| 922 | /// |
| 923 | /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if |
| 924 | /// this doesn't appear to actually be an attribute-specifier, and the caller |
| 925 | /// should try to parse it. |
| 926 | bool Parser::DiagnoseProhibitedCXX11Attribute() { |
| 927 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)); |
| 928 | |
| 929 | switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) { |
| 930 | case CAK_NotAttributeSpecifier: |
| 931 | // No diagnostic: we're in Obj-C++11 and this is not actually an attribute. |
| 932 | return false; |
| 933 | |
| 934 | case CAK_InvalidAttributeSpecifier: |
| 935 | Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute); |
| 936 | return false; |
| 937 | |
| 938 | case CAK_AttributeSpecifier: |
| 939 | // Parse and discard the attributes. |
| 940 | SourceLocation BeginLoc = ConsumeBracket(); |
| 941 | ConsumeBracket(); |
| 942 | SkipUntil(tok::r_square, /*StopAtSemi*/ false); |
| 943 | assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied"); |
| 944 | SourceLocation EndLoc = ConsumeBracket(); |
| 945 | Diag(BeginLoc, diag::err_attributes_not_allowed) |
| 946 | << SourceRange(BeginLoc, EndLoc); |
| 947 | return true; |
| 948 | } |
Chandler Carruth | 2c6dbd7 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 949 | llvm_unreachable("All cases handled above."); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 950 | } |
| 951 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 952 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 953 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 954 | << attrs.Range; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 957 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 958 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 959 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 960 | /// location of the semicolon in DeclEnd. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 961 | /// |
| 962 | /// declaration: [C99 6.7] |
| 963 | /// block-declaration -> |
| 964 | /// simple-declaration |
| 965 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 966 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 967 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 968 | /// [C++] using-directive |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 969 | /// [C++] using-declaration |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 970 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 971 | /// others... [FIXME] |
| 972 | /// |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 973 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 974 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 975 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 976 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 977 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | e8cff36 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 978 | // Must temporarily exit the objective-c container scope for |
| 979 | // parsing c none objective-c decls. |
| 980 | ObjCDeclContextSwitch ObjCDC(*this); |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 981 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 982 | Decl *SingleDecl = 0; |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 983 | Decl *OwnedType = 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 984 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 985 | case tok::kw_template: |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 986 | case tok::kw_export: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 987 | ProhibitAttributes(attrs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 988 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 989 | break; |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 990 | case tok::kw_inline: |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 991 | // 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] | 992 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 993 | ProhibitAttributes(attrs); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 994 | SourceLocation InlineLoc = ConsumeToken(); |
| 995 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 996 | break; |
| 997 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 998 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 999 | true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1000 | case tok::kw_namespace: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1001 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1002 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1003 | break; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1004 | case tok::kw_using: |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1005 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1006 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1007 | break; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1008 | case tok::kw_static_assert: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1009 | case tok::kw__Static_assert: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1010 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1011 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1012 | break; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1013 | default: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1014 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1015 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1016 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1017 | // 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] | 1018 | // single decl, convert it now. Alias declarations can also declare a type; |
| 1019 | // include that too if it is present. |
| 1020 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
| 1023 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1024 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 1025 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 1026 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1027 | /// |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1028 | /// for-range-declaration: [C++0x 6.5p1: stmt.ranged] |
| 1029 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1030 | /// |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1031 | /// 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] | 1032 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1033 | /// |
| 1034 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 1035 | /// of a simple-declaration. If we find that we are, we also parse the |
| 1036 | /// for-range-initializer, and place it here. |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1037 | Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts, |
| 1038 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1039 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1040 | ParsedAttributes &attrs, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1041 | bool RequireSemi, |
| 1042 | ForRangeInit *FRI) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1043 | // Parse the common declaration-specifiers piece. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1044 | ParsingDeclSpec DS(*this); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1045 | DS.takeAttributesFrom(attrs); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1046 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1047 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1048 | getDeclSpecContextFromDeclaratorContext(Context)); |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1049 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1050 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1051 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1052 | if (Tok.is(tok::semi)) { |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1053 | if (RequireSemi) ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1054 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1055 | DS); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1056 | DS.complete(TheDecl); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1057 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1058 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1059 | |
| 1060 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1061 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1062 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1063 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1064 | /// for a declarator. |
| 1065 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1066 | switch (Tok.getKind()) { |
| 1067 | case tok::annot_cxxscope: |
| 1068 | case tok::annot_template_id: |
| 1069 | case tok::caret: |
| 1070 | case tok::code_completion: |
| 1071 | case tok::coloncolon: |
| 1072 | case tok::ellipsis: |
| 1073 | case tok::kw___attribute: |
| 1074 | case tok::kw_operator: |
| 1075 | case tok::l_paren: |
| 1076 | case tok::star: |
| 1077 | return true; |
| 1078 | |
| 1079 | case tok::amp: |
| 1080 | case tok::ampamp: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1081 | return getLangOpts().CPlusPlus; |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1082 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1083 | 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] | 1084 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x && |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1085 | NextToken().is(tok::l_square); |
| 1086 | |
| 1087 | 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] | 1088 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1089 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1090 | case tok::identifier: |
| 1091 | switch (NextToken().getKind()) { |
| 1092 | case tok::code_completion: |
| 1093 | case tok::coloncolon: |
| 1094 | case tok::comma: |
| 1095 | case tok::equal: |
| 1096 | case tok::equalequal: // Might be a typo for '='. |
| 1097 | case tok::kw_alignas: |
| 1098 | case tok::kw_asm: |
| 1099 | case tok::kw___attribute: |
| 1100 | case tok::l_brace: |
| 1101 | case tok::l_paren: |
| 1102 | case tok::l_square: |
| 1103 | case tok::less: |
| 1104 | case tok::r_brace: |
| 1105 | case tok::r_paren: |
| 1106 | case tok::r_square: |
| 1107 | case tok::semi: |
| 1108 | return true; |
| 1109 | |
| 1110 | case tok::colon: |
| 1111 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1112 | // and in block scope it's probably a label. Inside a class definition, |
| 1113 | // this is a bit-field. |
| 1114 | return Context == Declarator::MemberContext || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1115 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1116 | |
| 1117 | case tok::identifier: // Possible virt-specifier. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1118 | return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken()); |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1119 | |
| 1120 | default: |
| 1121 | return false; |
| 1122 | } |
| 1123 | |
| 1124 | default: |
| 1125 | return false; |
| 1126 | } |
| 1127 | } |
| 1128 | |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1129 | /// Skip until we reach something which seems like a sensible place to pick |
| 1130 | /// up parsing after a malformed declaration. This will sometimes stop sooner |
| 1131 | /// than SkipUntil(tok::r_brace) would, but will never stop later. |
| 1132 | void Parser::SkipMalformedDecl() { |
| 1133 | while (true) { |
| 1134 | switch (Tok.getKind()) { |
| 1135 | case tok::l_brace: |
| 1136 | // Skip until matching }, then stop. We've probably skipped over |
| 1137 | // a malformed class or function definition or similar. |
| 1138 | ConsumeBrace(); |
| 1139 | SkipUntil(tok::r_brace, /*StopAtSemi*/false); |
| 1140 | if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) { |
| 1141 | // This declaration isn't over yet. Keep skipping. |
| 1142 | continue; |
| 1143 | } |
| 1144 | if (Tok.is(tok::semi)) |
| 1145 | ConsumeToken(); |
| 1146 | return; |
| 1147 | |
| 1148 | case tok::l_square: |
| 1149 | ConsumeBracket(); |
| 1150 | SkipUntil(tok::r_square, /*StopAtSemi*/false); |
| 1151 | continue; |
| 1152 | |
| 1153 | case tok::l_paren: |
| 1154 | ConsumeParen(); |
| 1155 | SkipUntil(tok::r_paren, /*StopAtSemi*/false); |
| 1156 | continue; |
| 1157 | |
| 1158 | case tok::r_brace: |
| 1159 | return; |
| 1160 | |
| 1161 | case tok::semi: |
| 1162 | ConsumeToken(); |
| 1163 | return; |
| 1164 | |
| 1165 | case tok::kw_inline: |
| 1166 | // 'inline namespace' at the start of a line is almost certainly |
| 1167 | // a good place to pick back up parsing. |
| 1168 | if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace)) |
| 1169 | return; |
| 1170 | break; |
| 1171 | |
| 1172 | case tok::kw_namespace: |
| 1173 | // 'namespace' at the start of a line is almost certainly a good |
| 1174 | // place to pick back up parsing. |
| 1175 | if (Tok.isAtStartOfLine()) |
| 1176 | return; |
| 1177 | break; |
| 1178 | |
| 1179 | case tok::eof: |
| 1180 | return; |
| 1181 | |
| 1182 | default: |
| 1183 | break; |
| 1184 | } |
| 1185 | |
| 1186 | ConsumeAnyToken(); |
| 1187 | } |
| 1188 | } |
| 1189 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1190 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1191 | /// definition or a group of object declarations, actually parse the |
| 1192 | /// result. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1193 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1194 | unsigned Context, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1195 | bool AllowFunctionDefinitions, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1196 | SourceLocation *DeclEnd, |
| 1197 | ForRangeInit *FRI) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1198 | // Parse the first declarator. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1199 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1200 | ParseDeclarator(D); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1201 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1202 | // Bail out if the first declarator didn't seem well-formed. |
| 1203 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1204 | SkipMalformedDecl(); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1205 | return DeclGroupPtrTy(); |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1206 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1207 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1208 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1209 | // appropriate function scope after the function Decl has been constructed. |
| 1210 | LateParsedAttrList LateParsedAttrs; |
| 1211 | if (D.isFunctionDeclarator()) |
| 1212 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1213 | |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1214 | // Check to see if we have a function *definition* which must have a body. |
| 1215 | if (AllowFunctionDefinitions && D.isFunctionDeclarator() && |
| 1216 | // Look at the next token to make sure that this isn't a function |
| 1217 | // declaration. We have to check this because __attribute__ might be the |
| 1218 | // start of a function definition in GCC-extended K&R C. |
| 1219 | !isDeclarationAfterDeclarator()) { |
Richard Smith | 58196dc | 2011-11-30 23:45:35 +0000 | [diff] [blame] | 1220 | |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1221 | if (isStartOfFunctionDefinition(D)) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1222 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1223 | Diag(Tok, diag::err_function_declared_typedef); |
| 1224 | |
| 1225 | // Recover by treating the 'typedef' as spurious. |
| 1226 | DS.ClearStorageClassSpecs(); |
| 1227 | } |
| 1228 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1229 | Decl *TheDecl = |
| 1230 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1231 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | if (isDeclarationSpecifier()) { |
| 1235 | // If there is an invalid declaration specifier right after the function |
| 1236 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1237 | // actually a body. Just fall through into the code that handles it as a |
| 1238 | // prototype, and let the top-level code handle the erroneous declspec |
| 1239 | // where it would otherwise expect a comma or semicolon. |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1240 | } else { |
| 1241 | Diag(Tok, diag::err_expected_fn_body); |
| 1242 | SkipUntil(tok::semi); |
| 1243 | return DeclGroupPtrTy(); |
| 1244 | } |
| 1245 | } |
| 1246 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1247 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1248 | return DeclGroupPtrTy(); |
| 1249 | |
| 1250 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1251 | // must parse and analyze the for-range-initializer before the declaration is |
| 1252 | // analyzed. |
| 1253 | if (FRI && Tok.is(tok::colon)) { |
| 1254 | FRI->ColonLoc = ConsumeToken(); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1255 | if (Tok.is(tok::l_brace)) |
| 1256 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1257 | else |
| 1258 | FRI->RangeExpr = ParseExpression(); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1259 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1260 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
| 1261 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | 6895a64 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 1262 | D.complete(ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1263 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1); |
| 1264 | } |
| 1265 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1266 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1267 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1268 | if (LateParsedAttrs.size() > 0) |
| 1269 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1270 | D.complete(FirstDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1271 | if (FirstDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1272 | DeclsInGroup.push_back(FirstDecl); |
| 1273 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1274 | bool ExpectSemi = Context != Declarator::ForContext; |
| 1275 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1276 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1277 | // error, bail out. |
| 1278 | while (Tok.is(tok::comma)) { |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1279 | SourceLocation CommaLoc = ConsumeToken(); |
| 1280 | |
| 1281 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1282 | // This comma was followed by a line-break and something which can't be |
| 1283 | // the start of a declarator. The comma was probably a typo for a |
| 1284 | // semicolon. |
| 1285 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1286 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1287 | ExpectSemi = false; |
| 1288 | break; |
| 1289 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1290 | |
| 1291 | // Parse the next declarator. |
| 1292 | D.clear(); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1293 | D.setCommaLoc(CommaLoc); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1294 | |
| 1295 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1296 | // declaration, these would be part of the declspec. In subsequent |
| 1297 | // declarators, they become part of the declarator itself, so that they |
| 1298 | // don't apply to declarators after *this* one. Examples: |
| 1299 | // short __attribute__((common)) var; -> declspec |
| 1300 | // short var __attribute__((common)); -> declarator |
| 1301 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1302 | MaybeParseGNUAttributes(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1303 | |
| 1304 | ParseDeclarator(D); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1305 | if (!D.isInvalidType()) { |
| 1306 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1307 | D.complete(ThisDecl); |
| 1308 | if (ThisDecl) |
| 1309 | DeclsInGroup.push_back(ThisDecl); |
| 1310 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | if (DeclEnd) |
| 1314 | *DeclEnd = Tok.getLocation(); |
| 1315 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1316 | if (ExpectSemi && |
Chris Lattner | 8bb21d3 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 1317 | ExpectAndConsumeSemi(Context == Declarator::FileContext |
| 1318 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1319 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1320 | // Okay, there was no semicolon and one was expected. If we see a |
| 1321 | // declaration specifier, just assume it was missing and continue parsing. |
| 1322 | // Otherwise things are very confused and we skip to recover. |
| 1323 | if (!isDeclarationSpecifier()) { |
| 1324 | SkipUntil(tok::r_brace, true, true); |
| 1325 | if (Tok.is(tok::semi)) |
| 1326 | ConsumeToken(); |
| 1327 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1328 | } |
| 1329 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1330 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1331 | DeclsInGroup.data(), |
| 1332 | DeclsInGroup.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1333 | } |
| 1334 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1335 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1336 | /// declarator. Returns true on an error. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1337 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1338 | // If a simple-asm-expr is present, parse it. |
| 1339 | if (Tok.is(tok::kw_asm)) { |
| 1340 | SourceLocation Loc; |
| 1341 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1342 | if (AsmLabel.isInvalid()) { |
| 1343 | SkipUntil(tok::semi, true, true); |
| 1344 | return true; |
| 1345 | } |
| 1346 | |
| 1347 | D.setAsmLabel(AsmLabel.release()); |
| 1348 | D.SetRangeEnd(Loc); |
| 1349 | } |
| 1350 | |
| 1351 | MaybeParseGNUAttributes(D); |
| 1352 | return false; |
| 1353 | } |
| 1354 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1355 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1356 | /// declarator'. This method parses the remainder of the declaration |
| 1357 | /// (including any attributes or initializer, among other things) and |
| 1358 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1359 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1360 | /// init-declarator: [C99 6.7] |
| 1361 | /// declarator |
| 1362 | /// declarator '=' initializer |
| 1363 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1364 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1365 | /// [C++] declarator initializer[opt] |
| 1366 | /// |
| 1367 | /// [C++] initializer: |
| 1368 | /// [C++] '=' initializer-clause |
| 1369 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1370 | /// [C++0x] '=' 'default' [TODO] |
| 1371 | /// [C++0x] '=' 'delete' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1372 | /// [C++0x] braced-init-list |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1373 | /// |
| 1374 | /// According to the standard grammar, =default and =delete are function |
| 1375 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1376 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1377 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1378 | const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1379 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1380 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1381 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1382 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1383 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1384 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1385 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1386 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1387 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1388 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1389 | switch (TemplateInfo.Kind) { |
| 1390 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1391 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1392 | break; |
| 1393 | |
| 1394 | case ParsedTemplateInfo::Template: |
| 1395 | case ParsedTemplateInfo::ExplicitSpecialization: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1396 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1397 | MultiTemplateParamsArg(Actions, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1398 | TemplateInfo.TemplateParams->data(), |
| 1399 | TemplateInfo.TemplateParams->size()), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1400 | D); |
| 1401 | break; |
| 1402 | |
| 1403 | case ParsedTemplateInfo::ExplicitInstantiation: { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1404 | DeclResult ThisRes |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1405 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1406 | TemplateInfo.ExternLoc, |
| 1407 | TemplateInfo.TemplateLoc, |
| 1408 | D); |
| 1409 | if (ThisRes.isInvalid()) { |
| 1410 | SkipUntil(tok::semi, true, true); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1411 | return 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | ThisDecl = ThisRes.get(); |
| 1415 | break; |
| 1416 | } |
| 1417 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1419 | bool TypeContainsAuto = |
| 1420 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
| 1421 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1422 | // Parse declarator '=' initializer. |
Richard Trieu | d6c7c67 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1423 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | fcaf27e | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1424 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1425 | ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1426 | if (Tok.is(tok::kw_delete)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1427 | if (D.isFunctionDeclarator()) |
| 1428 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1429 | << 1 /* delete */; |
| 1430 | else |
| 1431 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Sean Hunt | fe2695e | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1432 | } else if (Tok.is(tok::kw_default)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1433 | if (D.isFunctionDeclarator()) |
Sebastian Redl | ecfcd56 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1434 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1435 | << 0 /* default */; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1436 | else |
| 1437 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1438 | } else { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1439 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1440 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1441 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1442 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1443 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1444 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1445 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1446 | cutOffParsing(); |
| 1447 | return 0; |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1450 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1451 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1452 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1453 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1454 | ExitScope(); |
| 1455 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1456 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1457 | if (Init.isInvalid()) { |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1458 | SkipUntil(tok::comma, true, true); |
| 1459 | Actions.ActOnInitializerError(ThisDecl); |
| 1460 | } else |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1461 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1462 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1463 | } |
| 1464 | } else if (Tok.is(tok::l_paren)) { |
| 1465 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1466 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1467 | T.consumeOpen(); |
| 1468 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1469 | ExprVector Exprs(Actions); |
| 1470 | CommaLocsTy CommaLocs; |
| 1471 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1472 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1473 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1474 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1477 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 1478 | SkipUntil(tok::r_paren); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1479 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1480 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1481 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1482 | ExitScope(); |
| 1483 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1484 | } else { |
| 1485 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1486 | T.consumeClose(); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1487 | |
| 1488 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1489 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1490 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1491 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1492 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1493 | ExitScope(); |
| 1494 | } |
| 1495 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1496 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 1497 | T.getCloseLocation(), |
| 1498 | move_arg(Exprs)); |
| 1499 | Actions.AddInitializerToDecl(ThisDecl, Initializer.take(), |
| 1500 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1501 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1502 | } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) { |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1503 | // Parse C++0x braced-init-list. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1504 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1505 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1506 | if (D.getCXXScopeSpec().isSet()) { |
| 1507 | EnterScope(0); |
| 1508 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1509 | } |
| 1510 | |
| 1511 | ExprResult Init(ParseBraceInitializer()); |
| 1512 | |
| 1513 | if (D.getCXXScopeSpec().isSet()) { |
| 1514 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1515 | ExitScope(); |
| 1516 | } |
| 1517 | |
| 1518 | if (Init.isInvalid()) { |
| 1519 | Actions.ActOnInitializerError(ThisDecl); |
| 1520 | } else |
| 1521 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1522 | /*DirectInit=*/true, TypeContainsAuto); |
| 1523 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1524 | } else { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1525 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1528 | Actions.FinalizeDeclaration(ThisDecl); |
| 1529 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1530 | return ThisDecl; |
| 1531 | } |
| 1532 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1533 | /// ParseSpecifierQualifierList |
| 1534 | /// specifier-qualifier-list: |
| 1535 | /// type-specifier specifier-qualifier-list[opt] |
| 1536 | /// type-qualifier specifier-qualifier-list[opt] |
| 1537 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 1538 | /// |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1539 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 1540 | DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1541 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1542 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1543 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1544 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1545 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1546 | // Validate declspec for type-name. |
| 1547 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 1548 | if ((DSC == DSC_type_specifier || DSC == DSC_trailing) && |
| 1549 | !DS.hasTypeSpecifier()) { |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1550 | Diag(Tok, diag::err_expected_type); |
| 1551 | DS.SetTypeSpecError(); |
| 1552 | } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 1553 | !DS.hasAttributes()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1554 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1555 | if (!DS.hasTypeSpecifier()) |
| 1556 | DS.SetTypeSpecError(); |
| 1557 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1558 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1559 | // Issue diagnostic and remove storage class if present. |
| 1560 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 1561 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1562 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 1563 | else |
| 1564 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
| 1565 | DS.ClearStorageClassSpecs(); |
| 1566 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1567 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1568 | // Issue diagnostic and remove function specfier if present. |
| 1569 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1570 | if (DS.isInlineSpecified()) |
| 1571 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1572 | if (DS.isVirtualSpecified()) |
| 1573 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1574 | if (DS.isExplicitSpecified()) |
| 1575 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1576 | DS.ClearFunctionSpecs(); |
| 1577 | } |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1578 | |
| 1579 | // Issue diagnostic and remove constexpr specfier if present. |
| 1580 | if (DS.isConstexprSpecified()) { |
| 1581 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 1582 | DS.ClearConstexprSpec(); |
| 1583 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1584 | } |
| 1585 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1586 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 1587 | /// specified token is valid after the identifier in a declarator which |
| 1588 | /// immediately follows the declspec. For example, these things are valid: |
| 1589 | /// |
| 1590 | /// int x [ 4]; // direct-declarator |
| 1591 | /// int x ( int y); // direct-declarator |
| 1592 | /// int(int x ) // direct-declarator |
| 1593 | /// int x ; // simple-declaration |
| 1594 | /// int x = 17; // init-declarator-list |
| 1595 | /// int x , y; // init-declarator-list |
| 1596 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1597 | /// int x : 4; // struct-declarator |
Chris Lattner | c83c27a | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 1598 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1599 | /// |
| 1600 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 1601 | /// ')' happens to be valid anyway). |
| 1602 | /// int (x) |
| 1603 | /// |
| 1604 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 1605 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 1606 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1607 | 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] | 1608 | } |
| 1609 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1610 | |
| 1611 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 1612 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 1613 | /// the declspec has no type specifier. In this case, the declspec is either |
| 1614 | /// malformed or is "implicit int" (in K&R and C89). |
| 1615 | /// |
| 1616 | /// This method handles diagnosing this prettily and returns false if the |
| 1617 | /// declspec is done being processed. If it recovers and thinks there may be |
| 1618 | /// other pieces of declspec after it, it returns true. |
| 1619 | /// |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1620 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1621 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1622 | AccessSpecifier AS, DeclSpecContext DSC) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1623 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1624 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1625 | SourceLocation Loc = Tok.getLocation(); |
| 1626 | // If we see an identifier that is not a type name, we normally would |
| 1627 | // parse it as the identifer being declared. However, when a typename |
| 1628 | // is typo'd or the definition is not included, this will incorrectly |
| 1629 | // parse the typename as the identifier name and fall over misparsing |
| 1630 | // later parts of the diagnostic. |
| 1631 | // |
| 1632 | // As such, we try to do some look-ahead in cases where this would |
| 1633 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 1634 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 1635 | // an identifier with implicit int, we'd get a parse error because the |
| 1636 | // next token is obviously invalid for a type. Parse these as a case |
| 1637 | // with an invalid type specifier. |
| 1638 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1639 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1640 | // Since we know that this either implicit int (which is rare) or an |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 1641 | // error, do lookahead to try to do better recovery. This never applies |
| 1642 | // within a type specifier. Outside of C++, we allow this even if the |
| 1643 | // language doesn't "officially" support implicit int -- we support |
| 1644 | // implicit int as an extension in C99 and C11. Allegedly, MS also |
| 1645 | // supports implicit int in C++ mode. |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 1646 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 1647 | (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) && |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1648 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1649 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 1650 | // we just avoid eating the identifier, so it will be parsed as the |
| 1651 | // identifier in the declarator. |
| 1652 | return false; |
| 1653 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1654 | |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 1655 | if (getLangOpts().CPlusPlus && |
| 1656 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 1657 | // Don't require a type specifier if we have the 'auto' storage class |
| 1658 | // specifier in C++98 -- we'll promote it to a type specifier. |
| 1659 | return false; |
| 1660 | } |
| 1661 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1662 | // Otherwise, if we don't consume this token, we are going to emit an |
| 1663 | // error anyway. Try to recover from various common problems. Check |
| 1664 | // to see if this was a reference to a tag name without a tag specified. |
| 1665 | // 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] | 1666 | // |
| 1667 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 1668 | if (SS == 0) { |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1669 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1670 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1671 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1672 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1673 | default: break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1674 | case DeclSpec::TST_enum: |
| 1675 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 1676 | case DeclSpec::TST_union: |
| 1677 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 1678 | case DeclSpec::TST_struct: |
| 1679 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
| 1680 | case DeclSpec::TST_class: |
| 1681 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1682 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1683 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1684 | if (TagName) { |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 1685 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 1686 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 1687 | Sema::LookupOrdinaryName); |
| 1688 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1689 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 1690 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 1691 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 1692 | |
| 1693 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 1694 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 1695 | I != IEnd; ++I) |
Kaelyn Uhrain | 392b3f5 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 1696 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 1697 | << TokenName << TagName; |
| 1698 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1699 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1700 | // Parse this as a tag as if the missing tag were present. |
| 1701 | if (TagKind == tok::kw_enum) |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1702 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1703 | else |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1704 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
| 1705 | /*EnteringContext*/ false, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1706 | return true; |
| 1707 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1708 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1709 | |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame^] | 1710 | // Determine whether this identifier could plausibly be the name of something |
| 1711 | // being declared (with a missign type). |
| 1712 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
| 1713 | (!SS || DSC == DSC_top_level || DSC == DSC_class)) { |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 1714 | // Look ahead to the next token to try to figure out what this declaration |
| 1715 | // was supposed to be. |
| 1716 | switch (NextToken().getKind()) { |
| 1717 | case tok::comma: |
| 1718 | case tok::equal: |
| 1719 | case tok::kw_asm: |
| 1720 | case tok::l_brace: |
| 1721 | case tok::l_square: |
| 1722 | case tok::semi: |
| 1723 | // This looks like a variable declaration. The type is probably missing. |
| 1724 | // We're done parsing decl-specifiers. |
| 1725 | return false; |
| 1726 | |
| 1727 | case tok::l_paren: { |
| 1728 | // static x(4); // 'x' is not a type |
| 1729 | // x(int n); // 'x' is not a type |
| 1730 | // x (*p)[]; // 'x' is a type |
| 1731 | // |
| 1732 | // Since we're in an error case (or the rare 'implicit int in C++' MS |
| 1733 | // extension), we can afford to perform a tentative parse to determine |
| 1734 | // which case we're in. |
| 1735 | TentativeParsingAction PA(*this); |
| 1736 | ConsumeToken(); |
| 1737 | TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false); |
| 1738 | PA.Revert(); |
| 1739 | if (TPR == TPResult::False()) |
| 1740 | return false; |
| 1741 | // The identifier is followed by a parenthesized declarator. |
| 1742 | // It's supposed to be a type. |
| 1743 | break; |
| 1744 | } |
| 1745 | |
| 1746 | default: |
| 1747 | // This is probably supposed to be a type. This includes cases like: |
| 1748 | // int f(itn); |
| 1749 | // struct S { unsinged : 4; }; |
| 1750 | break; |
| 1751 | } |
| 1752 | } |
| 1753 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1754 | // This is almost certainly an invalid type name. Let the action emit a |
| 1755 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1756 | ParsedType T; |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1757 | if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc, |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1758 | getCurScope(), SS, T)) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1759 | // The action emitted a diagnostic, so we don't have to. |
| 1760 | if (T) { |
| 1761 | // The action has suggested that the type T could be used. Set that as |
| 1762 | // the type in the declaration specifiers, consume the would-be type |
| 1763 | // name token, and we're done. |
| 1764 | const char *PrevSpec; |
| 1765 | unsigned DiagID; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1766 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1767 | DS.SetRangeEnd(Tok.getLocation()); |
| 1768 | ConsumeToken(); |
| 1769 | |
| 1770 | // There may be other declaration specifiers after this. |
| 1771 | return true; |
| 1772 | } |
| 1773 | |
| 1774 | // Fall through; the action had no suggestion for us. |
| 1775 | } else { |
| 1776 | // The action did not emit a diagnostic, so emit one now. |
| 1777 | SourceRange R; |
| 1778 | if (SS) R = SS->getRange(); |
| 1779 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 1780 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1781 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1782 | // Mark this as an error. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1783 | DS.SetTypeSpecError(); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1784 | DS.SetRangeEnd(Tok.getLocation()); |
| 1785 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1786 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1787 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 1788 | // avoid rippling error messages on subsequent uses of the same type, |
| 1789 | // could be useful if #include was forgotten. |
| 1790 | return false; |
| 1791 | } |
| 1792 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1793 | /// \brief Determine the declaration specifier context from the declarator |
| 1794 | /// context. |
| 1795 | /// |
| 1796 | /// \param Context the declarator context, which is one of the |
| 1797 | /// Declarator::TheContext enumerator values. |
| 1798 | Parser::DeclSpecContext |
| 1799 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 1800 | if (Context == Declarator::MemberContext) |
| 1801 | return DSC_class; |
| 1802 | if (Context == Declarator::FileContext) |
| 1803 | return DSC_top_level; |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 1804 | if (Context == Declarator::TrailingReturnContext) |
| 1805 | return DSC_trailing; |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1806 | return DSC_normal; |
| 1807 | } |
| 1808 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1809 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 1810 | /// |
| 1811 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 1812 | /// type. Ideally, the type should be propagated directly into Sema. |
| 1813 | /// |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1814 | /// [C11] type-id |
| 1815 | /// [C11] constant-expression |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1816 | /// [C++0x] type-id ...[opt] |
| 1817 | /// [C++0x] assignment-expression ...[opt] |
| 1818 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 1819 | SourceLocation &EllipsisLoc) { |
| 1820 | ExprResult ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1821 | if (isTypeIdInParens()) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1822 | SourceLocation TypeLoc = Tok.getLocation(); |
| 1823 | ParsedType Ty = ParseTypeName().get(); |
| 1824 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1825 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 1826 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1827 | } else |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1828 | ER = ParseConstantExpression(); |
| 1829 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1830 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis)) |
Peter Collingbourne | fe9b2a8 | 2011-10-24 17:56:00 +0000 | [diff] [blame] | 1831 | EllipsisLoc = ConsumeToken(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1832 | |
| 1833 | return ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 1837 | /// attribute to Attrs. |
| 1838 | /// |
| 1839 | /// alignment-specifier: |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1840 | /// [C11] '_Alignas' '(' type-id ')' |
| 1841 | /// [C11] '_Alignas' '(' constant-expression ')' |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1842 | /// [C++0x] 'alignas' '(' type-id ...[opt] ')' |
| 1843 | /// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1844 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
| 1845 | SourceLocation *endLoc) { |
| 1846 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 1847 | "Not an alignment-specifier!"); |
| 1848 | |
| 1849 | SourceLocation KWLoc = Tok.getLocation(); |
| 1850 | ConsumeToken(); |
| 1851 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1852 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1853 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1854 | return; |
| 1855 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1856 | SourceLocation EllipsisLoc; |
| 1857 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1858 | if (ArgExpr.isInvalid()) { |
| 1859 | SkipUntil(tok::r_paren); |
| 1860 | return; |
| 1861 | } |
| 1862 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1863 | T.consumeClose(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1864 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1865 | *endLoc = T.getCloseLocation(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1866 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1867 | // FIXME: Handle pack-expansions here. |
| 1868 | if (EllipsisLoc.isValid()) { |
| 1869 | Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported); |
| 1870 | return; |
| 1871 | } |
| 1872 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1873 | ExprVector ArgExprs(Actions); |
| 1874 | ArgExprs.push_back(ArgExpr.release()); |
| 1875 | Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1876 | 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1879 | /// ParseDeclarationSpecifiers |
| 1880 | /// declaration-specifiers: [C99 6.7] |
| 1881 | /// storage-class-specifier declaration-specifiers[opt] |
| 1882 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1883 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1884 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1885 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1886 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1887 | /// |
| 1888 | /// storage-class-specifier: [C99 6.7.1] |
| 1889 | /// 'typedef' |
| 1890 | /// 'extern' |
| 1891 | /// 'static' |
| 1892 | /// 'auto' |
| 1893 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1894 | /// [C++] 'mutable' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1895 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1896 | /// function-specifier: [C99 6.7.4] |
| 1897 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1898 | /// [C++] 'virtual' |
| 1899 | /// [C++] 'explicit' |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1900 | /// [OpenCL] '__kernel' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1901 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 1902 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1903 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1904 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1905 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1906 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1907 | AccessSpecifier AS, |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1908 | DeclSpecContext DSContext, |
| 1909 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1910 | if (DS.getSourceRange().isInvalid()) { |
| 1911 | DS.SetRangeStart(Tok.getLocation()); |
| 1912 | DS.SetRangeEnd(Tok.getLocation()); |
| 1913 | } |
| 1914 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 1915 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1916 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1917 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1918 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1919 | unsigned DiagID = 0; |
| 1920 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1921 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1922 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1923 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1924 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 1925 | DoneWithDeclSpec: |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 1926 | // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt] |
| 1927 | MaybeParseCXX0XAttributes(DS.getAttributes()); |
| 1928 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1929 | // If this is not a declaration specifier token, we're done reading decl |
| 1930 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 1931 | DS.Finish(Diags, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1932 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1933 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1934 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1935 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1936 | if (DS.hasTypeSpecifier()) { |
| 1937 | bool AllowNonIdentifiers |
| 1938 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 1939 | Scope::BlockScope | |
| 1940 | Scope::TemplateParamScope | |
| 1941 | Scope::FunctionPrototypeScope | |
| 1942 | Scope::AtCatchScope)) == 0; |
| 1943 | bool AllowNestedNameSpecifiers |
| 1944 | = DSContext == DSC_top_level || |
| 1945 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 1946 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 1947 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
| 1948 | AllowNonIdentifiers, |
| 1949 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1950 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1951 | } |
| 1952 | |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1953 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 1954 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 1955 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1956 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
| 1957 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1958 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1959 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1960 | else if (CurParsedObjCImpl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1961 | CCC = Sema::PCC_ObjCImplementation; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1962 | |
| 1963 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1964 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1965 | } |
| 1966 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 1967 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1968 | // C++ scope specifier. Annotate and loop, or bail out on error. |
| 1969 | if (TryAnnotateCXXScopeToken(true)) { |
| 1970 | if (!DS.hasTypeSpecifier()) |
| 1971 | DS.SetTypeSpecError(); |
| 1972 | goto DoneWithDeclSpec; |
| 1973 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 1974 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 1975 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1976 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1977 | |
| 1978 | case tok::annot_cxxscope: { |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 1979 | if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector()) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1980 | goto DoneWithDeclSpec; |
| 1981 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1982 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 1983 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 1984 | Tok.getAnnotationRange(), |
| 1985 | SS); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1986 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1987 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1988 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1989 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1990 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1991 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1992 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1993 | |
| 1994 | // C++ [class.qual]p2: |
| 1995 | // In a lookup in which the constructor is an acceptable lookup |
| 1996 | // result and the nested-name-specifier nominates a class C: |
| 1997 | // |
| 1998 | // - if the name specified after the |
| 1999 | // nested-name-specifier, when looked up in C, is the |
| 2000 | // injected-class-name of C (Clause 9), or |
| 2001 | // |
| 2002 | // - if the name specified after the nested-name-specifier |
| 2003 | // is the same as the identifier or the |
| 2004 | // simple-template-id's template-name in the last |
| 2005 | // component of the nested-name-specifier, |
| 2006 | // |
| 2007 | // the name is instead considered to name the constructor of |
| 2008 | // class C. |
| 2009 | // |
| 2010 | // Thus, if the template-name is actually the constructor |
| 2011 | // name, then the code is ill-formed; this interpretation is |
| 2012 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2013 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2014 | if ((DSContext == DSC_top_level || |
| 2015 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
| 2016 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2017 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2018 | if (isConstructorDeclarator()) { |
| 2019 | // The user meant this to be an out-of-line constructor |
| 2020 | // definition, but template arguments are not allowed |
| 2021 | // there. Just allow this as a constructor; we'll |
| 2022 | // complain about it later. |
| 2023 | goto DoneWithDeclSpec; |
| 2024 | } |
| 2025 | |
| 2026 | // The user meant this to name a type, but it actually names |
| 2027 | // a constructor with some extraneous template |
| 2028 | // arguments. Complain, then parse it as a type as the user |
| 2029 | // intended. |
| 2030 | Diag(TemplateId->TemplateNameLoc, |
| 2031 | diag::err_out_of_line_template_id_names_constructor) |
| 2032 | << TemplateId->Name; |
| 2033 | } |
| 2034 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2035 | DS.getTypeSpecScope() = SS; |
| 2036 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2037 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2038 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2039 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2040 | continue; |
| 2041 | } |
| 2042 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2043 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2044 | DS.getTypeSpecScope() = SS; |
| 2045 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2046 | if (Tok.getAnnotationValue()) { |
| 2047 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2048 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
| 2049 | Tok.getAnnotationEndLoc(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2050 | PrevSpec, DiagID, T); |
| 2051 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2052 | else |
| 2053 | DS.SetTypeSpecError(); |
| 2054 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2055 | ConsumeToken(); // The typename |
| 2056 | } |
| 2057 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2058 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2059 | goto DoneWithDeclSpec; |
| 2060 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2061 | // If we're in a context where the identifier could be a class name, |
| 2062 | // check whether this is a constructor declaration. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2063 | if ((DSContext == DSC_top_level || |
| 2064 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2065 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2066 | &SS)) { |
| 2067 | if (isConstructorDeclarator()) |
| 2068 | goto DoneWithDeclSpec; |
| 2069 | |
| 2070 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 2071 | // of the class is qualified in a context where it could name |
| 2072 | // a constructor, its a constructor name. However, we've |
| 2073 | // looked at the declarator, and the user probably meant this |
| 2074 | // to be a type. Complain that it isn't supposed to be treated |
| 2075 | // as a type, then proceed to parse it as a type. |
| 2076 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 2077 | << Next.getIdentifierInfo(); |
| 2078 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2079 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2080 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 2081 | Next.getLocation(), |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2082 | getCurScope(), &SS, |
| 2083 | false, false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2084 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2085 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2086 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2087 | // If the referenced identifier is not a type, then this declspec is |
| 2088 | // erroneous: We already checked about that it has no type specifier, and |
| 2089 | // 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] | 2090 | // typename. |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2091 | if (TypeRep == 0) { |
| 2092 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2093 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2094 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2095 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2096 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2097 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2098 | ConsumeToken(); // The C++ scope. |
| 2099 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2100 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2101 | DiagID, TypeRep); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2102 | if (isInvalid) |
| 2103 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2104 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2105 | DS.SetRangeEnd(Tok.getLocation()); |
| 2106 | ConsumeToken(); // The typename. |
| 2107 | |
| 2108 | continue; |
| 2109 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2110 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2111 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2112 | if (Tok.getAnnotationValue()) { |
| 2113 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | c43271e | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 2114 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2115 | DiagID, T); |
| 2116 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2117 | DS.SetTypeSpecError(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 2118 | |
| 2119 | if (isInvalid) |
| 2120 | break; |
| 2121 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2122 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2123 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2124 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2125 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2126 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2127 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2128 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2129 | ParseObjCProtocolQualifiers(DS); |
| 2130 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2131 | continue; |
| 2132 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2133 | |
Douglas Gregor | bfad915 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2134 | case tok::kw___is_signed: |
| 2135 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 2136 | // typically treats it as a trait. If we see __is_signed as it appears |
| 2137 | // in libstdc++, e.g., |
| 2138 | // |
| 2139 | // static const bool __is_signed; |
| 2140 | // |
| 2141 | // then treat __is_signed as an identifier rather than as a keyword. |
| 2142 | if (DS.getTypeSpecType() == TST_bool && |
| 2143 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
| 2144 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 2145 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
| 2146 | Tok.setKind(tok::identifier); |
| 2147 | } |
| 2148 | |
| 2149 | // We're done with the declaration-specifiers. |
| 2150 | goto DoneWithDeclSpec; |
| 2151 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2152 | // typedef-name |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2153 | case tok::kw_decltype: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2154 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2155 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 2156 | // so handle it as such. This is important for ctor parsing. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2157 | if (getLangOpts().CPlusPlus) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2158 | if (TryAnnotateCXXScopeToken(true)) { |
| 2159 | if (!DS.hasTypeSpecifier()) |
| 2160 | DS.SetTypeSpecError(); |
| 2161 | goto DoneWithDeclSpec; |
| 2162 | } |
| 2163 | if (!Tok.is(tok::identifier)) |
| 2164 | continue; |
| 2165 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2166 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2167 | // This identifier can only be a typedef name if we haven't already seen |
| 2168 | // a type-specifier. Without this check we misparse: |
| 2169 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 2170 | if (DS.hasTypeSpecifier()) |
| 2171 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2172 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2173 | // Check for need to substitute AltiVec keyword tokens. |
| 2174 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2175 | break; |
| 2176 | |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2177 | // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not |
| 2178 | // allow the use of a typedef name as a type specifier. |
| 2179 | if (DS.isTypeAltiVecVector()) |
| 2180 | goto DoneWithDeclSpec; |
| 2181 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2182 | ParsedType TypeRep = |
| 2183 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 2184 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2185 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2186 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 2187 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2188 | if (!TypeRep) { |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2189 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2190 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2191 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2192 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2193 | // If we're in a context where the identifier could be a class name, |
| 2194 | // check whether this is a constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2195 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2196 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2197 | isConstructorDeclarator()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2198 | goto DoneWithDeclSpec; |
| 2199 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2200 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2201 | DiagID, TypeRep); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2202 | if (isInvalid) |
| 2203 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2204 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2205 | DS.SetRangeEnd(Tok.getLocation()); |
| 2206 | ConsumeToken(); // The identifier |
| 2207 | |
| 2208 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2209 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2210 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2211 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2212 | ParseObjCProtocolQualifiers(DS); |
| 2213 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2214 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2215 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2216 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2217 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2218 | |
| 2219 | // type-name |
| 2220 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2221 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2222 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2223 | // This template-id does not refer to a type name, so we're |
| 2224 | // done with the type-specifiers. |
| 2225 | goto DoneWithDeclSpec; |
| 2226 | } |
| 2227 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2228 | // If we're in a context where the template-id could be a |
| 2229 | // constructor name or specialization, check whether this is a |
| 2230 | // constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2231 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2232 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2233 | isConstructorDeclarator()) |
| 2234 | goto DoneWithDeclSpec; |
| 2235 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2236 | // Turn the template-id annotation token into a type annotation |
| 2237 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2238 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2239 | continue; |
| 2240 | } |
| 2241 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2242 | // GNU attributes support. |
| 2243 | case tok::kw___attribute: |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2244 | ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2245 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2246 | |
| 2247 | // Microsoft declspec support. |
| 2248 | case tok::kw___declspec: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2249 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2250 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2251 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2252 | // Microsoft single token adornments. |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2253 | case tok::kw___forceinline: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2254 | // FIXME: Add handling here! |
| 2255 | break; |
| 2256 | |
| 2257 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2258 | case tok::kw___ptr32: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2259 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2260 | case tok::kw___cdecl: |
| 2261 | case tok::kw___stdcall: |
| 2262 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2263 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2264 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2265 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2266 | continue; |
| 2267 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2268 | // Borland single token adornments. |
| 2269 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2270 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2271 | continue; |
| 2272 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2273 | // OpenCL single token adornments. |
| 2274 | case tok::kw___kernel: |
| 2275 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2276 | continue; |
| 2277 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2278 | // storage-class-specifier |
| 2279 | case tok::kw_typedef: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2280 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
| 2281 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2282 | break; |
| 2283 | case tok::kw_extern: |
| 2284 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2285 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2286 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
| 2287 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2288 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2289 | case tok::kw___private_extern__: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2290 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
| 2291 | Loc, PrevSpec, DiagID); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2292 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2293 | case tok::kw_static: |
| 2294 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2295 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2296 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
| 2297 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2298 | break; |
| 2299 | case tok::kw_auto: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2300 | if (getLangOpts().CPlusPlus0x) { |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2301 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2302 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2303 | PrevSpec, DiagID); |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2304 | if (!isInvalid) |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2305 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2306 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2307 | } else |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2308 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 2309 | DiagID); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2310 | } else |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2311 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2312 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2313 | break; |
| 2314 | case tok::kw_register: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2315 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
| 2316 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2317 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2318 | case tok::kw_mutable: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2319 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
| 2320 | PrevSpec, DiagID); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2321 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2322 | case tok::kw___thread: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2323 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2324 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2325 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2326 | // function-specifier |
| 2327 | case tok::kw_inline: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2328 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2329 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2330 | case tok::kw_virtual: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2331 | isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2332 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2333 | case tok::kw_explicit: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2334 | isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2335 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2336 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2337 | // alignment-specifier |
| 2338 | case tok::kw__Alignas: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2339 | if (!getLangOpts().C11) |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2340 | Diag(Tok, diag::ext_c11_alignas); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2341 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2342 | continue; |
| 2343 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2344 | // friend |
| 2345 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2346 | if (DSContext == DSC_class) |
| 2347 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 2348 | else { |
| 2349 | PrevSpec = ""; // not actually used by the diagnostic |
| 2350 | DiagID = diag::err_friend_invalid_in_context; |
| 2351 | isInvalid = true; |
| 2352 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2353 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2354 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2355 | // Modules |
| 2356 | case tok::kw___module_private__: |
| 2357 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 2358 | break; |
| 2359 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2360 | // constexpr |
| 2361 | case tok::kw_constexpr: |
| 2362 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 2363 | break; |
| 2364 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2365 | // type-specifier |
| 2366 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2367 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 2368 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2369 | break; |
| 2370 | case tok::kw_long: |
| 2371 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2372 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2373 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2374 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2375 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2376 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2377 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2378 | case tok::kw___int64: |
| 2379 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2380 | DiagID); |
| 2381 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2382 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2383 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 2384 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2385 | break; |
| 2386 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2387 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2388 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2389 | break; |
| 2390 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2391 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2392 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2393 | break; |
| 2394 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2395 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2396 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2397 | break; |
| 2398 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2399 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 2400 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2401 | break; |
| 2402 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2403 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 2404 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2405 | break; |
| 2406 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2407 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 2408 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2409 | break; |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 2410 | case tok::kw___int128: |
| 2411 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
| 2412 | DiagID); |
| 2413 | break; |
| 2414 | case tok::kw_half: |
| 2415 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
| 2416 | DiagID); |
| 2417 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2418 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2419 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 2420 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2421 | break; |
| 2422 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2423 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 2424 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2425 | break; |
| 2426 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2427 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 2428 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2429 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2430 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2431 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 2432 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2433 | break; |
| 2434 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2435 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 2436 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2437 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2438 | case tok::kw_bool: |
| 2439 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2440 | if (Tok.is(tok::kw_bool) && |
| 2441 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 2442 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 2443 | PrevSpec = ""; // Not used by the diagnostic. |
| 2444 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2445 | // For better error recovery. |
| 2446 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2447 | isInvalid = true; |
| 2448 | } else { |
| 2449 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 2450 | DiagID); |
| 2451 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2452 | break; |
| 2453 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2454 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2455 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2456 | break; |
| 2457 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2458 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2459 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2460 | break; |
| 2461 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2462 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2463 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2464 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2465 | case tok::kw___vector: |
| 2466 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2467 | break; |
| 2468 | case tok::kw___pixel: |
| 2469 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2470 | break; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 2471 | case tok::kw___unknown_anytype: |
| 2472 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 2473 | PrevSpec, DiagID); |
| 2474 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2475 | |
| 2476 | // class-specifier: |
| 2477 | case tok::kw_class: |
| 2478 | case tok::kw_struct: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2479 | case tok::kw_union: { |
| 2480 | tok::TokenKind Kind = Tok.getKind(); |
| 2481 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2482 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
| 2483 | EnteringContext, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2484 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2485 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2486 | |
| 2487 | // enum-specifier: |
| 2488 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2489 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2490 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2491 | continue; |
| 2492 | |
| 2493 | // cv-qualifier: |
| 2494 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2495 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2496 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2497 | break; |
| 2498 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2499 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2500 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2501 | break; |
| 2502 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2503 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2504 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2505 | break; |
| 2506 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2507 | // C++ typename-specifier: |
| 2508 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2509 | if (TryAnnotateTypeOrScopeToken()) { |
| 2510 | DS.SetTypeSpecError(); |
| 2511 | goto DoneWithDeclSpec; |
| 2512 | } |
| 2513 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2514 | continue; |
| 2515 | break; |
| 2516 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2517 | // GNU typeof support. |
| 2518 | case tok::kw_typeof: |
| 2519 | ParseTypeofSpecifier(DS); |
| 2520 | continue; |
| 2521 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2522 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2523 | ParseDecltypeSpecifier(DS); |
| 2524 | continue; |
| 2525 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2526 | case tok::kw___underlying_type: |
| 2527 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2528 | continue; |
| 2529 | |
| 2530 | case tok::kw__Atomic: |
| 2531 | ParseAtomicSpecifier(DS); |
| 2532 | continue; |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2533 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2534 | // OpenCL qualifiers: |
| 2535 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2536 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2537 | goto DoneWithDeclSpec; |
| 2538 | case tok::kw___private: |
| 2539 | case tok::kw___global: |
| 2540 | case tok::kw___local: |
| 2541 | case tok::kw___constant: |
| 2542 | case tok::kw___read_only: |
| 2543 | case tok::kw___write_only: |
| 2544 | case tok::kw___read_write: |
| 2545 | ParseOpenCLQualifiers(DS); |
| 2546 | break; |
| 2547 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 2548 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2549 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2550 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 2551 | // but we support it. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2552 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2553 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2554 | |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 2555 | if (!ParseObjCProtocolQualifiers(DS)) |
| 2556 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 2557 | << FixItHint::CreateInsertion(Loc, "id") |
| 2558 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2559 | |
| 2560 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2561 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2562 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2563 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2564 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2565 | if (isInvalid) { |
| 2566 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2567 | assert(DiagID); |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 2568 | |
| 2569 | if (DiagID == diag::ext_duplicate_declspec) |
| 2570 | Diag(Tok, DiagID) |
| 2571 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 2572 | else |
| 2573 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2574 | } |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2575 | |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 2576 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2577 | if (DiagID != diag::err_bool_redeclaration) |
| 2578 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2579 | } |
| 2580 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 2581 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2582 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 2583 | /// semicolon. |
| 2584 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2585 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2586 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2587 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2588 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2589 | /// struct-declarator-list: |
| 2590 | /// struct-declarator |
| 2591 | /// struct-declarator-list ',' struct-declarator |
| 2592 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 2593 | /// struct-declarator: |
| 2594 | /// declarator |
| 2595 | /// [GNU] declarator attributes[opt] |
| 2596 | /// declarator[opt] ':' constant-expression |
| 2597 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 2598 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2599 | void Parser:: |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2600 | ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 2601 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2602 | if (Tok.is(tok::kw___extension__)) { |
| 2603 | // __extension__ silences extension warnings in the subexpression. |
| 2604 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2605 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2606 | return ParseStructDeclaration(DS, Fields); |
| 2607 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2608 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2609 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2610 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2611 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 2612 | // If there are no declarators, this is a free-standing declaration |
| 2613 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2614 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2615 | Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2616 | return; |
| 2617 | } |
| 2618 | |
| 2619 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2620 | bool FirstDeclarator = true; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2621 | SourceLocation CommaLoc; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2622 | while (1) { |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 2623 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2624 | FieldDeclarator DeclaratorInfo(DS); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2625 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2626 | |
| 2627 | // Attributes are only allowed here on successive declarators. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2628 | if (!FirstDeclarator) |
| 2629 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2630 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2631 | /// struct-declarator: declarator |
| 2632 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2633 | if (Tok.isNot(tok::colon)) { |
| 2634 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 2635 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2636 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2637 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2638 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2639 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2640 | ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2641 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2642 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2643 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 2644 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2645 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2646 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2647 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2648 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2649 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2650 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2651 | // We're done with this declarator; invoke the callback. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2652 | Decl *D = Fields.invoke(DeclaratorInfo); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2653 | PD.complete(D); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2654 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2655 | // If we don't have a comma, it is either the end of the list (a ';') |
| 2656 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2657 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2658 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2659 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2660 | // Consume the comma. |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2661 | CommaLoc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2662 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2663 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2664 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2665 | } |
| 2666 | |
| 2667 | /// ParseStructUnionBody |
| 2668 | /// struct-contents: |
| 2669 | /// struct-declaration-list |
| 2670 | /// [EXT] empty |
| 2671 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 2672 | /// struct-declaration-list: |
| 2673 | /// struct-declaration |
| 2674 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2675 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2676 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2677 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2678 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2679 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 2680 | "parsing struct/union body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2681 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2682 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 2683 | if (T.consumeOpen()) |
| 2684 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2685 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 2686 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2687 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2688 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2689 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 2690 | // C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2691 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) { |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 2692 | Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union); |
| 2693 | Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union); |
| 2694 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2695 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2696 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2697 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2698 | // 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] | 2699 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2700 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2701 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2702 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2703 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2704 | Diag(Tok, diag::ext_extra_struct_semi) |
Douglas Gregor | f13ca06 | 2010-06-16 23:08:59 +0000 | [diff] [blame] | 2705 | << DeclSpec::getSpecifierName((DeclSpec::TST)TagType) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2706 | << FixItHint::CreateRemoval(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2707 | ConsumeToken(); |
| 2708 | continue; |
| 2709 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2710 | |
| 2711 | // Parse all the comma separated declarators. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2712 | DeclSpec DS(AttrFactory); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2713 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2714 | if (!Tok.is(tok::at)) { |
| 2715 | struct CFieldCallback : FieldCallback { |
| 2716 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2717 | Decl *TagDecl; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2718 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2719 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2720 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2721 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2722 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 2723 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2724 | virtual Decl *invoke(FieldDeclarator &FD) { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2725 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2726 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 2727 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 2728 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2729 | FieldDecls.push_back(Field); |
| 2730 | return Field; |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 2731 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2732 | } Callback(*this, TagDecl, FieldDecls); |
| 2733 | |
| 2734 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2735 | } else { // Handle @defs |
| 2736 | ConsumeToken(); |
| 2737 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 2738 | Diag(Tok, diag::err_unexpected_at); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2739 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2740 | continue; |
| 2741 | } |
| 2742 | ConsumeToken(); |
| 2743 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 2744 | if (!Tok.is(tok::identifier)) { |
| 2745 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2746 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2747 | continue; |
| 2748 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2749 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2750 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2751 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2752 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 2753 | ConsumeToken(); |
| 2754 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2755 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2756 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2757 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2758 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2759 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2760 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2761 | break; |
| 2762 | } else { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2763 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 2764 | // 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] | 2765 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2766 | // If we stopped at a ';', eat it. |
| 2767 | if (Tok.is(tok::semi)) ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2768 | } |
| 2769 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2770 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2771 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2772 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2773 | ParsedAttributes attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2774 | // If attributes exist after struct contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2775 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 2776 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2777 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 2778 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2779 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2780 | attrs.getList()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2781 | StructScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2782 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 2783 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2784 | } |
| 2785 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2786 | /// ParseEnumSpecifier |
| 2787 | /// enum-specifier: [C99 6.7.2.2] |
| 2788 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2789 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2790 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 2791 | /// '}' attributes[opt] |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 2792 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 2793 | /// '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2794 | /// 'enum' identifier |
| 2795 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2796 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2797 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 2798 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2799 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2800 | /// enum-head: [C++11] |
| 2801 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 2802 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 2803 | /// identifier enum-base[opt] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2804 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2805 | /// enum-key: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2806 | /// 'enum' |
| 2807 | /// 'enum' 'class' |
| 2808 | /// 'enum' 'struct' |
| 2809 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2810 | /// enum-base: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2811 | /// ':' type-specifier-seq |
| 2812 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2813 | /// [C++] elaborated-type-specifier: |
| 2814 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 2815 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2816 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 2817 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2818 | AccessSpecifier AS, DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2819 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2820 | if (Tok.is(tok::code_completion)) { |
| 2821 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2822 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2823 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2824 | } |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2825 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2826 | SourceLocation ScopedEnumKWLoc; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2827 | bool IsScopedUsingClassTag = false; |
| 2828 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2829 | if (getLangOpts().CPlusPlus0x && |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2830 | (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2831 | Diag(Tok, diag::warn_cxx98_compat_scoped_enum); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2832 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2833 | ScopedEnumKWLoc = ConsumeToken(); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2834 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2835 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 2836 | // C++11 [temp.explicit]p12: |
| 2837 | // The usual access controls do not apply to names used to specify |
| 2838 | // explicit instantiations. |
| 2839 | // We extend this to also cover explicit specializations. Note that |
| 2840 | // we don't suppress if this turns out to be an elaborated type |
| 2841 | // specifier. |
| 2842 | bool shouldDelayDiagsInTag = |
| 2843 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 2844 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 2845 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2846 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2847 | // If attributes exist after tag, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2848 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2849 | MaybeParseGNUAttributes(attrs); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2850 | |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 2851 | // If declspecs exist after tag, parse them. |
| 2852 | while (Tok.is(tok::kw___declspec)) |
| 2853 | ParseMicrosoftDeclSpec(attrs); |
| 2854 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2855 | // Enum definitions should not be parsed in a trailing-return-type. |
| 2856 | bool AllowDeclaration = DSC != DSC_trailing; |
| 2857 | |
| 2858 | bool AllowFixedUnderlyingType = AllowDeclaration && |
| 2859 | (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt || |
| 2860 | getLangOpts().ObjC2); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2861 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2862 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2863 | if (getLangOpts().CPlusPlus) { |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2864 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 2865 | // if a fixed underlying type is allowed. |
| 2866 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
| 2867 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2868 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
| 2869 | /*EnteringContext=*/false)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2870 | return; |
| 2871 | |
| 2872 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2873 | Diag(Tok, diag::err_expected_ident); |
| 2874 | if (Tok.isNot(tok::l_brace)) { |
| 2875 | // Has no name and is not a definition. |
| 2876 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2877 | SkipUntil(tok::comma, true); |
| 2878 | return; |
| 2879 | } |
| 2880 | } |
| 2881 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2882 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2883 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2884 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2885 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2886 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2887 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2888 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2889 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2890 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2891 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2892 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2893 | // If an identifier is present, consume and remember it. |
| 2894 | IdentifierInfo *Name = 0; |
| 2895 | SourceLocation NameLoc; |
| 2896 | if (Tok.is(tok::identifier)) { |
| 2897 | Name = Tok.getIdentifierInfo(); |
| 2898 | NameLoc = ConsumeToken(); |
| 2899 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2900 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2901 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2902 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 2903 | // declaration of a scoped enumeration. |
| 2904 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2905 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2906 | IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2907 | } |
| 2908 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 2909 | // Okay, end the suppression area. We'll decide whether to emit the |
| 2910 | // diagnostics in a second. |
| 2911 | if (shouldDelayDiagsInTag) |
| 2912 | diagsFromTag.done(); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2913 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2914 | TypeResult BaseType; |
| 2915 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2916 | // Parse the fixed underlying type. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2917 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2918 | bool PossibleBitfield = false; |
| 2919 | if (getCurScope()->getFlags() & Scope::ClassScope) { |
| 2920 | // If we're in class scope, this can either be an enum declaration with |
| 2921 | // an underlying type, or a declaration of a bitfield member. We try to |
| 2922 | // use a simple disambiguation scheme first to catch the common cases |
| 2923 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 2924 | // anything that's a simple-type-specifier followed by '(' as an |
| 2925 | // expression. This suffices because function types are not valid |
| 2926 | // underlying types anyway. |
| 2927 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
| 2928 | // If the next token starts an expression, we know we're parsing a |
| 2929 | // bit-field. This is the common case. |
| 2930 | if (TPR == TPResult::True()) |
| 2931 | PossibleBitfield = true; |
| 2932 | // If the next token starts a type-specifier-seq, it may be either a |
| 2933 | // a fixed underlying type or the start of a function-style cast in C++; |
| 2934 | // lookahead one more token to see if it's obvious that we have a |
| 2935 | // fixed underlying type. |
| 2936 | else if (TPR == TPResult::False() && |
| 2937 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 2938 | // Consume the ':'. |
| 2939 | ConsumeToken(); |
| 2940 | } else { |
| 2941 | // We have the start of a type-specifier-seq, so we have to perform |
| 2942 | // tentative parsing to determine whether we have an expression or a |
| 2943 | // type. |
| 2944 | TentativeParsingAction TPA(*this); |
| 2945 | |
| 2946 | // Consume the ':'. |
| 2947 | ConsumeToken(); |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 2948 | |
| 2949 | // If we see a type specifier followed by an open-brace, we have an |
| 2950 | // ambiguity between an underlying type and a C++11 braced |
| 2951 | // function-style cast. Resolve this by always treating it as an |
| 2952 | // underlying type. |
| 2953 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 2954 | // this case. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2955 | if ((getLangOpts().CPlusPlus && |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 2956 | isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2957 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2958 | // We'll parse this as a bitfield later. |
| 2959 | PossibleBitfield = true; |
| 2960 | TPA.Revert(); |
| 2961 | } else { |
| 2962 | // We have a type-specifier-seq. |
| 2963 | TPA.Commit(); |
| 2964 | } |
| 2965 | } |
| 2966 | } else { |
| 2967 | // Consume the ':'. |
| 2968 | ConsumeToken(); |
| 2969 | } |
| 2970 | |
| 2971 | if (!PossibleBitfield) { |
| 2972 | SourceRange Range; |
| 2973 | BaseType = ParseTypeName(&Range); |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 2974 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2975 | if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2) |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 2976 | Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type) |
| 2977 | << Range; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2978 | if (getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2979 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2980 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2981 | } |
| 2982 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2983 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 2984 | // friend declaration, and cannot have an accompanying definition. If we have |
| 2985 | // 'enum foo;', then this is a forward declaration. If we have |
| 2986 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 2987 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2988 | // |
| 2989 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 2990 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 2991 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 2992 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2993 | Sema::TagUseKind TUK; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 2994 | if (!AllowDeclaration) { |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2995 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 2996 | } else if (Tok.is(tok::l_brace)) { |
| 2997 | if (DS.isFriendSpecified()) { |
| 2998 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
| 2999 | << SourceRange(DS.getFriendSpecLoc()); |
| 3000 | ConsumeBrace(); |
| 3001 | SkipUntil(tok::r_brace); |
| 3002 | TUK = Sema::TUK_Friend; |
| 3003 | } else { |
| 3004 | TUK = Sema::TUK_Definition; |
| 3005 | } |
| 3006 | } else if (Tok.is(tok::semi) && DSC != DSC_type_specifier) { |
| 3007 | TUK = (DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration); |
| 3008 | } else { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3009 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3010 | } |
| 3011 | |
| 3012 | // If this is an elaborated type specifier, and we delayed |
| 3013 | // diagnostics before, just merge them into the current pool. |
| 3014 | if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) { |
| 3015 | diagsFromTag.redelay(); |
| 3016 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3017 | |
| 3018 | MultiTemplateParamsArg TParams; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3019 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3020 | TUK != Sema::TUK_Reference) { |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3021 | if (!getLangOpts().CPlusPlus0x || !SS.isSet()) { |
| 3022 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3023 | Diag(Tok, diag::err_enum_template); |
| 3024 | SkipUntil(tok::comma, true); |
| 3025 | return; |
| 3026 | } |
| 3027 | |
| 3028 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 3029 | // Enumerations can't be explicitly instantiated. |
| 3030 | DS.SetTypeSpecError(); |
| 3031 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 3032 | return; |
| 3033 | } |
| 3034 | |
| 3035 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 3036 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 3037 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3038 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3039 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3040 | if (!Name && TUK != Sema::TUK_Definition) { |
| 3041 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3042 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3043 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3044 | SkipUntil(tok::comma, true); |
| 3045 | return; |
| 3046 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3047 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3048 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3049 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3050 | const char *PrevSpec = 0; |
| 3051 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3052 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3053 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3054 | AS, DS.getModulePrivateSpecLoc(), TParams, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3055 | Owned, IsDependent, ScopedEnumKWLoc, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3056 | IsScopedUsingClassTag, BaseType); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3057 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3058 | if (IsDependent) { |
| 3059 | // This enum has a dependent nested-name-specifier. Handle it as a |
| 3060 | // dependent tag. |
| 3061 | if (!Name) { |
| 3062 | DS.SetTypeSpecError(); |
| 3063 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 3064 | return; |
| 3065 | } |
| 3066 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3067 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3068 | TUK, SS, Name, StartLoc, |
| 3069 | NameLoc); |
| 3070 | if (Type.isInvalid()) { |
| 3071 | DS.SetTypeSpecError(); |
| 3072 | return; |
| 3073 | } |
| 3074 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3075 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 3076 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3077 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3078 | Diag(StartLoc, DiagID) << PrevSpec; |
| 3079 | |
| 3080 | return; |
| 3081 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3082 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3083 | if (!TagDecl) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3084 | // The action failed to produce an enumeration tag. If this is a |
| 3085 | // definition, consume the entire definition. |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3086 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3087 | ConsumeBrace(); |
| 3088 | SkipUntil(tok::r_brace); |
| 3089 | } |
| 3090 | |
| 3091 | DS.SetTypeSpecError(); |
| 3092 | return; |
| 3093 | } |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3094 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3095 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3096 | ParseEnumBody(StartLoc, TagDecl); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3097 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3098 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3099 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3100 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3101 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3102 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3103 | } |
| 3104 | |
| 3105 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3106 | /// enumerator-list: |
| 3107 | /// enumerator |
| 3108 | /// enumerator-list ',' enumerator |
| 3109 | /// enumerator: |
| 3110 | /// enumeration-constant |
| 3111 | /// enumeration-constant '=' constant-expression |
| 3112 | /// enumeration-constant: |
| 3113 | /// identifier |
| 3114 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3115 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3116 | // Enter the scope of the enum body and start the definition. |
| 3117 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3118 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3119 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3120 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3121 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3122 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3123 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3124 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3125 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3126 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3127 | SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3128 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3129 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3130 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3131 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3132 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3133 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3134 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3135 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3136 | // If attributes exist after the enumerator, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3137 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3138 | MaybeParseGNUAttributes(attrs); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3139 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3140 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3141 | ExprResult AssignedVal; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 3142 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3143 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3144 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3145 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3146 | AssignedVal = ParseConstantExpression(); |
| 3147 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3148 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3149 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3150 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3151 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3152 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3153 | LastEnumConstDecl, |
| 3154 | IdentLoc, Ident, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3155 | attrs.getList(), EqualLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3156 | AssignedVal.release()); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3157 | PD.complete(EnumConstDecl); |
| 3158 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3159 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3160 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3161 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3162 | if (Tok.is(tok::identifier)) { |
| 3163 | // We're missing a comma between enumerators. |
| 3164 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
| 3165 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
| 3166 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3167 | continue; |
| 3168 | } |
| 3169 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3170 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3171 | break; |
| 3172 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3173 | |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3174 | if (Tok.isNot(tok::identifier)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3175 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3176 | Diag(CommaLoc, diag::ext_enumerator_list_comma) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3177 | << getLangOpts().CPlusPlus |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3178 | << FixItHint::CreateRemoval(CommaLoc); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3179 | else if (getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3180 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3181 | << FixItHint::CreateRemoval(CommaLoc); |
| 3182 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3183 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3184 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3185 | // Eat the }. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3186 | T.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3187 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3188 | // If attributes exist after the identifier list, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3189 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3190 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3191 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3192 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
| 3193 | EnumDecl, EnumConstantDecls.data(), |
| 3194 | EnumConstantDecls.size(), getCurScope(), |
| 3195 | attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3196 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3197 | EnumScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3198 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3199 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3200 | } |
| 3201 | |
| 3202 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3203 | /// start of a type-qualifier-list. |
| 3204 | bool Parser::isTypeQualifier() const { |
| 3205 | switch (Tok.getKind()) { |
| 3206 | default: return false; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3207 | |
| 3208 | // type-qualifier only in OpenCL |
| 3209 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3210 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3211 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3212 | // type-qualifier |
| 3213 | case tok::kw_const: |
| 3214 | case tok::kw_volatile: |
| 3215 | case tok::kw_restrict: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3216 | case tok::kw___private: |
| 3217 | case tok::kw___local: |
| 3218 | case tok::kw___global: |
| 3219 | case tok::kw___constant: |
| 3220 | case tok::kw___read_only: |
| 3221 | case tok::kw___read_write: |
| 3222 | case tok::kw___write_only: |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3223 | return true; |
| 3224 | } |
| 3225 | } |
| 3226 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3227 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3228 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3229 | /// specifier or if we're not sure. |
| 3230 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3231 | switch (Tok.getKind()) { |
| 3232 | default: return false; |
| 3233 | // type-specifiers |
| 3234 | case tok::kw_short: |
| 3235 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3236 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3237 | case tok::kw___int128: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3238 | case tok::kw_signed: |
| 3239 | case tok::kw_unsigned: |
| 3240 | case tok::kw__Complex: |
| 3241 | case tok::kw__Imaginary: |
| 3242 | case tok::kw_void: |
| 3243 | case tok::kw_char: |
| 3244 | case tok::kw_wchar_t: |
| 3245 | case tok::kw_char16_t: |
| 3246 | case tok::kw_char32_t: |
| 3247 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3248 | case tok::kw_half: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3249 | case tok::kw_float: |
| 3250 | case tok::kw_double: |
| 3251 | case tok::kw_bool: |
| 3252 | case tok::kw__Bool: |
| 3253 | case tok::kw__Decimal32: |
| 3254 | case tok::kw__Decimal64: |
| 3255 | case tok::kw__Decimal128: |
| 3256 | case tok::kw___vector: |
| 3257 | |
| 3258 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3259 | case tok::kw_class: |
| 3260 | case tok::kw_struct: |
| 3261 | case tok::kw_union: |
| 3262 | // enum-specifier |
| 3263 | case tok::kw_enum: |
| 3264 | |
| 3265 | // typedef-name |
| 3266 | case tok::annot_typename: |
| 3267 | return true; |
| 3268 | } |
| 3269 | } |
| 3270 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3271 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3272 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3273 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3274 | switch (Tok.getKind()) { |
| 3275 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3276 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3277 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3278 | if (TryAltiVecVectorToken()) |
| 3279 | return true; |
| 3280 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3281 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3282 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3283 | // recurse to handle whatever we get. |
| 3284 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3285 | return true; |
| 3286 | if (Tok.is(tok::identifier)) |
| 3287 | return false; |
| 3288 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3289 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3290 | case tok::coloncolon: // ::foo::bar |
| 3291 | if (NextToken().is(tok::kw_new) || // ::new |
| 3292 | NextToken().is(tok::kw_delete)) // ::delete |
| 3293 | return false; |
| 3294 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3295 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3296 | return true; |
| 3297 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3298 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3299 | // GNU attributes support. |
| 3300 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3301 | // GNU typeof support. |
| 3302 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3303 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3304 | // type-specifiers |
| 3305 | case tok::kw_short: |
| 3306 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3307 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3308 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3309 | case tok::kw_signed: |
| 3310 | case tok::kw_unsigned: |
| 3311 | case tok::kw__Complex: |
| 3312 | case tok::kw__Imaginary: |
| 3313 | case tok::kw_void: |
| 3314 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3315 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3316 | case tok::kw_char16_t: |
| 3317 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3318 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3319 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3320 | case tok::kw_float: |
| 3321 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3322 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3323 | case tok::kw__Bool: |
| 3324 | case tok::kw__Decimal32: |
| 3325 | case tok::kw__Decimal64: |
| 3326 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3327 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3328 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3329 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3330 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3331 | case tok::kw_struct: |
| 3332 | case tok::kw_union: |
| 3333 | // enum-specifier |
| 3334 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3335 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3336 | // type-qualifier |
| 3337 | case tok::kw_const: |
| 3338 | case tok::kw_volatile: |
| 3339 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3340 | |
| 3341 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 3342 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3343 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3344 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 3345 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3346 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3347 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3348 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3349 | case tok::kw___cdecl: |
| 3350 | case tok::kw___stdcall: |
| 3351 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3352 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3353 | case tok::kw___w64: |
| 3354 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3355 | case tok::kw___ptr32: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3356 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3357 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3358 | |
| 3359 | case tok::kw___private: |
| 3360 | case tok::kw___local: |
| 3361 | case tok::kw___global: |
| 3362 | case tok::kw___constant: |
| 3363 | case tok::kw___read_only: |
| 3364 | case tok::kw___read_write: |
| 3365 | case tok::kw___write_only: |
| 3366 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3367 | return true; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3368 | |
| 3369 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3370 | return getLangOpts().OpenCL; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3371 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3372 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3373 | case tok::kw__Atomic: |
| 3374 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3375 | } |
| 3376 | } |
| 3377 | |
| 3378 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 3379 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3380 | /// |
| 3381 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 3382 | /// this check is to disambiguate between an expression and a declaration. |
| 3383 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3384 | switch (Tok.getKind()) { |
| 3385 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3386 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3387 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3388 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3389 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3390 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3391 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3392 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3393 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3394 | if (TryAltiVecVectorToken()) |
| 3395 | return true; |
| 3396 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3397 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3398 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3399 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3400 | // recurse to handle whatever we get. |
| 3401 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3402 | return true; |
| 3403 | if (Tok.is(tok::identifier)) |
| 3404 | return false; |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3405 | |
| 3406 | // If we're in Objective-C and we have an Objective-C class type followed |
| 3407 | // by an identifier and then either ':' or ']', in a place where an |
| 3408 | // expression is permitted, then this is probably a class message send |
| 3409 | // missing the initial '['. In this case, we won't consider this to be |
| 3410 | // the start of a declaration. |
| 3411 | if (DisambiguatingWithExpression && |
| 3412 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 3413 | return false; |
| 3414 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3415 | return isDeclarationSpecifier(); |
| 3416 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3417 | case tok::coloncolon: // ::foo::bar |
| 3418 | if (NextToken().is(tok::kw_new) || // ::new |
| 3419 | NextToken().is(tok::kw_delete)) // ::delete |
| 3420 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3421 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3422 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3423 | // recurse to handle whatever we get. |
| 3424 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3425 | return true; |
| 3426 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3427 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3428 | // storage-class-specifier |
| 3429 | case tok::kw_typedef: |
| 3430 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 3431 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3432 | case tok::kw_static: |
| 3433 | case tok::kw_auto: |
| 3434 | case tok::kw_register: |
| 3435 | case tok::kw___thread: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3436 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 3437 | // Modules |
| 3438 | case tok::kw___module_private__: |
| 3439 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3440 | // type-specifiers |
| 3441 | case tok::kw_short: |
| 3442 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3443 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3444 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3445 | case tok::kw_signed: |
| 3446 | case tok::kw_unsigned: |
| 3447 | case tok::kw__Complex: |
| 3448 | case tok::kw__Imaginary: |
| 3449 | case tok::kw_void: |
| 3450 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3451 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3452 | case tok::kw_char16_t: |
| 3453 | case tok::kw_char32_t: |
| 3454 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3455 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3456 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3457 | case tok::kw_float: |
| 3458 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3459 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3460 | case tok::kw__Bool: |
| 3461 | case tok::kw__Decimal32: |
| 3462 | case tok::kw__Decimal64: |
| 3463 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3464 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3465 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3466 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3467 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3468 | case tok::kw_struct: |
| 3469 | case tok::kw_union: |
| 3470 | // enum-specifier |
| 3471 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3472 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3473 | // type-qualifier |
| 3474 | case tok::kw_const: |
| 3475 | case tok::kw_volatile: |
| 3476 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3477 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3478 | // function-specifier |
| 3479 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3480 | case tok::kw_virtual: |
| 3481 | case tok::kw_explicit: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3482 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 3483 | // static_assert-declaration |
| 3484 | case tok::kw__Static_assert: |
| 3485 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3486 | // GNU typeof support. |
| 3487 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3488 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3489 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3490 | case tok::kw___attribute: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3491 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3492 | |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3493 | // C++0x decltype. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3494 | case tok::annot_decltype: |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3495 | return true; |
| 3496 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3497 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3498 | case tok::kw__Atomic: |
| 3499 | return true; |
| 3500 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 3501 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3502 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3503 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3504 | |
Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 3505 | // typedef-name |
| 3506 | case tok::annot_typename: |
| 3507 | return !DisambiguatingWithExpression || |
| 3508 | !isStartOfObjCClassMessageMissingOpenBracket(); |
| 3509 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 3510 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3511 | case tok::kw___cdecl: |
| 3512 | case tok::kw___stdcall: |
| 3513 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3514 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3515 | case tok::kw___w64: |
| 3516 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3517 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3518 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3519 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3520 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3521 | |
| 3522 | case tok::kw___private: |
| 3523 | case tok::kw___local: |
| 3524 | case tok::kw___global: |
| 3525 | case tok::kw___constant: |
| 3526 | case tok::kw___read_only: |
| 3527 | case tok::kw___read_write: |
| 3528 | case tok::kw___write_only: |
| 3529 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3530 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3531 | } |
| 3532 | } |
| 3533 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3534 | bool Parser::isConstructorDeclarator() { |
| 3535 | TentativeParsingAction TPA(*this); |
| 3536 | |
| 3537 | // Parse the C++ scope specifier. |
| 3538 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3539 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
| 3540 | /*EnteringContext=*/true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3541 | TPA.Revert(); |
| 3542 | return false; |
| 3543 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3544 | |
| 3545 | // Parse the constructor name. |
| 3546 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 3547 | // We already know that we have a constructor name; just consume |
| 3548 | // the token. |
| 3549 | ConsumeToken(); |
| 3550 | } else { |
| 3551 | TPA.Revert(); |
| 3552 | return false; |
| 3553 | } |
| 3554 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 3555 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3556 | if (Tok.isNot(tok::l_paren)) { |
| 3557 | TPA.Revert(); |
| 3558 | return false; |
| 3559 | } |
| 3560 | ConsumeParen(); |
| 3561 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 3562 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 3563 | // that we have a constructor. |
| 3564 | if (Tok.is(tok::r_paren) || |
| 3565 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3566 | TPA.Revert(); |
| 3567 | return true; |
| 3568 | } |
| 3569 | |
| 3570 | // If we need to, enter the specified scope. |
| 3571 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3572 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3573 | DeclScopeObj.EnterDeclaratorScope(); |
| 3574 | |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3575 | // Optionally skip Microsoft attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3576 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3577 | MaybeParseMicrosoftAttributes(Attrs); |
| 3578 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3579 | // Check whether the next token(s) are part of a declaration |
| 3580 | // specifier, in which case we have the start of a parameter and, |
| 3581 | // therefore, we know that this is a constructor. |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 3582 | bool IsConstructor = false; |
| 3583 | if (isDeclarationSpecifier()) |
| 3584 | IsConstructor = true; |
| 3585 | else if (Tok.is(tok::identifier) || |
| 3586 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 3587 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 3588 | // This might be a parenthesized member name, but is more likely to |
| 3589 | // be a constructor declaration with an invalid argument type. Keep |
| 3590 | // looking. |
| 3591 | if (Tok.is(tok::annot_cxxscope)) |
| 3592 | ConsumeToken(); |
| 3593 | ConsumeToken(); |
| 3594 | |
| 3595 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 3596 | // which must have one of the following syntactic forms (see the |
| 3597 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 3598 | switch (Tok.getKind()) { |
| 3599 | case tok::l_paren: |
| 3600 | // C(X ( int)); |
| 3601 | case tok::l_square: |
| 3602 | // C(X [ 5]); |
| 3603 | // C(X [ [attribute]]); |
| 3604 | case tok::coloncolon: |
| 3605 | // C(X :: Y); |
| 3606 | // C(X :: *p); |
| 3607 | case tok::r_paren: |
| 3608 | // C(X ) |
| 3609 | // Assume this isn't a constructor, rather than assuming it's a |
| 3610 | // constructor with an unnamed parameter of an ill-formed type. |
| 3611 | break; |
| 3612 | |
| 3613 | default: |
| 3614 | IsConstructor = true; |
| 3615 | break; |
| 3616 | } |
| 3617 | } |
| 3618 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3619 | TPA.Revert(); |
| 3620 | return IsConstructor; |
| 3621 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3622 | |
| 3623 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3624 | /// type-qualifier-list: [C99 6.7.5] |
| 3625 | /// type-qualifier |
| 3626 | /// [vendor] attributes |
| 3627 | /// [ only if VendorAttributesAllowed=true ] |
| 3628 | /// type-qualifier-list type-qualifier |
| 3629 | /// [vendor] type-qualifier-list attributes |
| 3630 | /// [ only if VendorAttributesAllowed=true ] |
| 3631 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
| 3632 | /// [ only if CXX0XAttributesAllowed=true ] |
| 3633 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3634 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3635 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 3636 | bool VendorAttributesAllowed, |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3637 | bool CXX11AttributesAllowed) { |
| 3638 | if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed && |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3639 | isCXX11AttributeSpecifier()) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3640 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3641 | ParseCXX11Attributes(attrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3642 | DS.takeAttributesFrom(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3643 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3644 | |
| 3645 | SourceLocation EndLoc; |
| 3646 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3647 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3648 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3649 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3650 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3651 | SourceLocation Loc = Tok.getLocation(); |
| 3652 | |
| 3653 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3654 | case tok::code_completion: |
| 3655 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3656 | return cutOffParsing(); |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3657 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3658 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3659 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3660 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3661 | break; |
| 3662 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3663 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3664 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3665 | break; |
| 3666 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3667 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3668 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3669 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3670 | |
| 3671 | // OpenCL qualifiers: |
| 3672 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3673 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3674 | goto DoneWithTypeQuals; |
| 3675 | case tok::kw___private: |
| 3676 | case tok::kw___global: |
| 3677 | case tok::kw___local: |
| 3678 | case tok::kw___constant: |
| 3679 | case tok::kw___read_only: |
| 3680 | case tok::kw___write_only: |
| 3681 | case tok::kw___read_write: |
| 3682 | ParseOpenCLQualifiers(DS); |
| 3683 | break; |
| 3684 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3685 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 3686 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3687 | case tok::kw___ptr32: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3688 | case tok::kw___cdecl: |
| 3689 | case tok::kw___stdcall: |
| 3690 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3691 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3692 | case tok::kw___unaligned: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3693 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3694 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3695 | continue; |
| 3696 | } |
| 3697 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3698 | case tok::kw___pascal: |
| 3699 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3700 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3701 | continue; |
| 3702 | } |
| 3703 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3704 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3705 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3706 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3707 | continue; // do *not* consume the next token! |
| 3708 | } |
| 3709 | // otherwise, FALL THROUGH! |
| 3710 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3711 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3712 | // If this is not a type-qualifier token, we're done reading type |
| 3713 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 3714 | DS.Finish(Diags, PP); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3715 | if (EndLoc.isValid()) |
| 3716 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3717 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3718 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 3719 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3720 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 3721 | if (isInvalid) { |
| 3722 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3723 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3724 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3725 | EndLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3726 | } |
| 3727 | } |
| 3728 | |
| 3729 | |
| 3730 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 3731 | /// |
| 3732 | void Parser::ParseDeclarator(Declarator &D) { |
| 3733 | /// This implements the 'declarator' production in the C grammar, then checks |
| 3734 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3735 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3736 | } |
| 3737 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3738 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) { |
| 3739 | if (Kind == tok::star || Kind == tok::caret) |
| 3740 | return true; |
| 3741 | |
| 3742 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 3743 | if (!Lang.CPlusPlus) |
| 3744 | return false; |
| 3745 | |
| 3746 | return Kind == tok::amp || Kind == tok::ampamp; |
| 3747 | } |
| 3748 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3749 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 3750 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 3751 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3752 | /// ptr-operator production. |
| 3753 | /// |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 3754 | /// 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] | 3755 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 3756 | /// isConstructorDeclarator. |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 3757 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3758 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 3759 | /// [C] pointer[opt] direct-declarator |
| 3760 | /// [C++] direct-declarator |
| 3761 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3762 | /// |
| 3763 | /// pointer: [C99 6.7.5] |
| 3764 | /// '*' type-qualifier-list[opt] |
| 3765 | /// '*' type-qualifier-list[opt] pointer |
| 3766 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3767 | /// ptr-operator: |
| 3768 | /// '*' cv-qualifier-seq[opt] |
| 3769 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3770 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3771 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3772 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3773 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3774 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 3775 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3776 | if (Diags.hasAllExtensionsSilenced()) |
| 3777 | D.setExtension(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3778 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3779 | // C++ member pointers start with a '::' or a nested-name. |
| 3780 | // Member pointers get special handling, since there's no place for the |
| 3781 | // scope spec in the generic path below. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3782 | if (getLangOpts().CPlusPlus && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 3783 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 3784 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3785 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 3786 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3787 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3788 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3789 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 3790 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3791 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3792 | // The scope spec really belongs to the direct-declarator. |
| 3793 | D.getCXXScopeSpec() = SS; |
| 3794 | if (DirectDeclParser) |
| 3795 | (this->*DirectDeclParser)(D); |
| 3796 | return; |
| 3797 | } |
| 3798 | |
| 3799 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3800 | D.SetRangeEnd(Loc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3801 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3802 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3803 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3804 | |
| 3805 | // Recurse to parse whatever is left. |
| 3806 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 3807 | |
| 3808 | // Sema will have to catch (syntactically invalid) pointers into global |
| 3809 | // scope. It has to catch pointers into namespace scope anyway. |
| 3810 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3811 | Loc), |
| 3812 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3813 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3814 | return; |
| 3815 | } |
| 3816 | } |
| 3817 | |
| 3818 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3819 | // Not a pointer, C++ reference, or block. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3820 | if (!isPtrOperatorToken(Kind, getLangOpts())) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3821 | if (DirectDeclParser) |
| 3822 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3823 | return; |
| 3824 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3825 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3826 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 3827 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3828 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3829 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3830 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 3831 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 3832 | // Is a pointer. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3833 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3834 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3835 | // FIXME: GNU attributes are not allowed here in a new-type-id. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3836 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3837 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3838 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3839 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3840 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3841 | if (Kind == tok::star) |
| 3842 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 3843 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 3844 | DS.getConstSpecLoc(), |
| 3845 | DS.getVolatileSpecLoc(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3846 | DS.getRestrictSpecLoc()), |
| 3847 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3848 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3849 | else |
| 3850 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3851 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3852 | Loc), |
| 3853 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3854 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3855 | } else { |
| 3856 | // Is a reference |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3857 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3858 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3859 | // Complain about rvalue references in C++03, but then go on and build |
| 3860 | // the declarator. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3861 | if (Kind == tok::ampamp) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3862 | Diag(Loc, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3863 | diag::warn_cxx98_compat_rvalue_reference : |
| 3864 | diag::ext_rvalue_reference); |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3865 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3866 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 3867 | ParseTypeQualifierListOpt(DS); |
| 3868 | D.ExtendWithDeclSpec(DS); |
| 3869 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3870 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 3871 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 3872 | // template type argument, in which case the cv-qualifiers are ignored. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3873 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 3874 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 3875 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3876 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3877 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 3878 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3879 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3880 | } |
| 3881 | |
| 3882 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3883 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3884 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3885 | if (D.getNumTypeObjects() > 0) { |
| 3886 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 3887 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 3888 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 3889 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 3890 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 3891 | << II; |
| 3892 | else |
| 3893 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 3894 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3895 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3896 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3897 | // can go ahead and build the (technically ill-formed) |
| 3898 | // declarator: reference collapsing will take care of it. |
| 3899 | } |
| 3900 | } |
| 3901 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3902 | // 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] | 3903 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3904 | Kind == tok::amp), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3905 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3906 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3907 | } |
| 3908 | } |
| 3909 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3910 | static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D, |
| 3911 | SourceLocation EllipsisLoc) { |
| 3912 | if (EllipsisLoc.isValid()) { |
| 3913 | FixItHint Insertion; |
| 3914 | if (!D.getEllipsisLoc().isValid()) { |
| 3915 | Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "..."); |
| 3916 | D.setEllipsisLoc(EllipsisLoc); |
| 3917 | } |
| 3918 | P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) |
| 3919 | << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName(); |
| 3920 | } |
| 3921 | } |
| 3922 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3923 | /// ParseDirectDeclarator |
| 3924 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3925 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3926 | /// '(' declarator ')' |
| 3927 | /// [GNU] '(' attributes declarator ')' |
| 3928 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 3929 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 3930 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 3931 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 3932 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3933 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 3934 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3935 | /// direct-declarator '(' parameter-type-list ')' |
| 3936 | /// direct-declarator '(' identifier-list[opt] ')' |
| 3937 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 3938 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3939 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 3940 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3941 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 3942 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 3943 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3944 | /// [C++] declarator-id |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3945 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3946 | /// |
| 3947 | /// declarator-id: [C++ 8] |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3948 | /// '...'[opt] id-expression |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3949 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 3950 | /// |
| 3951 | /// id-expression: [C++ 5.1] |
| 3952 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3953 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3954 | /// |
| 3955 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3956 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3957 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3958 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3959 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3960 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 3961 | /// |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 3962 | /// Note, any additional constructs added here may need corresponding changes |
| 3963 | /// in isConstructorDeclarator. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3964 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3965 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3966 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3967 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3968 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3969 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3970 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 3971 | D.getContext() == Declarator::MemberContext; |
| 3972 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
| 3973 | EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3974 | } |
| 3975 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3976 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3977 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 3978 | // Change the declaration context for name lookup, until this function |
| 3979 | // is exited (and the declarator has been parsed). |
| 3980 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3981 | } |
| 3982 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3983 | // C++0x [dcl.fct]p14: |
| 3984 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
| 3985 | // of a parameter-declaration-clause without a preceding comma. In |
| 3986 | // this case, the ellipsis is parsed as part of the |
| 3987 | // abstract-declarator if the type of the parameter names a template |
| 3988 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 3989 | // as part of the parameter-declaration-clause. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3990 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3991 | !((D.getContext() == Declarator::PrototypeContext || |
| 3992 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3993 | NextToken().is(tok::r_paren) && |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3994 | !Actions.containsUnexpandedParameterPacks(D))) { |
| 3995 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 3996 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) { |
| 3997 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 3998 | // the user what they should have done. |
| 3999 | ParseDeclarator(D); |
| 4000 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4001 | return; |
| 4002 | } else |
| 4003 | D.setEllipsisLoc(EllipsisLoc); |
| 4004 | |
| 4005 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 4006 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 4007 | // the l_paren token. |
| 4008 | } |
| 4009 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4010 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 4011 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 4012 | // We found something that indicates the start of an unqualified-id. |
| 4013 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4014 | bool AllowConstructorName; |
| 4015 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 4016 | AllowConstructorName = false; |
| 4017 | else if (D.getCXXScopeSpec().isSet()) |
| 4018 | AllowConstructorName = |
| 4019 | (D.getContext() == Declarator::FileContext || |
| 4020 | (D.getContext() == Declarator::MemberContext && |
| 4021 | D.getDeclSpec().isFriendSpecified())); |
| 4022 | else |
| 4023 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 4024 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4025 | SourceLocation TemplateKWLoc; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4026 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 4027 | /*EnteringContext=*/true, |
| 4028 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4029 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4030 | ParsedType(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4031 | TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4032 | D.getName()) || |
| 4033 | // Once we're past the identifier, if the scope was bad, mark the |
| 4034 | // whole declarator bad. |
| 4035 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4036 | D.SetIdentifier(0, Tok.getLocation()); |
| 4037 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4038 | } else { |
| 4039 | // Parsed the unqualified-id; update range information and move along. |
| 4040 | if (D.getSourceRange().getBegin().isInvalid()) |
| 4041 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 4042 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4043 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4044 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4045 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4046 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4047 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4048 | "There's a C++-specific check for tok::identifier above"); |
| 4049 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 4050 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 4051 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4052 | goto PastIdentifier; |
| 4053 | } |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4054 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4055 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4056 | // direct-declarator: '(' declarator ')' |
| 4057 | // direct-declarator: '(' attributes declarator ')' |
| 4058 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 4059 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4060 | |
| 4061 | // If the declarator was parenthesized, we entered the declarator |
| 4062 | // scope when parsing the parenthesized declarator, then exited |
| 4063 | // the scope already. Re-enter the scope, if we need to. |
| 4064 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4065 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4066 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4067 | if (!D.isInvalidType() && |
| 4068 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4069 | // Change the declaration context for name lookup, until this function |
| 4070 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4071 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4072 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4073 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4074 | // This could be something simple like "int" (in which case the declarator |
| 4075 | // portion is empty), if an abstract-declarator is allowed. |
| 4076 | D.SetIdentifier(0, Tok.getLocation()); |
| 4077 | } else { |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 4078 | if (D.getContext() == Declarator::MemberContext) |
| 4079 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 4080 | << D.getDeclSpec().getSourceRange(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4081 | else if (getLangOpts().CPlusPlus) |
| 4082 | Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4083 | else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4084 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4085 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 4086 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4087 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4088 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4089 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4090 | assert(D.isPastIdentifier() && |
| 4091 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4092 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4093 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 4094 | if (D.hasName() && !D.getNumTypeObjects()) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4095 | MaybeParseCXX0XAttributes(D); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4096 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4097 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4098 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4099 | // Enter function-declaration scope, limiting any declarators to the |
| 4100 | // function prototype scope, including parameter declarators. |
| 4101 | ParseScope PrototypeScope(this, |
| 4102 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4103 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 4104 | // In such a case, check if we actually have a function declarator; if it |
| 4105 | // is not, the declarator has been fully parsed. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4106 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4107 | // When not in file scope, warn for ambiguous function declarators, just |
| 4108 | // in case the author intended it as a variable definition. |
| 4109 | bool warnIfAmbiguous = D.getContext() != Declarator::FileContext; |
| 4110 | if (!isCXXFunctionDeclarator(warnIfAmbiguous)) |
| 4111 | break; |
| 4112 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4113 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4114 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4115 | T.consumeOpen(); |
| 4116 | ParseFunctionDeclarator(D, attrs, T); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4117 | PrototypeScope.Exit(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4118 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4119 | ParseBracketDeclarator(D); |
| 4120 | } else { |
| 4121 | break; |
| 4122 | } |
| 4123 | } |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4124 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4125 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4126 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 4127 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4128 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4129 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 4130 | /// |
| 4131 | /// direct-declarator: |
| 4132 | /// '(' declarator ')' |
| 4133 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4134 | /// direct-declarator '(' parameter-type-list ')' |
| 4135 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4136 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4137 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4138 | /// |
| 4139 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4140 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4141 | T.consumeOpen(); |
| 4142 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4143 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4144 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4145 | // Eat any attributes before we look at whether this is a grouping or function |
| 4146 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 4147 | // the type being built up, for example: |
| 4148 | // int (__attribute__(()) *x)(long y) |
| 4149 | // If this ends up not being a grouping paren, the attribute applies to the |
| 4150 | // first argument, for example: |
| 4151 | // int (__attribute__(()) int x) |
| 4152 | // In either case, we need to eat any attributes to be able to determine what |
| 4153 | // sort of paren this is. |
| 4154 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4155 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4156 | bool RequiresArg = false; |
| 4157 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4158 | ParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4159 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4160 | // We require that the argument list (if this is a non-grouping paren) be |
| 4161 | // present even if the attribute list was empty. |
| 4162 | RequiresArg = true; |
| 4163 | } |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4164 | // Eat any Microsoft extensions. |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4165 | if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4166 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4167 | Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4168 | Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4169 | ParseMicrosoftTypeAttributes(attrs); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4170 | } |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4171 | // Eat any Borland extensions. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 4172 | if (Tok.is(tok::kw___pascal)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4173 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4174 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4175 | // If we haven't past the identifier yet (or where the identifier would be |
| 4176 | // stored, if this is an abstract declarator), then this is probably just |
| 4177 | // grouping parens. However, if this could be an abstract-declarator, then |
| 4178 | // this could also be the start of function arguments (consider 'void()'). |
| 4179 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4180 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4181 | if (!D.mayOmitIdentifier()) { |
| 4182 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 4183 | // paren, because we haven't seen the identifier yet. |
| 4184 | isGrouping = true; |
| 4185 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4186 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 4187 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4188 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 4189 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4190 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 4191 | // considered to be a type, not a K&R identifier-list. |
| 4192 | isGrouping = false; |
| 4193 | } else { |
| 4194 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 4195 | isGrouping = true; |
| 4196 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4197 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4198 | // If this is a grouping paren, handle: |
| 4199 | // direct-declarator: '(' declarator ')' |
| 4200 | // direct-declarator: '(' attributes declarator ')' |
| 4201 | if (isGrouping) { |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4202 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 4203 | D.setEllipsisLoc(SourceLocation()); |
| 4204 | |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4205 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4206 | D.setGroupingParens(true); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4207 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4208 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4209 | T.consumeClose(); |
| 4210 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
| 4211 | T.getCloseLocation()), |
| 4212 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4213 | |
| 4214 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4215 | |
| 4216 | // An ellipsis cannot be placed outside parentheses. |
| 4217 | if (EllipsisLoc.isValid()) |
| 4218 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4219 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4220 | return; |
| 4221 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4222 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4223 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 4224 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4225 | // identifier (and remember where it would have been), then call into |
| 4226 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4227 | D.SetIdentifier(0, Tok.getLocation()); |
| 4228 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4229 | // Enter function-declaration scope, limiting any declarators to the |
| 4230 | // function prototype scope, including parameter declarators. |
| 4231 | ParseScope PrototypeScope(this, |
| 4232 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4233 | ParseFunctionDeclarator(D, attrs, T, RequiresArg); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4234 | PrototypeScope.Exit(); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4235 | } |
| 4236 | |
| 4237 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 4238 | /// declarator D up to a paren, which indicates that we are parsing function |
| 4239 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4240 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4241 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 4242 | /// immediately after the open paren - they should be considered to be the |
| 4243 | /// first argument of a parameter. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4244 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4245 | /// If RequiresArg is true, then the first argument of the function is required |
| 4246 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4247 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4248 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 4249 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 4250 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 4251 | /// |
| 4252 | /// [C++11] exception-specification: |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4253 | /// dynamic-exception-specification |
| 4254 | /// noexcept-specification |
| 4255 | /// |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4256 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4257 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4258 | BalancedDelimiterTracker &Tracker, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4259 | bool RequiresArg) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4260 | assert(getCurScope()->isFunctionPrototypeScope() && |
| 4261 | "Should call from a Function scope"); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4262 | // lparen is already consumed! |
| 4263 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 4264 | |
| 4265 | // This should be true when the function has typed arguments. |
| 4266 | // Otherwise, it is treated as a K&R-style function. |
| 4267 | bool HasProto = false; |
| 4268 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4269 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4270 | // Remember where we see an ellipsis, if any. |
| 4271 | SourceLocation EllipsisLoc; |
| 4272 | |
| 4273 | DeclSpec DS(AttrFactory); |
| 4274 | bool RefQualifierIsLValueRef = true; |
| 4275 | SourceLocation RefQualifierLoc; |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4276 | SourceLocation ConstQualifierLoc; |
| 4277 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4278 | ExceptionSpecificationType ESpecType = EST_None; |
| 4279 | SourceRange ESpecRange; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4280 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 4281 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4282 | ExprResult NoexceptExpr; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4283 | ParsedAttributes FnAttrs(AttrFactory); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4284 | ParsedType TrailingReturnType; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4285 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4286 | Actions.ActOnStartFunctionDeclarator(); |
| 4287 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4288 | SourceLocation EndLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4289 | if (isFunctionDeclaratorIdentifierList()) { |
| 4290 | if (RequiresArg) |
| 4291 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4292 | |
| 4293 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 4294 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4295 | Tracker.consumeClose(); |
| 4296 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4297 | } else { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4298 | if (Tok.isNot(tok::r_paren)) |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4299 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4300 | else if (RequiresArg) |
| 4301 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4302 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4303 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4304 | |
| 4305 | // If we have the closing ')', eat it. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4306 | Tracker.consumeClose(); |
| 4307 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4308 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4309 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4310 | // FIXME: Accept these components in any order, and produce fixits to |
| 4311 | // correct the order if the user gets it wrong. Ideally we should deal |
| 4312 | // with the virt-specifier-seq and pure-specifier in the same way. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4313 | |
| 4314 | // Parse cv-qualifier-seq[opt]. |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4315 | ParseTypeQualifierListOpt(DS, false /*no attributes*/, false); |
| 4316 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 4317 | EndLoc = DS.getSourceRange().getEnd(); |
| 4318 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 4319 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 4320 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4321 | |
| 4322 | // Parse ref-qualifier[opt]. |
| 4323 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4324 | Diag(Tok, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4325 | diag::warn_cxx98_compat_ref_qualifier : |
| 4326 | diag::ext_ref_qualifier); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4327 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4328 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 4329 | RefQualifierLoc = ConsumeToken(); |
| 4330 | EndLoc = RefQualifierLoc; |
| 4331 | } |
| 4332 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4333 | // C++11 [expr.prim.general]p3: |
| 4334 | // If a declaration declares a member function or member function |
| 4335 | // template of a class X, the expression this is a prvalue of type |
| 4336 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
| 4337 | // and the end of the function-definition, member-declarator, or |
| 4338 | // declarator. |
| 4339 | bool IsCXX11MemberFunction = |
| 4340 | getLangOpts().CPlusPlus0x && |
| 4341 | (D.getContext() == Declarator::MemberContext || |
| 4342 | (D.getContext() == Declarator::FileContext && |
| 4343 | D.getCXXScopeSpec().isValid() && |
| 4344 | Actions.CurContext->isRecord())); |
| 4345 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 4346 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
| 4347 | DS.getTypeQualifiers(), |
| 4348 | IsCXX11MemberFunction); |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 4349 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4350 | // Parse exception-specification[opt]. |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 4351 | ESpecType = tryParseExceptionSpecification(ESpecRange, |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 4352 | DynamicExceptions, |
| 4353 | DynamicExceptionRanges, |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 4354 | NoexceptExpr); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4355 | if (ESpecType != EST_None) |
| 4356 | EndLoc = ESpecRange.getEnd(); |
| 4357 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4358 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 4359 | // after the exception-specification. |
| 4360 | MaybeParseCXX0XAttributes(FnAttrs); |
| 4361 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4362 | // Parse trailing-return-type[opt]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4363 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4364 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 4365 | SourceRange Range; |
| 4366 | TrailingReturnType = ParseTrailingReturnType(Range).get(); |
| 4367 | if (Range.getEnd().isValid()) |
| 4368 | EndLoc = Range.getEnd(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4369 | } |
| 4370 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4371 | } |
| 4372 | |
| 4373 | // Remember that we parsed a function type, and remember the attributes. |
| 4374 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
| 4375 | /*isVariadic=*/EllipsisLoc.isValid(), |
| 4376 | EllipsisLoc, |
| 4377 | ParamInfo.data(), ParamInfo.size(), |
| 4378 | DS.getTypeQualifiers(), |
| 4379 | RefQualifierIsLValueRef, |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4380 | RefQualifierLoc, ConstQualifierLoc, |
| 4381 | VolatileQualifierLoc, |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 4382 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4383 | ESpecType, ESpecRange.getBegin(), |
| 4384 | DynamicExceptions.data(), |
| 4385 | DynamicExceptionRanges.data(), |
| 4386 | DynamicExceptions.size(), |
| 4387 | NoexceptExpr.isUsable() ? |
| 4388 | NoexceptExpr.get() : 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4389 | Tracker.getOpenLocation(), |
| 4390 | EndLoc, D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4391 | TrailingReturnType), |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4392 | FnAttrs, EndLoc); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4393 | |
| 4394 | Actions.ActOnEndFunctionDeclarator(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4395 | } |
| 4396 | |
| 4397 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 4398 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 4399 | /// |
| 4400 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 4401 | /// abstract-declarators. |
| 4402 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4403 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4404 | && Tok.is(tok::identifier) |
| 4405 | && !TryAltiVecVectorToken() |
| 4406 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 4407 | // 6.7.5.3p11. |
| 4408 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 4409 | // Identifier lists follow a really simple grammar: the identifiers can |
| 4410 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 4411 | // identifier lists are really rare in the brave new modern world, and |
| 4412 | // it is very common for someone to typo a type in a non-K&R style |
| 4413 | // list. If we are presented with something like: "void foo(intptr x, |
| 4414 | // float y)", we don't want to start parsing the function declarator as |
| 4415 | // though it is a K&R style declarator just because intptr is an |
| 4416 | // invalid type. |
| 4417 | // |
| 4418 | // To handle this, we check to see if the token after the first |
| 4419 | // identifier is a "," or ")". Only then do we parse it as an |
| 4420 | // identifier list. |
| 4421 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 4422 | } |
| 4423 | |
| 4424 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 4425 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 4426 | /// |
| 4427 | /// After returning, ParamInfo will hold the parsed parameters. |
| 4428 | /// |
| 4429 | /// identifier-list: [C99 6.7.5] |
| 4430 | /// identifier |
| 4431 | /// identifier-list ',' identifier |
| 4432 | /// |
| 4433 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 4434 | Declarator &D, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4435 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4436 | // If there was no identifier specified for the declarator, either we are in |
| 4437 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 4438 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 4439 | // diagnose this. |
| 4440 | if (!D.getIdentifier()) |
| 4441 | Diag(Tok, diag::ext_ident_list_in_param); |
| 4442 | |
| 4443 | // Maintain an efficient lookup of params we have seen so far. |
| 4444 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 4445 | |
| 4446 | while (1) { |
| 4447 | // If this isn't an identifier, report the error and skip until ')'. |
| 4448 | if (Tok.isNot(tok::identifier)) { |
| 4449 | Diag(Tok, diag::err_expected_ident); |
| 4450 | SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 4451 | // Forget we parsed anything. |
| 4452 | ParamInfo.clear(); |
| 4453 | return; |
| 4454 | } |
| 4455 | |
| 4456 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 4457 | |
| 4458 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 4459 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 4460 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 4461 | |
| 4462 | // Verify that the argument identifier has not already been mentioned. |
| 4463 | if (!ParamsSoFar.insert(ParmII)) { |
| 4464 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 4465 | } else { |
| 4466 | // Remember this identifier in ParamInfo. |
| 4467 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4468 | Tok.getLocation(), |
| 4469 | 0)); |
| 4470 | } |
| 4471 | |
| 4472 | // Eat the identifier. |
| 4473 | ConsumeToken(); |
| 4474 | |
| 4475 | // The list continues if we see a comma. |
| 4476 | if (Tok.isNot(tok::comma)) |
| 4477 | break; |
| 4478 | ConsumeToken(); |
| 4479 | } |
| 4480 | } |
| 4481 | |
| 4482 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 4483 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 4484 | /// identifier list. |
| 4485 | /// |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4486 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 4487 | /// caller parsed those arguments immediately after the open paren - they should |
| 4488 | /// be considered to be part of the first parameter. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4489 | /// |
| 4490 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 4491 | /// be the location of the ellipsis, if any was parsed. |
| 4492 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4493 | /// parameter-type-list: [C99 6.7.5] |
| 4494 | /// parameter-list |
| 4495 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4496 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4497 | /// |
| 4498 | /// parameter-list: [C99 6.7.5] |
| 4499 | /// parameter-declaration |
| 4500 | /// parameter-list ',' parameter-declaration |
| 4501 | /// |
| 4502 | /// parameter-declaration: [C99 6.7.5] |
| 4503 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4504 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4505 | /// [C++11] initializer-clause |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4506 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 4507 | /// declaration-specifiers abstract-declarator[opt] |
| 4508 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 4509 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4510 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4511 | /// [C++11] attribute-specifier-seq parameter-declaration |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4512 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4513 | void Parser::ParseParameterDeclarationClause( |
| 4514 | Declarator &D, |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4515 | ParsedAttributes &FirstArgAttrs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4516 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4517 | SourceLocation &EllipsisLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4518 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4519 | while (1) { |
| 4520 | if (Tok.is(tok::ellipsis)) { |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4521 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 4522 | // before deciding this was a parameter-declaration-clause. |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 4523 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4524 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4525 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4526 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4527 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4528 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4529 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4530 | |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4531 | // Parse any C++11 attributes. |
| 4532 | MaybeParseCXX0XAttributes(DS.getAttributes()); |
| 4533 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4534 | // Skip any Microsoft attributes before a param. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4535 | if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4536 | ParseMicrosoftAttributes(DS.getAttributes()); |
| 4537 | |
| 4538 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4539 | |
| 4540 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4541 | // 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] | 4542 | // 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] | 4543 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 4544 | // too much hassle. |
| 4545 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4546 | |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4547 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4548 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4549 | // Parse the declarator. This is "PrototypeContext", because we must |
| 4550 | // accept either 'declarator' or 'abstract-declarator' here. |
| 4551 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 4552 | ParseDeclarator(ParmDecl); |
| 4553 | |
| 4554 | // Parse GNU attributes, if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4555 | MaybeParseGNUAttributes(ParmDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4556 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4557 | // Remember this parsed parameter in ParamInfo. |
| 4558 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4559 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4560 | // DefArgToks is used when the parsing of default arguments needs |
| 4561 | // to be delayed. |
| 4562 | CachedTokens *DefArgToks = 0; |
| 4563 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4564 | // If no parameter was specified, verify that *something* was specified, |
| 4565 | // otherwise we have a missing type and identifier. |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4566 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 4567 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4568 | // Completely missing, emit error. |
| 4569 | Diag(DSStart, diag::err_missing_param); |
| 4570 | } else { |
| 4571 | // Otherwise, we have something. Add it and let semantic analysis try |
| 4572 | // 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] | 4573 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4574 | // Inform the actions module about the parameter declarator, so it gets |
| 4575 | // added to the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4576 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4577 | |
| 4578 | // Parse the default argument, if any. We parse the default |
| 4579 | // arguments in all dialects; the semantic analysis in |
| 4580 | // ActOnParamDefaultArgument will reject the default argument in |
| 4581 | // C. |
| 4582 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4583 | SourceLocation EqualLoc = Tok.getLocation(); |
| 4584 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4585 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4586 | if (D.getContext() == Declarator::MemberContext) { |
| 4587 | // If we're inside a class definition, cache the tokens |
| 4588 | // corresponding to the default argument. We'll actually parse |
| 4589 | // them when we see the end of the class definition. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4590 | // FIXME: Can we use a smart pointer for Toks? |
| 4591 | DefArgToks = new CachedTokens; |
| 4592 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4593 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 4594 | /*StopAtSemi=*/true, |
| 4595 | /*ConsumeFinalToken=*/false)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4596 | delete DefArgToks; |
| 4597 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4598 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4599 | } else { |
| 4600 | // Mark the end of the default argument so that we know when to |
| 4601 | // stop when we parse it later on. |
| 4602 | Token DefArgEnd; |
| 4603 | DefArgEnd.startToken(); |
| 4604 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 4605 | DefArgEnd.setLocation(Tok.getLocation()); |
| 4606 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4607 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4608 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4609 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4610 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4611 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4612 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4613 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4614 | // The argument isn't actually potentially evaluated unless it is |
| 4615 | // used. |
| 4616 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 4617 | Sema::PotentiallyEvaluatedIfUsed, |
| 4618 | Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4619 | |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4620 | ExprResult DefArgResult; |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 4621 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) { |
| 4622 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4623 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 4624 | } else |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4625 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4626 | if (DefArgResult.isInvalid()) { |
| 4627 | Actions.ActOnParamDefaultArgumentError(Param); |
| 4628 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 4629 | } else { |
| 4630 | // Inform the actions module about the default argument |
| 4631 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4632 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4633 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4634 | } |
| 4635 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4636 | |
| 4637 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4638 | ParmDecl.getIdentifierLoc(), Param, |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4639 | DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4640 | } |
| 4641 | |
| 4642 | // 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] | 4643 | if (Tok.isNot(tok::comma)) { |
| 4644 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4645 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
| 4646 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4647 | if (!getLangOpts().CPlusPlus) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4648 | // We have ellipsis without a preceding ',', which is ill-formed |
| 4649 | // in C. Complain and provide the fix. |
| 4650 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4651 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4652 | } |
| 4653 | } |
| 4654 | |
| 4655 | break; |
| 4656 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4657 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4658 | // Consume the comma. |
| 4659 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4660 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4661 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 4662 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4663 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4664 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4665 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4666 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4667 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4668 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4669 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 4670 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4671 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4672 | if (CheckProhibitedCXX11Attribute()) |
| 4673 | return; |
| 4674 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4675 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 4676 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4677 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4678 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 4679 | // This code does a fast path to handle some of the most obvious cases. |
| 4680 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4681 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4682 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4683 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4684 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4685 | // Remember that we parsed the empty array type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4686 | ExprResult NumElements; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4687 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4688 | T.getOpenLocation(), |
| 4689 | T.getCloseLocation()), |
| 4690 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4691 | return; |
| 4692 | } else if (Tok.getKind() == tok::numeric_constant && |
| 4693 | GetLookAheadToken(1).is(tok::r_square)) { |
| 4694 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 4695 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4696 | ConsumeToken(); |
| 4697 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4698 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4699 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4700 | MaybeParseCXX0XAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4701 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4702 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4703 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4704 | ExprRes.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4705 | T.getOpenLocation(), |
| 4706 | T.getCloseLocation()), |
| 4707 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4708 | return; |
| 4709 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4710 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4711 | // If valid, this location is the position where we read the 'static' keyword. |
| 4712 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4713 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4714 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4715 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4716 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4717 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4718 | DeclSpec DS(AttrFactory); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4719 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4720 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4721 | // If we haven't already read 'static', check to see if there is one after the |
| 4722 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4723 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4724 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4725 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4726 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 4727 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4728 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4729 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4730 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 4731 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
| 4732 | // the the token after the star is a ']'. Since stars in arrays are |
| 4733 | // infrequent, use of lookahead is not costly here. |
| 4734 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 4735 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4736 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4737 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4738 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4739 | StaticLoc = SourceLocation(); // Drop the static. |
| 4740 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4741 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4742 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4743 | // Note, in C89, this production uses the constant-expr production instead |
| 4744 | // of assignment-expr. The only difference is that assignment-expr allows |
| 4745 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 4746 | // 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] | 4747 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4748 | // Parse the constant-expression or assignment-expression now (depending |
| 4749 | // on dialect). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4750 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4751 | NumElements = ParseConstantExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4752 | } else { |
| 4753 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 4754 | Sema::ConstantEvaluated); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4755 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4756 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4757 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4758 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4759 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 4760 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 4761 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4762 | // If the expression was invalid, skip it. |
| 4763 | SkipUntil(tok::r_square); |
| 4764 | return; |
| 4765 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4766 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4767 | T.consumeClose(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4768 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4769 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4770 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4771 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4772 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4773 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4774 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4775 | NumElements.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4776 | T.getOpenLocation(), |
| 4777 | T.getCloseLocation()), |
| 4778 | attrs, T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4779 | } |
| 4780 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4781 | /// [GNU] typeof-specifier: |
| 4782 | /// typeof ( expressions ) |
| 4783 | /// typeof ( type-name ) |
| 4784 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4785 | /// |
| 4786 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4787 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4788 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4789 | SourceLocation StartLoc = ConsumeToken(); |
| 4790 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 4791 | const bool hasParens = Tok.is(tok::l_paren); |
| 4792 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4793 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
| 4794 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4795 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4796 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4797 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4798 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 4799 | CastTy, CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 4800 | if (hasParens) |
| 4801 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4802 | |
| 4803 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4804 | // FIXME: Not accurate, the range gets one token more than it should. |
| 4805 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4806 | else |
| 4807 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4808 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4809 | if (isCastExpr) { |
| 4810 | if (!CastTy) { |
| 4811 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4812 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 4813 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4814 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4815 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4816 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4817 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 4818 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4819 | DiagID, CastTy)) |
| 4820 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4821 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4822 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4823 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4824 | // If we get here, the operand to the typeof was an expresion. |
| 4825 | if (Operand.isInvalid()) { |
| 4826 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 4827 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4828 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4829 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4830 | // We might need to transform the operand if it is potentially evaluated. |
| 4831 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 4832 | if (Operand.isInvalid()) { |
| 4833 | DS.SetTypeSpecError(); |
| 4834 | return; |
| 4835 | } |
| 4836 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4837 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4838 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4839 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 4840 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4841 | DiagID, Operand.get())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4842 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4843 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4844 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 4845 | /// [C11] atomic-specifier: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4846 | /// _Atomic ( type-name ) |
| 4847 | /// |
| 4848 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
| 4849 | assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier"); |
| 4850 | |
| 4851 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4852 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4853 | if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) { |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4854 | SkipUntil(tok::r_paren); |
| 4855 | return; |
| 4856 | } |
| 4857 | |
| 4858 | TypeResult Result = ParseTypeName(); |
| 4859 | if (Result.isInvalid()) { |
| 4860 | SkipUntil(tok::r_paren); |
| 4861 | return; |
| 4862 | } |
| 4863 | |
| 4864 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4865 | T.consumeClose(); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4866 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4867 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4868 | return; |
| 4869 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4870 | DS.setTypeofParensRange(T.getRange()); |
| 4871 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4872 | |
| 4873 | const char *PrevSpec = 0; |
| 4874 | unsigned DiagID; |
| 4875 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
| 4876 | DiagID, Result.release())) |
| 4877 | Diag(StartLoc, DiagID) << PrevSpec; |
| 4878 | } |
| 4879 | |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4880 | |
| 4881 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 4882 | /// from TryAltiVecVectorToken. |
| 4883 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 4884 | Token Next = NextToken(); |
| 4885 | switch (Next.getKind()) { |
| 4886 | default: return false; |
| 4887 | case tok::kw_short: |
| 4888 | case tok::kw_long: |
| 4889 | case tok::kw_signed: |
| 4890 | case tok::kw_unsigned: |
| 4891 | case tok::kw_void: |
| 4892 | case tok::kw_char: |
| 4893 | case tok::kw_int: |
| 4894 | case tok::kw_float: |
| 4895 | case tok::kw_double: |
| 4896 | case tok::kw_bool: |
| 4897 | case tok::kw___pixel: |
| 4898 | Tok.setKind(tok::kw___vector); |
| 4899 | return true; |
| 4900 | case tok::identifier: |
| 4901 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 4902 | Tok.setKind(tok::kw___vector); |
| 4903 | return true; |
| 4904 | } |
| 4905 | return false; |
| 4906 | } |
| 4907 | } |
| 4908 | |
| 4909 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 4910 | const char *&PrevSpec, unsigned &DiagID, |
| 4911 | bool &isInvalid) { |
| 4912 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 4913 | Token Next = NextToken(); |
| 4914 | switch (Next.getKind()) { |
| 4915 | case tok::kw_short: |
| 4916 | case tok::kw_long: |
| 4917 | case tok::kw_signed: |
| 4918 | case tok::kw_unsigned: |
| 4919 | case tok::kw_void: |
| 4920 | case tok::kw_char: |
| 4921 | case tok::kw_int: |
| 4922 | case tok::kw_float: |
| 4923 | case tok::kw_double: |
| 4924 | case tok::kw_bool: |
| 4925 | case tok::kw___pixel: |
| 4926 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 4927 | return true; |
| 4928 | case tok::identifier: |
| 4929 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 4930 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 4931 | return true; |
| 4932 | } |
| 4933 | break; |
| 4934 | default: |
| 4935 | break; |
| 4936 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 4937 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4938 | DS.isTypeAltiVecVector()) { |
| 4939 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 4940 | return true; |
| 4941 | } |
| 4942 | return false; |
| 4943 | } |