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 | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 41 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 42 | // Parse the common declaration-specifiers piece. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 43 | DeclSpec DS(AttrFactory); |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 44 | ParseSpecifierQualifierList(DS, AS, DSC); |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 45 | if (OwnedType) |
| 46 | *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0; |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 47 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 48 | // Parse the abstract-declarator, if present. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 49 | Declarator DeclaratorInfo(DS, Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 50 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 51 | if (Range) |
| 52 | *Range = DeclaratorInfo.getSourceRange(); |
| 53 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 54 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 55 | return true; |
| 56 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 57 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 60 | |
| 61 | /// isAttributeLateParsed - Return true if the attribute has arguments that |
| 62 | /// require late parsing. |
| 63 | static bool isAttributeLateParsed(const IdentifierInfo &II) { |
| 64 | return llvm::StringSwitch<bool>(II.getName()) |
| 65 | #include "clang/Parse/AttrLateParsed.inc" |
| 66 | .Default(false); |
| 67 | } |
| 68 | |
| 69 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 70 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 71 | /// |
| 72 | /// [GNU] attributes: |
| 73 | /// attribute |
| 74 | /// attributes attribute |
| 75 | /// |
| 76 | /// [GNU] attribute: |
| 77 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 78 | /// |
| 79 | /// [GNU] attribute-list: |
| 80 | /// attrib |
| 81 | /// attribute_list ',' attrib |
| 82 | /// |
| 83 | /// [GNU] attrib: |
| 84 | /// empty |
| 85 | /// attrib-name |
| 86 | /// attrib-name '(' identifier ')' |
| 87 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 88 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 89 | /// |
| 90 | /// [GNU] attrib-name: |
| 91 | /// identifier |
| 92 | /// typespec |
| 93 | /// typequal |
| 94 | /// storageclass |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 95 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 96 | /// FIXME: The GCC grammar/code for this construct implies we need two |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 98 | /// which is followed by a comma or close parenthesis, then the arguments |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 99 | /// start with that identifier; otherwise they are an expression list." |
| 100 | /// |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 101 | /// GCC does not require the ',' between attribs in an attribute-list. |
| 102 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 103 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 104 | /// any attributes that don't work (based on my limited testing). Most |
| 105 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 106 | /// a pressing need to implement the 2 token lookahead. |
| 107 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 108 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 109 | SourceLocation *endLoc, |
| 110 | LateParsedAttrList *LateAttrs) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 111 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 112 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 113 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 114 | ConsumeToken(); |
| 115 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 116 | "attribute")) { |
| 117 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 118 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 119 | } |
| 120 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 121 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 122 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 123 | } |
| 124 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 125 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 126 | Tok.is(tok::comma)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 127 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 128 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 129 | ConsumeToken(); |
| 130 | continue; |
| 131 | } |
| 132 | // we have an identifier or declaration specifier (const, int, etc.) |
| 133 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 134 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 135 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 136 | if (Tok.is(tok::l_paren)) { |
| 137 | // handle "parameterized" attributes |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 138 | if (LateAttrs && isAttributeLateParsed(*AttrName)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 139 | LateParsedAttribute *LA = |
| 140 | new LateParsedAttribute(this, *AttrName, AttrNameLoc); |
| 141 | LateAttrs->push_back(LA); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 142 | |
| 143 | // Attributes in a class are parsed at the end of the class, along |
| 144 | // with other late-parsed declarations. |
| 145 | if (!ClassStack.empty()) |
| 146 | getCurrentClass().LateParsedDeclarations.push_back(LA); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 148 | // consume everything up to and including the matching right parens |
| 149 | ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 151 | Token Eof; |
| 152 | Eof.startToken(); |
| 153 | Eof.setLocation(Tok.getLocation()); |
| 154 | LA->Toks.push_back(Eof); |
| 155 | } else { |
| 156 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 157 | } |
| 158 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 159 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 160 | 0, SourceLocation(), 0, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 164 | SkipUntil(tok::r_paren, false); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 165 | SourceLocation Loc = Tok.getLocation(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 166 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 167 | SkipUntil(tok::r_paren, false); |
| 168 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 169 | if (endLoc) |
| 170 | *endLoc = Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 171 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 174 | |
| 175 | /// Parse the arguments to a parameterized GNU attribute |
| 176 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 177 | SourceLocation AttrNameLoc, |
| 178 | ParsedAttributes &Attrs, |
| 179 | SourceLocation *EndLoc) { |
| 180 | |
| 181 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 182 | |
| 183 | // Availability attributes have their own grammar. |
| 184 | if (AttrName->isStr("availability")) { |
| 185 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 186 | return; |
| 187 | } |
| 188 | // Thread safety attributes fit into the FIXME case above, so we |
| 189 | // just parse the arguments as a list of expressions |
| 190 | if (IsThreadSafetyAttribute(AttrName->getName())) { |
| 191 | ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | ConsumeParen(); // ignore the left paren loc for now |
| 196 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 197 | IdentifierInfo *ParmName = 0; |
| 198 | SourceLocation ParmLoc; |
| 199 | bool BuiltinType = false; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 200 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 201 | switch (Tok.getKind()) { |
| 202 | case tok::kw_char: |
| 203 | case tok::kw_wchar_t: |
| 204 | case tok::kw_char16_t: |
| 205 | case tok::kw_char32_t: |
| 206 | case tok::kw_bool: |
| 207 | case tok::kw_short: |
| 208 | case tok::kw_int: |
| 209 | case tok::kw_long: |
| 210 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 211 | case tok::kw___int128: |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 212 | case tok::kw_signed: |
| 213 | case tok::kw_unsigned: |
| 214 | case tok::kw_float: |
| 215 | case tok::kw_double: |
| 216 | case tok::kw_void: |
| 217 | case tok::kw_typeof: |
| 218 | // __attribute__(( vec_type_hint(char) )) |
| 219 | // FIXME: Don't just discard the builtin type token. |
| 220 | ConsumeToken(); |
| 221 | BuiltinType = true; |
| 222 | break; |
| 223 | |
| 224 | case tok::identifier: |
| 225 | ParmName = Tok.getIdentifierInfo(); |
| 226 | ParmLoc = ConsumeToken(); |
| 227 | break; |
| 228 | |
| 229 | default: |
| 230 | break; |
| 231 | } |
| 232 | |
| 233 | ExprVector ArgExprs(Actions); |
| 234 | |
| 235 | if (!BuiltinType && |
| 236 | (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) { |
| 237 | // Eat the comma. |
| 238 | if (ParmLoc.isValid()) |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 239 | ConsumeToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 240 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 241 | // Parse the non-empty comma-separated list of expressions. |
| 242 | while (1) { |
| 243 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 244 | if (ArgExpr.isInvalid()) { |
| 245 | SkipUntil(tok::r_paren); |
| 246 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 247 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 248 | ArgExprs.push_back(ArgExpr.release()); |
| 249 | if (Tok.isNot(tok::comma)) |
| 250 | break; |
| 251 | ConsumeToken(); // Eat the comma, move to the next argument |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 252 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 253 | } |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 254 | else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) { |
| 255 | if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<", |
| 256 | tok::greater)) { |
Fariborz Jahanian | b224343 | 2011-10-18 23:13:50 +0000 | [diff] [blame] | 257 | while (Tok.is(tok::identifier)) { |
| 258 | ConsumeToken(); |
| 259 | if (Tok.is(tok::greater)) |
| 260 | break; |
| 261 | if (Tok.is(tok::comma)) { |
| 262 | ConsumeToken(); |
| 263 | continue; |
| 264 | } |
| 265 | } |
| 266 | if (Tok.isNot(tok::greater)) |
| 267 | Diag(Tok, diag::err_iboutletcollection_with_protocol); |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 268 | SkipUntil(tok::r_paren, false, true); // skip until ')' |
| 269 | } |
| 270 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 271 | |
| 272 | SourceLocation RParen = Tok.getLocation(); |
| 273 | if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 274 | AttributeList *attr = |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 275 | Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc, |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 276 | ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size()); |
Michael Han | e53ac8a | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 277 | if (BuiltinType && attr->getKind() == AttributeList::AT_iboutletcollection) |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 278 | Diag(Tok, diag::err_iboutletcollection_builtintype); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
| 282 | |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 283 | /// ParseMicrosoftDeclSpec - Parse an __declspec construct |
| 284 | /// |
| 285 | /// [MS] decl-specifier: |
| 286 | /// __declspec ( extended-decl-modifier-seq ) |
| 287 | /// |
| 288 | /// [MS] extended-decl-modifier-seq: |
| 289 | /// extended-decl-modifier[opt] |
| 290 | /// extended-decl-modifier extended-decl-modifier-seq |
| 291 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 292 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) { |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 293 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 294 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 295 | ConsumeToken(); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 296 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 297 | "declspec")) { |
| 298 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 299 | return; |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 300 | } |
Francois Pichet | 373197b | 2011-05-07 19:04:49 +0000 | [diff] [blame] | 301 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 302 | while (Tok.getIdentifierInfo()) { |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 303 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 304 | SourceLocation AttrNameLoc = ConsumeToken(); |
Francois Pichet | 373197b | 2011-05-07 19:04:49 +0000 | [diff] [blame] | 305 | |
| 306 | // FIXME: Remove this when we have proper __declspec(property()) support. |
| 307 | // Just skip everything inside property(). |
| 308 | if (AttrName->getName() == "property") { |
| 309 | ConsumeParen(); |
| 310 | SkipUntil(tok::r_paren); |
| 311 | } |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 312 | if (Tok.is(tok::l_paren)) { |
| 313 | ConsumeParen(); |
| 314 | // FIXME: This doesn't parse __declspec(property(get=get_func_name)) |
| 315 | // correctly. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 316 | ExprResult ArgExpr(ParseAssignmentExpression()); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 317 | if (!ArgExpr.isInvalid()) { |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 318 | Expr *ExprList = ArgExpr.take(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 319 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 320 | SourceLocation(), &ExprList, 1, true); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 321 | } |
| 322 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 323 | SkipUntil(tok::r_paren, false); |
| 324 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 325 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 326 | 0, SourceLocation(), 0, 0, true); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 327 | } |
| 328 | } |
| 329 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 330 | SkipUntil(tok::r_paren, false); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 331 | return; |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 332 | } |
| 333 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 334 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 335 | // Treat these like attributes |
| 336 | // FIXME: Allow Sema to distinguish between these and real attributes! |
| 337 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 338 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 339 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 340 | Tok.is(tok::kw___ptr32) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 341 | Tok.is(tok::kw___unaligned)) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 342 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 343 | SourceLocation AttrNameLoc = ConsumeToken(); |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 344 | if (Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
| 345 | Tok.is(tok::kw___ptr32)) |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 346 | // FIXME: Support these properly! |
| 347 | continue; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 348 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 349 | SourceLocation(), 0, 0, true); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 350 | } |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 351 | } |
| 352 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 353 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 354 | // Treat these like attributes |
| 355 | while (Tok.is(tok::kw___pascal)) { |
| 356 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 357 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 358 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 359 | SourceLocation(), 0, 0, true); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 360 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 363 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 364 | // Treat these like attributes |
| 365 | while (Tok.is(tok::kw___kernel)) { |
| 366 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 367 | attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"), |
| 368 | AttrNameLoc, 0, AttrNameLoc, 0, |
| 369 | SourceLocation(), 0, 0, false); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 373 | void Parser::ParseOpenCLQualifiers(DeclSpec &DS) { |
| 374 | SourceLocation Loc = Tok.getLocation(); |
| 375 | switch(Tok.getKind()) { |
| 376 | // OpenCL qualifiers: |
| 377 | case tok::kw___private: |
| 378 | case tok::kw_private: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 379 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 380 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 381 | PP.getIdentifierInfo("address_space"), Loc, 0); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 382 | break; |
| 383 | |
| 384 | case tok::kw___global: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 385 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 386 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 387 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 388 | break; |
| 389 | |
| 390 | case tok::kw___local: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 391 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 392 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 393 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 394 | break; |
| 395 | |
| 396 | case tok::kw___constant: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 397 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 398 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 399 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 400 | break; |
| 401 | |
| 402 | case tok::kw___read_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 403 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 404 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 405 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 406 | break; |
| 407 | |
| 408 | case tok::kw___write_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 409 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 410 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 411 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 412 | break; |
| 413 | |
| 414 | case tok::kw___read_write: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 415 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 416 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 417 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 418 | break; |
| 419 | default: break; |
| 420 | } |
| 421 | } |
| 422 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 423 | /// \brief Parse a version number. |
| 424 | /// |
| 425 | /// version: |
| 426 | /// simple-integer |
| 427 | /// simple-integer ',' simple-integer |
| 428 | /// simple-integer ',' simple-integer ',' simple-integer |
| 429 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 430 | Range = Tok.getLocation(); |
| 431 | |
| 432 | if (!Tok.is(tok::numeric_constant)) { |
| 433 | Diag(Tok, diag::err_expected_version); |
| 434 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 435 | return VersionTuple(); |
| 436 | } |
| 437 | |
| 438 | // Parse the major (and possibly minor and subminor) versions, which |
| 439 | // are stored in the numeric constant. We utilize a quirk of the |
| 440 | // lexer, which is that it handles something like 1.2.3 as a single |
| 441 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 442 | SmallString<512> Buffer; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 443 | Buffer.resize(Tok.getLength()+1); |
| 444 | const char *ThisTokBegin = &Buffer[0]; |
| 445 | |
| 446 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 447 | bool Invalid = false; |
| 448 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 449 | if (Invalid) |
| 450 | return VersionTuple(); |
| 451 | |
| 452 | // Parse the major version. |
| 453 | unsigned AfterMajor = 0; |
| 454 | unsigned Major = 0; |
| 455 | while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) { |
| 456 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 457 | ++AfterMajor; |
| 458 | } |
| 459 | |
| 460 | if (AfterMajor == 0) { |
| 461 | Diag(Tok, diag::err_expected_version); |
| 462 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 463 | return VersionTuple(); |
| 464 | } |
| 465 | |
| 466 | if (AfterMajor == ActualLength) { |
| 467 | ConsumeToken(); |
| 468 | |
| 469 | // We only had a single version component. |
| 470 | if (Major == 0) { |
| 471 | Diag(Tok, diag::err_zero_version); |
| 472 | return VersionTuple(); |
| 473 | } |
| 474 | |
| 475 | return VersionTuple(Major); |
| 476 | } |
| 477 | |
| 478 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 479 | Diag(Tok, diag::err_expected_version); |
| 480 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 481 | return VersionTuple(); |
| 482 | } |
| 483 | |
| 484 | // Parse the minor version. |
| 485 | unsigned AfterMinor = AfterMajor + 1; |
| 486 | unsigned Minor = 0; |
| 487 | while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) { |
| 488 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 489 | ++AfterMinor; |
| 490 | } |
| 491 | |
| 492 | if (AfterMinor == ActualLength) { |
| 493 | ConsumeToken(); |
| 494 | |
| 495 | // We had major.minor. |
| 496 | if (Major == 0 && Minor == 0) { |
| 497 | Diag(Tok, diag::err_zero_version); |
| 498 | return VersionTuple(); |
| 499 | } |
| 500 | |
| 501 | return VersionTuple(Major, Minor); |
| 502 | } |
| 503 | |
| 504 | // If what follows is not a '.', we have a problem. |
| 505 | if (ThisTokBegin[AfterMinor] != '.') { |
| 506 | Diag(Tok, diag::err_expected_version); |
| 507 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 508 | return VersionTuple(); |
| 509 | } |
| 510 | |
| 511 | // Parse the subminor version. |
| 512 | unsigned AfterSubminor = AfterMinor + 1; |
| 513 | unsigned Subminor = 0; |
| 514 | while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) { |
| 515 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 516 | ++AfterSubminor; |
| 517 | } |
| 518 | |
| 519 | if (AfterSubminor != ActualLength) { |
| 520 | Diag(Tok, diag::err_expected_version); |
| 521 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 522 | return VersionTuple(); |
| 523 | } |
| 524 | ConsumeToken(); |
| 525 | return VersionTuple(Major, Minor, Subminor); |
| 526 | } |
| 527 | |
| 528 | /// \brief Parse the contents of the "availability" attribute. |
| 529 | /// |
| 530 | /// availability-attribute: |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 531 | /// 'availability' '(' platform ',' version-arg-list, opt-message')' |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 532 | /// |
| 533 | /// platform: |
| 534 | /// identifier |
| 535 | /// |
| 536 | /// version-arg-list: |
| 537 | /// version-arg |
| 538 | /// version-arg ',' version-arg-list |
| 539 | /// |
| 540 | /// version-arg: |
| 541 | /// 'introduced' '=' version |
| 542 | /// 'deprecated' '=' version |
Douglas Gregor | 93a7067 | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 543 | /// 'obsoleted' = version |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 544 | /// 'unavailable' |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 545 | /// opt-message: |
| 546 | /// 'message' '=' <string> |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 547 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 548 | SourceLocation AvailabilityLoc, |
| 549 | ParsedAttributes &attrs, |
| 550 | SourceLocation *endLoc) { |
| 551 | SourceLocation PlatformLoc; |
| 552 | IdentifierInfo *Platform = 0; |
| 553 | |
| 554 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 555 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 556 | ExprResult MessageExpr; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 557 | |
| 558 | // Opening '('. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 559 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 560 | if (T.consumeOpen()) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 561 | Diag(Tok, diag::err_expected_lparen); |
| 562 | return; |
| 563 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 564 | |
| 565 | // Parse the platform name, |
| 566 | if (Tok.isNot(tok::identifier)) { |
| 567 | Diag(Tok, diag::err_availability_expected_platform); |
| 568 | SkipUntil(tok::r_paren); |
| 569 | return; |
| 570 | } |
| 571 | Platform = Tok.getIdentifierInfo(); |
| 572 | PlatformLoc = ConsumeToken(); |
| 573 | |
| 574 | // Parse the ',' following the platform name. |
| 575 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren)) |
| 576 | return; |
| 577 | |
| 578 | // If we haven't grabbed the pointers for the identifiers |
| 579 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 580 | if (!Ident_introduced) { |
| 581 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 582 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 583 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 584 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 585 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 589 | SourceLocation UnavailableLoc; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 590 | do { |
| 591 | if (Tok.isNot(tok::identifier)) { |
| 592 | Diag(Tok, diag::err_availability_expected_change); |
| 593 | SkipUntil(tok::r_paren); |
| 594 | return; |
| 595 | } |
| 596 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 597 | SourceLocation KeywordLoc = ConsumeToken(); |
| 598 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 599 | if (Keyword == Ident_unavailable) { |
| 600 | if (UnavailableLoc.isValid()) { |
| 601 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 602 | << Keyword << SourceRange(UnavailableLoc); |
| 603 | } |
| 604 | UnavailableLoc = KeywordLoc; |
| 605 | |
| 606 | if (Tok.isNot(tok::comma)) |
| 607 | break; |
| 608 | |
| 609 | ConsumeToken(); |
| 610 | continue; |
| 611 | } |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 612 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 613 | if (Tok.isNot(tok::equal)) { |
| 614 | Diag(Tok, diag::err_expected_equal_after) |
| 615 | << Keyword; |
| 616 | SkipUntil(tok::r_paren); |
| 617 | return; |
| 618 | } |
| 619 | ConsumeToken(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 620 | if (Keyword == Ident_message) { |
| 621 | if (!isTokenStringLiteral()) { |
| 622 | Diag(Tok, diag::err_expected_string_literal); |
| 623 | SkipUntil(tok::r_paren); |
| 624 | return; |
| 625 | } |
| 626 | MessageExpr = ParseStringLiteralExpression(); |
| 627 | break; |
| 628 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 629 | |
| 630 | SourceRange VersionRange; |
| 631 | VersionTuple Version = ParseVersionTuple(VersionRange); |
| 632 | |
| 633 | if (Version.empty()) { |
| 634 | SkipUntil(tok::r_paren); |
| 635 | return; |
| 636 | } |
| 637 | |
| 638 | unsigned Index; |
| 639 | if (Keyword == Ident_introduced) |
| 640 | Index = Introduced; |
| 641 | else if (Keyword == Ident_deprecated) |
| 642 | Index = Deprecated; |
| 643 | else if (Keyword == Ident_obsoleted) |
| 644 | Index = Obsoleted; |
| 645 | else |
| 646 | Index = Unknown; |
| 647 | |
| 648 | if (Index < Unknown) { |
| 649 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 650 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 651 | << Keyword |
| 652 | << SourceRange(Changes[Index].KeywordLoc, |
| 653 | Changes[Index].VersionRange.getEnd()); |
| 654 | } |
| 655 | |
| 656 | Changes[Index].KeywordLoc = KeywordLoc; |
| 657 | Changes[Index].Version = Version; |
| 658 | Changes[Index].VersionRange = VersionRange; |
| 659 | } else { |
| 660 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 661 | << Keyword << VersionRange; |
| 662 | } |
| 663 | |
| 664 | if (Tok.isNot(tok::comma)) |
| 665 | break; |
| 666 | |
| 667 | ConsumeToken(); |
| 668 | } while (true); |
| 669 | |
| 670 | // Closing ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 671 | if (T.consumeClose()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 672 | return; |
| 673 | |
| 674 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 675 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 676 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 677 | // The 'unavailable' availability cannot be combined with any other |
| 678 | // availability changes. Make sure that hasn't happened. |
| 679 | if (UnavailableLoc.isValid()) { |
| 680 | bool Complained = false; |
| 681 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 682 | if (Changes[Index].KeywordLoc.isValid()) { |
| 683 | if (!Complained) { |
| 684 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 685 | << SourceRange(Changes[Index].KeywordLoc, |
| 686 | Changes[Index].VersionRange.getEnd()); |
| 687 | Complained = true; |
| 688 | } |
| 689 | |
| 690 | // Clear out the availability. |
| 691 | Changes[Index] = AvailabilityChange(); |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 696 | // Record this attribute |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 697 | attrs.addNew(&Availability, |
| 698 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Fariborz Jahanian | f96708d | 2012-01-23 23:38:32 +0000 | [diff] [blame] | 699 | 0, AvailabilityLoc, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 700 | Platform, PlatformLoc, |
| 701 | Changes[Introduced], |
| 702 | Changes[Deprecated], |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 703 | Changes[Obsoleted], |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 704 | UnavailableLoc, MessageExpr.take(), |
| 705 | false, false); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 708 | |
| 709 | // Late Parsed Attributes: |
| 710 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 711 | |
| 712 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 713 | |
| 714 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 715 | Self->ParseLexedAttributes(*Class); |
| 716 | } |
| 717 | |
| 718 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 719 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 723 | /// scope appropriately. |
| 724 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 725 | // Deal with templates |
| 726 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 727 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 728 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 729 | HasTemplateScope); |
| 730 | if (HasTemplateScope) |
| 731 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 732 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 733 | // Set or update the scope flags. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 734 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 735 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 736 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 737 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 738 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 739 | // Enter the scope of nested classes |
| 740 | if (!AlreadyHasClassScope) |
| 741 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 742 | Class.TagOrTemplate); |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 743 | { |
| 744 | // Allow 'this' within late-parsed attributes. |
| 745 | Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate, |
| 746 | /*TypeQuals=*/0); |
| 747 | |
| 748 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 749 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 750 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 751 | } |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 752 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 753 | if (!AlreadyHasClassScope) |
| 754 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 755 | Class.TagOrTemplate); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 756 | } |
| 757 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 758 | |
| 759 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 760 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 761 | bool EnterScope, bool OnDefinition) { |
| 762 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 763 | LAs[i]->addDecl(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 764 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | d306cf7 | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 765 | delete LAs[i]; |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 766 | } |
| 767 | LAs.clear(); |
| 768 | } |
| 769 | |
| 770 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 771 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 772 | /// This will be called at the end of parsing a class declaration |
| 773 | /// for each LateParsedAttribute. We consume the saved tokens and |
| 774 | /// create an attribute with the arguments filled in. We add this |
| 775 | /// to the Attribute list for the decl. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 776 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 777 | bool EnterScope, bool OnDefinition) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 778 | // Save the current token position. |
| 779 | SourceLocation OrigLoc = Tok.getLocation(); |
| 780 | |
| 781 | // Append the current token at the end of the new token stream so that it |
| 782 | // doesn't get lost. |
| 783 | LA.Toks.push_back(Tok); |
| 784 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 785 | // Consume the previously pushed token. |
| 786 | ConsumeAnyToken(); |
| 787 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 788 | if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) { |
| 789 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 790 | << LA.AttrName.getName(); |
| 791 | } |
| 792 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 793 | ParsedAttributes Attrs(AttrFactory); |
| 794 | SourceLocation endLoc; |
| 795 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 796 | if (LA.Decls.size() == 1) { |
| 797 | Decl *D = LA.Decls[0]; |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 798 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 799 | // If the Decl is templatized, add template parameters to scope. |
| 800 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 801 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 802 | if (HasTemplateScope) |
| 803 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 804 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 805 | // If the Decl is on a function, add function parameters to the scope. |
| 806 | bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
| 807 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope); |
| 808 | if (HasFunctionScope) |
| 809 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
| 810 | |
| 811 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc); |
| 812 | |
| 813 | if (HasFunctionScope) { |
| 814 | Actions.ActOnExitFunctionContext(); |
| 815 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 816 | } |
| 817 | if (HasTemplateScope) { |
| 818 | TempScope.Exit(); |
| 819 | } |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 820 | } else if (LA.Decls.size() > 0) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 821 | // If there are multiple decls, then the decl cannot be within the |
| 822 | // function scope. |
| 823 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc); |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 824 | } else { |
| 825 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 826 | } |
| 827 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 828 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) { |
| 829 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
| 830 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 831 | |
| 832 | if (Tok.getLocation() != OrigLoc) { |
| 833 | // Due to a parsing error, we either went over the cached tokens or |
| 834 | // there are still cached tokens left, so we skip the leftover tokens. |
| 835 | // Since this is an uncommon situation that should be avoided, use the |
| 836 | // expensive isBeforeInTranslationUnit call. |
| 837 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 838 | OrigLoc)) |
| 839 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
Douglas Gregor | d78ef5b | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 840 | ConsumeAnyToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 841 | } |
| 842 | } |
| 843 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 844 | /// \brief Wrapper around a case statement checking if AttrName is |
| 845 | /// one of the thread safety attributes |
| 846 | bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){ |
| 847 | return llvm::StringSwitch<bool>(AttrName) |
| 848 | .Case("guarded_by", true) |
| 849 | .Case("guarded_var", true) |
| 850 | .Case("pt_guarded_by", true) |
| 851 | .Case("pt_guarded_var", true) |
| 852 | .Case("lockable", true) |
| 853 | .Case("scoped_lockable", true) |
| 854 | .Case("no_thread_safety_analysis", true) |
| 855 | .Case("acquired_after", true) |
| 856 | .Case("acquired_before", true) |
| 857 | .Case("exclusive_lock_function", true) |
| 858 | .Case("shared_lock_function", true) |
| 859 | .Case("exclusive_trylock_function", true) |
| 860 | .Case("shared_trylock_function", true) |
| 861 | .Case("unlock_function", true) |
| 862 | .Case("lock_returned", true) |
| 863 | .Case("locks_excluded", true) |
| 864 | .Case("exclusive_locks_required", true) |
| 865 | .Case("shared_locks_required", true) |
| 866 | .Default(false); |
| 867 | } |
| 868 | |
| 869 | /// \brief Parse the contents of thread safety attributes. These |
| 870 | /// should always be parsed as an expression list. |
| 871 | /// |
| 872 | /// We need to special case the parsing due to the fact that if the first token |
| 873 | /// of the first argument is an identifier, the main parse loop will store |
| 874 | /// that token as a "parameter" and the rest of |
| 875 | /// the arguments will be added to a list of "arguments". However, |
| 876 | /// subsequent tokens in the first argument are lost. We instead parse each |
| 877 | /// argument as an expression and add all arguments to the list of "arguments". |
| 878 | /// In future, we will take advantage of this special case to also |
| 879 | /// deal with some argument scoping issues here (for example, referring to a |
| 880 | /// function parameter in the attribute on that function). |
| 881 | void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName, |
| 882 | SourceLocation AttrNameLoc, |
| 883 | ParsedAttributes &Attrs, |
| 884 | SourceLocation *EndLoc) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 885 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 886 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 887 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 888 | T.consumeOpen(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 889 | |
| 890 | ExprVector ArgExprs(Actions); |
| 891 | bool ArgExprsOk = true; |
| 892 | |
| 893 | // now parse the list of expressions |
DeLesley Hutchins | 4805f15 | 2011-12-14 19:36:06 +0000 | [diff] [blame] | 894 | while (Tok.isNot(tok::r_paren)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 895 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 896 | if (ArgExpr.isInvalid()) { |
| 897 | ArgExprsOk = false; |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 898 | T.consumeClose(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 899 | break; |
| 900 | } else { |
| 901 | ArgExprs.push_back(ArgExpr.release()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 902 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 903 | if (Tok.isNot(tok::comma)) |
| 904 | break; |
| 905 | ConsumeToken(); // Eat the comma, move to the next argument |
| 906 | } |
| 907 | // Match the ')'. |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 908 | if (ArgExprsOk && !T.consumeClose()) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 909 | Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
| 910 | ArgExprs.take(), ArgExprs.size()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 911 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 912 | if (EndLoc) |
| 913 | *EndLoc = T.getCloseLocation(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 914 | } |
| 915 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 916 | /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets |
| 917 | /// of a C++11 attribute-specifier in a location where an attribute is not |
| 918 | /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this |
| 919 | /// situation. |
| 920 | /// |
| 921 | /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if |
| 922 | /// this doesn't appear to actually be an attribute-specifier, and the caller |
| 923 | /// should try to parse it. |
| 924 | bool Parser::DiagnoseProhibitedCXX11Attribute() { |
| 925 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)); |
| 926 | |
| 927 | switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) { |
| 928 | case CAK_NotAttributeSpecifier: |
| 929 | // No diagnostic: we're in Obj-C++11 and this is not actually an attribute. |
| 930 | return false; |
| 931 | |
| 932 | case CAK_InvalidAttributeSpecifier: |
| 933 | Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute); |
| 934 | return false; |
| 935 | |
| 936 | case CAK_AttributeSpecifier: |
| 937 | // Parse and discard the attributes. |
| 938 | SourceLocation BeginLoc = ConsumeBracket(); |
| 939 | ConsumeBracket(); |
| 940 | SkipUntil(tok::r_square, /*StopAtSemi*/ false); |
| 941 | assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied"); |
| 942 | SourceLocation EndLoc = ConsumeBracket(); |
| 943 | Diag(BeginLoc, diag::err_attributes_not_allowed) |
| 944 | << SourceRange(BeginLoc, EndLoc); |
| 945 | return true; |
| 946 | } |
Chandler Carruth | 2c6dbd7 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 947 | llvm_unreachable("All cases handled above."); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 948 | } |
| 949 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 950 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 951 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 952 | << attrs.Range; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 955 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 956 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 957 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 958 | /// location of the semicolon in DeclEnd. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 959 | /// |
| 960 | /// declaration: [C99 6.7] |
| 961 | /// block-declaration -> |
| 962 | /// simple-declaration |
| 963 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 964 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 965 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 966 | /// [C++] using-directive |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 967 | /// [C++] using-declaration |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 968 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 969 | /// others... [FIXME] |
| 970 | /// |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 971 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 972 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 973 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 974 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 975 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | e8cff36 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 976 | // Must temporarily exit the objective-c container scope for |
| 977 | // parsing c none objective-c decls. |
| 978 | ObjCDeclContextSwitch ObjCDC(*this); |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 979 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 980 | Decl *SingleDecl = 0; |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 981 | Decl *OwnedType = 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 982 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 983 | case tok::kw_template: |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 984 | case tok::kw_export: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 985 | ProhibitAttributes(attrs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 986 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 987 | break; |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 988 | case tok::kw_inline: |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 989 | // 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] | 990 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 991 | ProhibitAttributes(attrs); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 992 | SourceLocation InlineLoc = ConsumeToken(); |
| 993 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 994 | break; |
| 995 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 996 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 997 | true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 998 | case tok::kw_namespace: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 999 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1000 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1001 | break; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1002 | case tok::kw_using: |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1003 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1004 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1005 | break; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1006 | case tok::kw_static_assert: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1007 | case tok::kw__Static_assert: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1008 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1009 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1010 | break; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1011 | default: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1012 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1013 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1014 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1015 | // 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] | 1016 | // single decl, convert it now. Alias declarations can also declare a type; |
| 1017 | // include that too if it is present. |
| 1018 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1022 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 1023 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 1024 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1025 | /// |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1026 | /// for-range-declaration: [C++0x 6.5p1: stmt.ranged] |
| 1027 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1028 | /// |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1029 | /// 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] | 1030 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1031 | /// |
| 1032 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 1033 | /// of a simple-declaration. If we find that we are, we also parse the |
| 1034 | /// for-range-initializer, and place it here. |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1035 | Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts, |
| 1036 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1037 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1038 | ParsedAttributes &attrs, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1039 | bool RequireSemi, |
| 1040 | ForRangeInit *FRI) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1041 | // Parse the common declaration-specifiers piece. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1042 | ParsingDeclSpec DS(*this); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1043 | DS.takeAttributesFrom(attrs); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1044 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1045 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1046 | getDeclSpecContextFromDeclaratorContext(Context)); |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1047 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1048 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1049 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1050 | if (Tok.is(tok::semi)) { |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1051 | if (RequireSemi) ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1052 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1053 | DS); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1054 | DS.complete(TheDecl); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1055 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1056 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1057 | |
| 1058 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1059 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1060 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1061 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1062 | /// for a declarator. |
| 1063 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1064 | switch (Tok.getKind()) { |
| 1065 | case tok::annot_cxxscope: |
| 1066 | case tok::annot_template_id: |
| 1067 | case tok::caret: |
| 1068 | case tok::code_completion: |
| 1069 | case tok::coloncolon: |
| 1070 | case tok::ellipsis: |
| 1071 | case tok::kw___attribute: |
| 1072 | case tok::kw_operator: |
| 1073 | case tok::l_paren: |
| 1074 | case tok::star: |
| 1075 | return true; |
| 1076 | |
| 1077 | case tok::amp: |
| 1078 | case tok::ampamp: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1079 | return getLangOpts().CPlusPlus; |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1080 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1081 | 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] | 1082 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x && |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1083 | NextToken().is(tok::l_square); |
| 1084 | |
| 1085 | 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] | 1086 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1087 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1088 | case tok::identifier: |
| 1089 | switch (NextToken().getKind()) { |
| 1090 | case tok::code_completion: |
| 1091 | case tok::coloncolon: |
| 1092 | case tok::comma: |
| 1093 | case tok::equal: |
| 1094 | case tok::equalequal: // Might be a typo for '='. |
| 1095 | case tok::kw_alignas: |
| 1096 | case tok::kw_asm: |
| 1097 | case tok::kw___attribute: |
| 1098 | case tok::l_brace: |
| 1099 | case tok::l_paren: |
| 1100 | case tok::l_square: |
| 1101 | case tok::less: |
| 1102 | case tok::r_brace: |
| 1103 | case tok::r_paren: |
| 1104 | case tok::r_square: |
| 1105 | case tok::semi: |
| 1106 | return true; |
| 1107 | |
| 1108 | case tok::colon: |
| 1109 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1110 | // and in block scope it's probably a label. Inside a class definition, |
| 1111 | // this is a bit-field. |
| 1112 | return Context == Declarator::MemberContext || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1113 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1114 | |
| 1115 | case tok::identifier: // Possible virt-specifier. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1116 | return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken()); |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1117 | |
| 1118 | default: |
| 1119 | return false; |
| 1120 | } |
| 1121 | |
| 1122 | default: |
| 1123 | return false; |
| 1124 | } |
| 1125 | } |
| 1126 | |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1127 | /// Skip until we reach something which seems like a sensible place to pick |
| 1128 | /// up parsing after a malformed declaration. This will sometimes stop sooner |
| 1129 | /// than SkipUntil(tok::r_brace) would, but will never stop later. |
| 1130 | void Parser::SkipMalformedDecl() { |
| 1131 | while (true) { |
| 1132 | switch (Tok.getKind()) { |
| 1133 | case tok::l_brace: |
| 1134 | // Skip until matching }, then stop. We've probably skipped over |
| 1135 | // a malformed class or function definition or similar. |
| 1136 | ConsumeBrace(); |
| 1137 | SkipUntil(tok::r_brace, /*StopAtSemi*/false); |
| 1138 | if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) { |
| 1139 | // This declaration isn't over yet. Keep skipping. |
| 1140 | continue; |
| 1141 | } |
| 1142 | if (Tok.is(tok::semi)) |
| 1143 | ConsumeToken(); |
| 1144 | return; |
| 1145 | |
| 1146 | case tok::l_square: |
| 1147 | ConsumeBracket(); |
| 1148 | SkipUntil(tok::r_square, /*StopAtSemi*/false); |
| 1149 | continue; |
| 1150 | |
| 1151 | case tok::l_paren: |
| 1152 | ConsumeParen(); |
| 1153 | SkipUntil(tok::r_paren, /*StopAtSemi*/false); |
| 1154 | continue; |
| 1155 | |
| 1156 | case tok::r_brace: |
| 1157 | return; |
| 1158 | |
| 1159 | case tok::semi: |
| 1160 | ConsumeToken(); |
| 1161 | return; |
| 1162 | |
| 1163 | case tok::kw_inline: |
| 1164 | // 'inline namespace' at the start of a line is almost certainly |
| 1165 | // a good place to pick back up parsing. |
| 1166 | if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace)) |
| 1167 | return; |
| 1168 | break; |
| 1169 | |
| 1170 | case tok::kw_namespace: |
| 1171 | // 'namespace' at the start of a line is almost certainly a good |
| 1172 | // place to pick back up parsing. |
| 1173 | if (Tok.isAtStartOfLine()) |
| 1174 | return; |
| 1175 | break; |
| 1176 | |
| 1177 | case tok::eof: |
| 1178 | return; |
| 1179 | |
| 1180 | default: |
| 1181 | break; |
| 1182 | } |
| 1183 | |
| 1184 | ConsumeAnyToken(); |
| 1185 | } |
| 1186 | } |
| 1187 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1188 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1189 | /// definition or a group of object declarations, actually parse the |
| 1190 | /// result. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1191 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1192 | unsigned Context, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1193 | bool AllowFunctionDefinitions, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1194 | SourceLocation *DeclEnd, |
| 1195 | ForRangeInit *FRI) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1196 | // Parse the first declarator. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1197 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1198 | ParseDeclarator(D); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1199 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1200 | // Bail out if the first declarator didn't seem well-formed. |
| 1201 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1202 | SkipMalformedDecl(); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1203 | return DeclGroupPtrTy(); |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1204 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1205 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1206 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1207 | // appropriate function scope after the function Decl has been constructed. |
| 1208 | LateParsedAttrList LateParsedAttrs; |
| 1209 | if (D.isFunctionDeclarator()) |
| 1210 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1211 | |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1212 | // Check to see if we have a function *definition* which must have a body. |
| 1213 | if (AllowFunctionDefinitions && D.isFunctionDeclarator() && |
| 1214 | // Look at the next token to make sure that this isn't a function |
| 1215 | // declaration. We have to check this because __attribute__ might be the |
| 1216 | // start of a function definition in GCC-extended K&R C. |
| 1217 | !isDeclarationAfterDeclarator()) { |
Richard Smith | 58196dc | 2011-11-30 23:45:35 +0000 | [diff] [blame] | 1218 | |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1219 | if (isStartOfFunctionDefinition(D)) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1220 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1221 | Diag(Tok, diag::err_function_declared_typedef); |
| 1222 | |
| 1223 | // Recover by treating the 'typedef' as spurious. |
| 1224 | DS.ClearStorageClassSpecs(); |
| 1225 | } |
| 1226 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1227 | Decl *TheDecl = |
| 1228 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1229 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | if (isDeclarationSpecifier()) { |
| 1233 | // If there is an invalid declaration specifier right after the function |
| 1234 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1235 | // actually a body. Just fall through into the code that handles it as a |
| 1236 | // prototype, and let the top-level code handle the erroneous declspec |
| 1237 | // where it would otherwise expect a comma or semicolon. |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1238 | } else { |
| 1239 | Diag(Tok, diag::err_expected_fn_body); |
| 1240 | SkipUntil(tok::semi); |
| 1241 | return DeclGroupPtrTy(); |
| 1242 | } |
| 1243 | } |
| 1244 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1245 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1246 | return DeclGroupPtrTy(); |
| 1247 | |
| 1248 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1249 | // must parse and analyze the for-range-initializer before the declaration is |
| 1250 | // analyzed. |
| 1251 | if (FRI && Tok.is(tok::colon)) { |
| 1252 | FRI->ColonLoc = ConsumeToken(); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1253 | if (Tok.is(tok::l_brace)) |
| 1254 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1255 | else |
| 1256 | FRI->RangeExpr = ParseExpression(); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1257 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1258 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
| 1259 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | 6895a64 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 1260 | D.complete(ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1261 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1); |
| 1262 | } |
| 1263 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1264 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1265 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1266 | if (LateParsedAttrs.size() > 0) |
| 1267 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1268 | D.complete(FirstDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1269 | if (FirstDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1270 | DeclsInGroup.push_back(FirstDecl); |
| 1271 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1272 | bool ExpectSemi = Context != Declarator::ForContext; |
| 1273 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1274 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1275 | // error, bail out. |
| 1276 | while (Tok.is(tok::comma)) { |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1277 | SourceLocation CommaLoc = ConsumeToken(); |
| 1278 | |
| 1279 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1280 | // This comma was followed by a line-break and something which can't be |
| 1281 | // the start of a declarator. The comma was probably a typo for a |
| 1282 | // semicolon. |
| 1283 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1284 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1285 | ExpectSemi = false; |
| 1286 | break; |
| 1287 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1288 | |
| 1289 | // Parse the next declarator. |
| 1290 | D.clear(); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1291 | D.setCommaLoc(CommaLoc); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1292 | |
| 1293 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1294 | // declaration, these would be part of the declspec. In subsequent |
| 1295 | // declarators, they become part of the declarator itself, so that they |
| 1296 | // don't apply to declarators after *this* one. Examples: |
| 1297 | // short __attribute__((common)) var; -> declspec |
| 1298 | // short var __attribute__((common)); -> declarator |
| 1299 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1300 | MaybeParseGNUAttributes(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1301 | |
| 1302 | ParseDeclarator(D); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1303 | if (!D.isInvalidType()) { |
| 1304 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1305 | D.complete(ThisDecl); |
| 1306 | if (ThisDecl) |
| 1307 | DeclsInGroup.push_back(ThisDecl); |
| 1308 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | if (DeclEnd) |
| 1312 | *DeclEnd = Tok.getLocation(); |
| 1313 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1314 | if (ExpectSemi && |
Chris Lattner | 8bb21d3 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 1315 | ExpectAndConsumeSemi(Context == Declarator::FileContext |
| 1316 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1317 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1318 | // Okay, there was no semicolon and one was expected. If we see a |
| 1319 | // declaration specifier, just assume it was missing and continue parsing. |
| 1320 | // Otherwise things are very confused and we skip to recover. |
| 1321 | if (!isDeclarationSpecifier()) { |
| 1322 | SkipUntil(tok::r_brace, true, true); |
| 1323 | if (Tok.is(tok::semi)) |
| 1324 | ConsumeToken(); |
| 1325 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1328 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1329 | DeclsInGroup.data(), |
| 1330 | DeclsInGroup.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1333 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1334 | /// declarator. Returns true on an error. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1335 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1336 | // If a simple-asm-expr is present, parse it. |
| 1337 | if (Tok.is(tok::kw_asm)) { |
| 1338 | SourceLocation Loc; |
| 1339 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1340 | if (AsmLabel.isInvalid()) { |
| 1341 | SkipUntil(tok::semi, true, true); |
| 1342 | return true; |
| 1343 | } |
| 1344 | |
| 1345 | D.setAsmLabel(AsmLabel.release()); |
| 1346 | D.SetRangeEnd(Loc); |
| 1347 | } |
| 1348 | |
| 1349 | MaybeParseGNUAttributes(D); |
| 1350 | return false; |
| 1351 | } |
| 1352 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1353 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1354 | /// declarator'. This method parses the remainder of the declaration |
| 1355 | /// (including any attributes or initializer, among other things) and |
| 1356 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1357 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1358 | /// init-declarator: [C99 6.7] |
| 1359 | /// declarator |
| 1360 | /// declarator '=' initializer |
| 1361 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1362 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1363 | /// [C++] declarator initializer[opt] |
| 1364 | /// |
| 1365 | /// [C++] initializer: |
| 1366 | /// [C++] '=' initializer-clause |
| 1367 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1368 | /// [C++0x] '=' 'default' [TODO] |
| 1369 | /// [C++0x] '=' 'delete' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1370 | /// [C++0x] braced-init-list |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1371 | /// |
| 1372 | /// According to the standard grammar, =default and =delete are function |
| 1373 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1374 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1375 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1376 | const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1377 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1378 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1379 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1380 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1381 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1382 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1383 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1384 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1385 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1386 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1387 | switch (TemplateInfo.Kind) { |
| 1388 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1389 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1390 | break; |
| 1391 | |
| 1392 | case ParsedTemplateInfo::Template: |
| 1393 | case ParsedTemplateInfo::ExplicitSpecialization: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1394 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1395 | MultiTemplateParamsArg(Actions, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1396 | TemplateInfo.TemplateParams->data(), |
| 1397 | TemplateInfo.TemplateParams->size()), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1398 | D); |
| 1399 | break; |
| 1400 | |
| 1401 | case ParsedTemplateInfo::ExplicitInstantiation: { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1402 | DeclResult ThisRes |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1403 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1404 | TemplateInfo.ExternLoc, |
| 1405 | TemplateInfo.TemplateLoc, |
| 1406 | D); |
| 1407 | if (ThisRes.isInvalid()) { |
| 1408 | SkipUntil(tok::semi, true, true); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1409 | return 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | ThisDecl = ThisRes.get(); |
| 1413 | break; |
| 1414 | } |
| 1415 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1416 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1417 | bool TypeContainsAuto = |
| 1418 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
| 1419 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1420 | // Parse declarator '=' initializer. |
Richard Trieu | d6c7c67 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1421 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | fcaf27e | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1422 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1423 | ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1424 | if (Tok.is(tok::kw_delete)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1425 | if (D.isFunctionDeclarator()) |
| 1426 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1427 | << 1 /* delete */; |
| 1428 | else |
| 1429 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Sean Hunt | fe2695e | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1430 | } else if (Tok.is(tok::kw_default)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1431 | if (D.isFunctionDeclarator()) |
Sebastian Redl | ecfcd56 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1432 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1433 | << 0 /* default */; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1434 | else |
| 1435 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1436 | } else { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1437 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1438 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1439 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1440 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1441 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1442 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1443 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1444 | cutOffParsing(); |
| 1445 | return 0; |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1448 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1449 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1450 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1451 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1452 | ExitScope(); |
| 1453 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1454 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1455 | if (Init.isInvalid()) { |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1456 | SkipUntil(tok::comma, true, true); |
| 1457 | Actions.ActOnInitializerError(ThisDecl); |
| 1458 | } else |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1459 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1460 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1461 | } |
| 1462 | } else if (Tok.is(tok::l_paren)) { |
| 1463 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1464 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1465 | T.consumeOpen(); |
| 1466 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1467 | ExprVector Exprs(Actions); |
| 1468 | CommaLocsTy CommaLocs; |
| 1469 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1470 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1471 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1472 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1473 | } |
| 1474 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1475 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 1476 | SkipUntil(tok::r_paren); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1477 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1478 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1479 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1480 | ExitScope(); |
| 1481 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1482 | } else { |
| 1483 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1484 | T.consumeClose(); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1485 | |
| 1486 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1487 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1488 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1489 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1490 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1491 | ExitScope(); |
| 1492 | } |
| 1493 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1494 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 1495 | T.getCloseLocation(), |
| 1496 | move_arg(Exprs)); |
| 1497 | Actions.AddInitializerToDecl(ThisDecl, Initializer.take(), |
| 1498 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1499 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1500 | } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) { |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1501 | // Parse C++0x braced-init-list. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1502 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1503 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1504 | if (D.getCXXScopeSpec().isSet()) { |
| 1505 | EnterScope(0); |
| 1506 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1507 | } |
| 1508 | |
| 1509 | ExprResult Init(ParseBraceInitializer()); |
| 1510 | |
| 1511 | if (D.getCXXScopeSpec().isSet()) { |
| 1512 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1513 | ExitScope(); |
| 1514 | } |
| 1515 | |
| 1516 | if (Init.isInvalid()) { |
| 1517 | Actions.ActOnInitializerError(ThisDecl); |
| 1518 | } else |
| 1519 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1520 | /*DirectInit=*/true, TypeContainsAuto); |
| 1521 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1522 | } else { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1523 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1524 | } |
| 1525 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1526 | Actions.FinalizeDeclaration(ThisDecl); |
| 1527 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1528 | return ThisDecl; |
| 1529 | } |
| 1530 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1531 | /// ParseSpecifierQualifierList |
| 1532 | /// specifier-qualifier-list: |
| 1533 | /// type-specifier specifier-qualifier-list[opt] |
| 1534 | /// type-qualifier specifier-qualifier-list[opt] |
| 1535 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 1536 | /// |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1537 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 1538 | DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1539 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1540 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1541 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1542 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1543 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1544 | // Validate declspec for type-name. |
| 1545 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1546 | if (DSC == DSC_type_specifier && !DS.hasTypeSpecifier()) { |
| 1547 | Diag(Tok, diag::err_expected_type); |
| 1548 | DS.SetTypeSpecError(); |
| 1549 | } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() && |
| 1550 | !DS.hasAttributes()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1551 | Diag(Tok, diag::err_typename_requires_specqual); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1552 | if (!DS.hasTypeSpecifier()) |
| 1553 | DS.SetTypeSpecError(); |
| 1554 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1556 | // Issue diagnostic and remove storage class if present. |
| 1557 | if (Specs & DeclSpec::PQ_StorageClassSpecifier) { |
| 1558 | if (DS.getStorageClassSpecLoc().isValid()) |
| 1559 | Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass); |
| 1560 | else |
| 1561 | Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass); |
| 1562 | DS.ClearStorageClassSpecs(); |
| 1563 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1564 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1565 | // Issue diagnostic and remove function specfier if present. |
| 1566 | if (Specs & DeclSpec::PQ_FunctionSpecifier) { |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1567 | if (DS.isInlineSpecified()) |
| 1568 | Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1569 | if (DS.isVirtualSpecified()) |
| 1570 | Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec); |
| 1571 | if (DS.isExplicitSpecified()) |
| 1572 | Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1573 | DS.ClearFunctionSpecs(); |
| 1574 | } |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1575 | |
| 1576 | // Issue diagnostic and remove constexpr specfier if present. |
| 1577 | if (DS.isConstexprSpecified()) { |
| 1578 | Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr); |
| 1579 | DS.ClearConstexprSpec(); |
| 1580 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1583 | /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the |
| 1584 | /// specified token is valid after the identifier in a declarator which |
| 1585 | /// immediately follows the declspec. For example, these things are valid: |
| 1586 | /// |
| 1587 | /// int x [ 4]; // direct-declarator |
| 1588 | /// int x ( int y); // direct-declarator |
| 1589 | /// int(int x ) // direct-declarator |
| 1590 | /// int x ; // simple-declaration |
| 1591 | /// int x = 17; // init-declarator-list |
| 1592 | /// int x , y; // init-declarator-list |
| 1593 | /// int x __asm__ ("foo"); // init-declarator-list |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1594 | /// int x : 4; // struct-declarator |
Chris Lattner | c83c27a | 2009-04-12 22:29:43 +0000 | [diff] [blame] | 1595 | /// int x { 5}; // C++'0x unified initializers |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1596 | /// |
| 1597 | /// This is not, because 'x' does not immediately follow the declspec (though |
| 1598 | /// ')' happens to be valid anyway). |
| 1599 | /// int (x) |
| 1600 | /// |
| 1601 | static bool isValidAfterIdentifierInDeclarator(const Token &T) { |
| 1602 | return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) || |
| 1603 | T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) || |
Chris Lattner | b6645dd | 2009-04-14 21:16:09 +0000 | [diff] [blame] | 1604 | T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon); |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 1605 | } |
| 1606 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1607 | |
| 1608 | /// ParseImplicitInt - This method is called when we have an non-typename |
| 1609 | /// identifier in a declspec (which normally terminates the decl spec) when |
| 1610 | /// the declspec has no type specifier. In this case, the declspec is either |
| 1611 | /// malformed or is "implicit int" (in K&R and C89). |
| 1612 | /// |
| 1613 | /// This method handles diagnosing this prettily and returns false if the |
| 1614 | /// declspec is done being processed. If it recovers and thinks there may be |
| 1615 | /// other pieces of declspec after it, it returns true. |
| 1616 | /// |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1617 | bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1618 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1619 | AccessSpecifier AS, DeclSpecContext DSC) { |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1620 | assert(Tok.is(tok::identifier) && "should have identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1621 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1622 | SourceLocation Loc = Tok.getLocation(); |
| 1623 | // If we see an identifier that is not a type name, we normally would |
| 1624 | // parse it as the identifer being declared. However, when a typename |
| 1625 | // is typo'd or the definition is not included, this will incorrectly |
| 1626 | // parse the typename as the identifier name and fall over misparsing |
| 1627 | // later parts of the diagnostic. |
| 1628 | // |
| 1629 | // As such, we try to do some look-ahead in cases where this would |
| 1630 | // otherwise be an "implicit-int" case to see if this is invalid. For |
| 1631 | // example: "static foo_t x = 4;" In this case, if we parsed foo_t as |
| 1632 | // an identifier with implicit int, we'd get a parse error because the |
| 1633 | // next token is obviously invalid for a type. Parse these as a case |
| 1634 | // with an invalid type specifier. |
| 1635 | assert(!DS.hasTypeSpecifier() && "Type specifier checked above"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1636 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1637 | // Since we know that this either implicit int (which is rare) or an |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1638 | // error, do lookahead to try to do better recovery. This never applies within |
| 1639 | // a type specifier. |
| 1640 | // FIXME: Don't bail out here in languages with no implicit int (like |
| 1641 | // C++ with no -fms-extensions). This is much more likely to be an undeclared |
| 1642 | // type or typo than a use of implicit int. |
| 1643 | if (DSC != DSC_type_specifier && |
| 1644 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1645 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 1646 | // we just avoid eating the identifier, so it will be parsed as the |
| 1647 | // identifier in the declarator. |
| 1648 | return false; |
| 1649 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1650 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1651 | // Otherwise, if we don't consume this token, we are going to emit an |
| 1652 | // error anyway. Try to recover from various common problems. Check |
| 1653 | // to see if this was a reference to a tag name without a tag specified. |
| 1654 | // This is a common problem in C (saying 'foo' instead of 'struct foo'). |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1655 | // |
| 1656 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 1657 | if (SS == 0) { |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1658 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1659 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1660 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1661 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1662 | default: break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1663 | case DeclSpec::TST_enum: |
| 1664 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 1665 | case DeclSpec::TST_union: |
| 1666 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 1667 | case DeclSpec::TST_struct: |
| 1668 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
| 1669 | case DeclSpec::TST_class: |
| 1670 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1671 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1672 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1673 | if (TagName) { |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 1674 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 1675 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 1676 | Sema::LookupOrdinaryName); |
| 1677 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1678 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 1679 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 1680 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 1681 | |
| 1682 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 1683 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 1684 | I != IEnd; ++I) |
Kaelyn Uhrain | 392b3f5 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 1685 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 1686 | << TokenName << TagName; |
| 1687 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1688 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1689 | // Parse this as a tag as if the missing tag were present. |
| 1690 | if (TagKind == tok::kw_enum) |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1691 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1692 | else |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1693 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
| 1694 | /*EnteringContext*/ false, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1695 | return true; |
| 1696 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1697 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1698 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1699 | // This is almost certainly an invalid type name. Let the action emit a |
| 1700 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1701 | ParsedType T; |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1702 | if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc, |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1703 | getCurScope(), SS, T)) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1704 | // The action emitted a diagnostic, so we don't have to. |
| 1705 | if (T) { |
| 1706 | // The action has suggested that the type T could be used. Set that as |
| 1707 | // the type in the declaration specifiers, consume the would-be type |
| 1708 | // name token, and we're done. |
| 1709 | const char *PrevSpec; |
| 1710 | unsigned DiagID; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1711 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1712 | DS.SetRangeEnd(Tok.getLocation()); |
| 1713 | ConsumeToken(); |
| 1714 | |
| 1715 | // There may be other declaration specifiers after this. |
| 1716 | return true; |
| 1717 | } |
| 1718 | |
| 1719 | // Fall through; the action had no suggestion for us. |
| 1720 | } else { |
| 1721 | // The action did not emit a diagnostic, so emit one now. |
| 1722 | SourceRange R; |
| 1723 | if (SS) R = SS->getRange(); |
| 1724 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 1725 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1726 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1727 | // Mark this as an error. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1728 | DS.SetTypeSpecError(); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1729 | DS.SetRangeEnd(Tok.getLocation()); |
| 1730 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1731 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1732 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 1733 | // avoid rippling error messages on subsequent uses of the same type, |
| 1734 | // could be useful if #include was forgotten. |
| 1735 | return false; |
| 1736 | } |
| 1737 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1738 | /// \brief Determine the declaration specifier context from the declarator |
| 1739 | /// context. |
| 1740 | /// |
| 1741 | /// \param Context the declarator context, which is one of the |
| 1742 | /// Declarator::TheContext enumerator values. |
| 1743 | Parser::DeclSpecContext |
| 1744 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 1745 | if (Context == Declarator::MemberContext) |
| 1746 | return DSC_class; |
| 1747 | if (Context == Declarator::FileContext) |
| 1748 | return DSC_top_level; |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 1749 | if (Context == Declarator::TrailingReturnContext) |
| 1750 | return DSC_trailing; |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1751 | return DSC_normal; |
| 1752 | } |
| 1753 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1754 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 1755 | /// |
| 1756 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 1757 | /// type. Ideally, the type should be propagated directly into Sema. |
| 1758 | /// |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1759 | /// [C11] type-id |
| 1760 | /// [C11] constant-expression |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1761 | /// [C++0x] type-id ...[opt] |
| 1762 | /// [C++0x] assignment-expression ...[opt] |
| 1763 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 1764 | SourceLocation &EllipsisLoc) { |
| 1765 | ExprResult ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1766 | if (isTypeIdInParens()) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1767 | SourceLocation TypeLoc = Tok.getLocation(); |
| 1768 | ParsedType Ty = ParseTypeName().get(); |
| 1769 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1770 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 1771 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1772 | } else |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1773 | ER = ParseConstantExpression(); |
| 1774 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1775 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis)) |
Peter Collingbourne | fe9b2a8 | 2011-10-24 17:56:00 +0000 | [diff] [blame] | 1776 | EllipsisLoc = ConsumeToken(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1777 | |
| 1778 | return ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 1782 | /// attribute to Attrs. |
| 1783 | /// |
| 1784 | /// alignment-specifier: |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1785 | /// [C11] '_Alignas' '(' type-id ')' |
| 1786 | /// [C11] '_Alignas' '(' constant-expression ')' |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1787 | /// [C++0x] 'alignas' '(' type-id ...[opt] ')' |
| 1788 | /// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1789 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
| 1790 | SourceLocation *endLoc) { |
| 1791 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 1792 | "Not an alignment-specifier!"); |
| 1793 | |
| 1794 | SourceLocation KWLoc = Tok.getLocation(); |
| 1795 | ConsumeToken(); |
| 1796 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1797 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1798 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1799 | return; |
| 1800 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1801 | SourceLocation EllipsisLoc; |
| 1802 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1803 | if (ArgExpr.isInvalid()) { |
| 1804 | SkipUntil(tok::r_paren); |
| 1805 | return; |
| 1806 | } |
| 1807 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1808 | T.consumeClose(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1809 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1810 | *endLoc = T.getCloseLocation(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1811 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1812 | // FIXME: Handle pack-expansions here. |
| 1813 | if (EllipsisLoc.isValid()) { |
| 1814 | Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported); |
| 1815 | return; |
| 1816 | } |
| 1817 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1818 | ExprVector ArgExprs(Actions); |
| 1819 | ArgExprs.push_back(ArgExpr.release()); |
| 1820 | Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1821 | 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1824 | /// ParseDeclarationSpecifiers |
| 1825 | /// declaration-specifiers: [C99 6.7] |
| 1826 | /// storage-class-specifier declaration-specifiers[opt] |
| 1827 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1828 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1829 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1830 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1831 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1832 | /// |
| 1833 | /// storage-class-specifier: [C99 6.7.1] |
| 1834 | /// 'typedef' |
| 1835 | /// 'extern' |
| 1836 | /// 'static' |
| 1837 | /// 'auto' |
| 1838 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1839 | /// [C++] 'mutable' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1840 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1841 | /// function-specifier: [C99 6.7.4] |
| 1842 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1843 | /// [C++] 'virtual' |
| 1844 | /// [C++] 'explicit' |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1845 | /// [OpenCL] '__kernel' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1846 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 1847 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1848 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1849 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1850 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1851 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1852 | AccessSpecifier AS, |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1853 | DeclSpecContext DSContext, |
| 1854 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1855 | if (DS.getSourceRange().isInvalid()) { |
| 1856 | DS.SetRangeStart(Tok.getLocation()); |
| 1857 | DS.SetRangeEnd(Tok.getLocation()); |
| 1858 | } |
| 1859 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 1860 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1861 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1862 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1863 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1864 | unsigned DiagID = 0; |
| 1865 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1866 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1867 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1868 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1869 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 1870 | DoneWithDeclSpec: |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 1871 | // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt] |
| 1872 | MaybeParseCXX0XAttributes(DS.getAttributes()); |
| 1873 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1874 | // If this is not a declaration specifier token, we're done reading decl |
| 1875 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 1876 | DS.Finish(Diags, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1877 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1878 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1879 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1880 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1881 | if (DS.hasTypeSpecifier()) { |
| 1882 | bool AllowNonIdentifiers |
| 1883 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 1884 | Scope::BlockScope | |
| 1885 | Scope::TemplateParamScope | |
| 1886 | Scope::FunctionPrototypeScope | |
| 1887 | Scope::AtCatchScope)) == 0; |
| 1888 | bool AllowNestedNameSpecifiers |
| 1889 | = DSContext == DSC_top_level || |
| 1890 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 1891 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 1892 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
| 1893 | AllowNonIdentifiers, |
| 1894 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1895 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1898 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 1899 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 1900 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1901 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
| 1902 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1903 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1904 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1905 | else if (CurParsedObjCImpl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1906 | CCC = Sema::PCC_ObjCImplementation; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1907 | |
| 1908 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1909 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1910 | } |
| 1911 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 1912 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1913 | // C++ scope specifier. Annotate and loop, or bail out on error. |
| 1914 | if (TryAnnotateCXXScopeToken(true)) { |
| 1915 | if (!DS.hasTypeSpecifier()) |
| 1916 | DS.SetTypeSpecError(); |
| 1917 | goto DoneWithDeclSpec; |
| 1918 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 1919 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 1920 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1921 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1922 | |
| 1923 | case tok::annot_cxxscope: { |
| 1924 | if (DS.hasTypeSpecifier()) |
| 1925 | goto DoneWithDeclSpec; |
| 1926 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1927 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 1928 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 1929 | Tok.getAnnotationRange(), |
| 1930 | SS); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1931 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1932 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1933 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1934 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1935 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1936 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1937 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1938 | |
| 1939 | // C++ [class.qual]p2: |
| 1940 | // In a lookup in which the constructor is an acceptable lookup |
| 1941 | // result and the nested-name-specifier nominates a class C: |
| 1942 | // |
| 1943 | // - if the name specified after the |
| 1944 | // nested-name-specifier, when looked up in C, is the |
| 1945 | // injected-class-name of C (Clause 9), or |
| 1946 | // |
| 1947 | // - if the name specified after the nested-name-specifier |
| 1948 | // is the same as the identifier or the |
| 1949 | // simple-template-id's template-name in the last |
| 1950 | // component of the nested-name-specifier, |
| 1951 | // |
| 1952 | // the name is instead considered to name the constructor of |
| 1953 | // class C. |
| 1954 | // |
| 1955 | // Thus, if the template-name is actually the constructor |
| 1956 | // name, then the code is ill-formed; this interpretation is |
| 1957 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 1958 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 1959 | if ((DSContext == DSC_top_level || |
| 1960 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
| 1961 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1962 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1963 | if (isConstructorDeclarator()) { |
| 1964 | // The user meant this to be an out-of-line constructor |
| 1965 | // definition, but template arguments are not allowed |
| 1966 | // there. Just allow this as a constructor; we'll |
| 1967 | // complain about it later. |
| 1968 | goto DoneWithDeclSpec; |
| 1969 | } |
| 1970 | |
| 1971 | // The user meant this to name a type, but it actually names |
| 1972 | // a constructor with some extraneous template |
| 1973 | // arguments. Complain, then parse it as a type as the user |
| 1974 | // intended. |
| 1975 | Diag(TemplateId->TemplateNameLoc, |
| 1976 | diag::err_out_of_line_template_id_names_constructor) |
| 1977 | << TemplateId->Name; |
| 1978 | } |
| 1979 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1980 | DS.getTypeSpecScope() = SS; |
| 1981 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1982 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1983 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 1984 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1985 | continue; |
| 1986 | } |
| 1987 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1988 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1989 | DS.getTypeSpecScope() = SS; |
| 1990 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1991 | if (Tok.getAnnotationValue()) { |
| 1992 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 1993 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
| 1994 | Tok.getAnnotationEndLoc(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1995 | PrevSpec, DiagID, T); |
| 1996 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 1997 | else |
| 1998 | DS.SetTypeSpecError(); |
| 1999 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2000 | ConsumeToken(); // The typename |
| 2001 | } |
| 2002 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2003 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2004 | goto DoneWithDeclSpec; |
| 2005 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2006 | // If we're in a context where the identifier could be a class name, |
| 2007 | // check whether this is a constructor declaration. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2008 | if ((DSContext == DSC_top_level || |
| 2009 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2010 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2011 | &SS)) { |
| 2012 | if (isConstructorDeclarator()) |
| 2013 | goto DoneWithDeclSpec; |
| 2014 | |
| 2015 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 2016 | // of the class is qualified in a context where it could name |
| 2017 | // a constructor, its a constructor name. However, we've |
| 2018 | // looked at the declarator, and the user probably meant this |
| 2019 | // to be a type. Complain that it isn't supposed to be treated |
| 2020 | // as a type, then proceed to parse it as a type. |
| 2021 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 2022 | << Next.getIdentifierInfo(); |
| 2023 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2024 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2025 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 2026 | Next.getLocation(), |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2027 | getCurScope(), &SS, |
| 2028 | false, false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2029 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2030 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2031 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2032 | // If the referenced identifier is not a type, then this declspec is |
| 2033 | // erroneous: We already checked about that it has no type specifier, and |
| 2034 | // 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] | 2035 | // typename. |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2036 | if (TypeRep == 0) { |
| 2037 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2038 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2039 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2040 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2041 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2042 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2043 | ConsumeToken(); // The C++ scope. |
| 2044 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2045 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2046 | DiagID, TypeRep); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2047 | if (isInvalid) |
| 2048 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2049 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2050 | DS.SetRangeEnd(Tok.getLocation()); |
| 2051 | ConsumeToken(); // The typename. |
| 2052 | |
| 2053 | continue; |
| 2054 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2055 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2056 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2057 | if (Tok.getAnnotationValue()) { |
| 2058 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | c43271e | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 2059 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2060 | DiagID, T); |
| 2061 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2062 | DS.SetTypeSpecError(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 2063 | |
| 2064 | if (isInvalid) |
| 2065 | break; |
| 2066 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2067 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2068 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2069 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2070 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2071 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2072 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2073 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2074 | ParseObjCProtocolQualifiers(DS); |
| 2075 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2076 | continue; |
| 2077 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2078 | |
Douglas Gregor | bfad915 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2079 | case tok::kw___is_signed: |
| 2080 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 2081 | // typically treats it as a trait. If we see __is_signed as it appears |
| 2082 | // in libstdc++, e.g., |
| 2083 | // |
| 2084 | // static const bool __is_signed; |
| 2085 | // |
| 2086 | // then treat __is_signed as an identifier rather than as a keyword. |
| 2087 | if (DS.getTypeSpecType() == TST_bool && |
| 2088 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
| 2089 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 2090 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
| 2091 | Tok.setKind(tok::identifier); |
| 2092 | } |
| 2093 | |
| 2094 | // We're done with the declaration-specifiers. |
| 2095 | goto DoneWithDeclSpec; |
| 2096 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2097 | // typedef-name |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2098 | case tok::kw_decltype: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2099 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2100 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 2101 | // so handle it as such. This is important for ctor parsing. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2102 | if (getLangOpts().CPlusPlus) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2103 | if (TryAnnotateCXXScopeToken(true)) { |
| 2104 | if (!DS.hasTypeSpecifier()) |
| 2105 | DS.SetTypeSpecError(); |
| 2106 | goto DoneWithDeclSpec; |
| 2107 | } |
| 2108 | if (!Tok.is(tok::identifier)) |
| 2109 | continue; |
| 2110 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2111 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2112 | // This identifier can only be a typedef name if we haven't already seen |
| 2113 | // a type-specifier. Without this check we misparse: |
| 2114 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 2115 | if (DS.hasTypeSpecifier()) |
| 2116 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2117 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2118 | // Check for need to substitute AltiVec keyword tokens. |
| 2119 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2120 | break; |
| 2121 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2122 | ParsedType TypeRep = |
| 2123 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 2124 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2125 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2126 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 2127 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2128 | if (!TypeRep) { |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2129 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2130 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2131 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2132 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2133 | // If we're in a context where the identifier could be a class name, |
| 2134 | // check whether this is a constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2135 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2136 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2137 | isConstructorDeclarator()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2138 | goto DoneWithDeclSpec; |
| 2139 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2140 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2141 | DiagID, TypeRep); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2142 | if (isInvalid) |
| 2143 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2144 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2145 | DS.SetRangeEnd(Tok.getLocation()); |
| 2146 | ConsumeToken(); // The identifier |
| 2147 | |
| 2148 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2149 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2150 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2151 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2152 | ParseObjCProtocolQualifiers(DS); |
| 2153 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2154 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2155 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2156 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2157 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2158 | |
| 2159 | // type-name |
| 2160 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2161 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2162 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2163 | // This template-id does not refer to a type name, so we're |
| 2164 | // done with the type-specifiers. |
| 2165 | goto DoneWithDeclSpec; |
| 2166 | } |
| 2167 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2168 | // If we're in a context where the template-id could be a |
| 2169 | // constructor name or specialization, check whether this is a |
| 2170 | // constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2171 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2172 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2173 | isConstructorDeclarator()) |
| 2174 | goto DoneWithDeclSpec; |
| 2175 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2176 | // Turn the template-id annotation token into a type annotation |
| 2177 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2178 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2179 | continue; |
| 2180 | } |
| 2181 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2182 | // GNU attributes support. |
| 2183 | case tok::kw___attribute: |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2184 | ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2185 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2186 | |
| 2187 | // Microsoft declspec support. |
| 2188 | case tok::kw___declspec: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2189 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2190 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2191 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2192 | // Microsoft single token adornments. |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2193 | case tok::kw___forceinline: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2194 | // FIXME: Add handling here! |
| 2195 | break; |
| 2196 | |
| 2197 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2198 | case tok::kw___ptr32: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2199 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2200 | case tok::kw___cdecl: |
| 2201 | case tok::kw___stdcall: |
| 2202 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2203 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2204 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2205 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2206 | continue; |
| 2207 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2208 | // Borland single token adornments. |
| 2209 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2210 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2211 | continue; |
| 2212 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2213 | // OpenCL single token adornments. |
| 2214 | case tok::kw___kernel: |
| 2215 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2216 | continue; |
| 2217 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2218 | // storage-class-specifier |
| 2219 | case tok::kw_typedef: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2220 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
| 2221 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2222 | break; |
| 2223 | case tok::kw_extern: |
| 2224 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2225 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2226 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
| 2227 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2228 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2229 | case tok::kw___private_extern__: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2230 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
| 2231 | Loc, PrevSpec, DiagID); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2232 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2233 | case tok::kw_static: |
| 2234 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2235 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2236 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
| 2237 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2238 | break; |
| 2239 | case tok::kw_auto: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2240 | if (getLangOpts().CPlusPlus0x) { |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2241 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2242 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2243 | PrevSpec, DiagID); |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2244 | if (!isInvalid) |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2245 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2246 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2247 | } else |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2248 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 2249 | DiagID); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2250 | } else |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2251 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2252 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2253 | break; |
| 2254 | case tok::kw_register: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2255 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
| 2256 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2257 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2258 | case tok::kw_mutable: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2259 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
| 2260 | PrevSpec, DiagID); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2261 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2262 | case tok::kw___thread: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2263 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2264 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2265 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2266 | // function-specifier |
| 2267 | case tok::kw_inline: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2268 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2269 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2270 | case tok::kw_virtual: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2271 | isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2272 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2273 | case tok::kw_explicit: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2274 | isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2275 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2276 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2277 | // alignment-specifier |
| 2278 | case tok::kw__Alignas: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2279 | if (!getLangOpts().C11) |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2280 | Diag(Tok, diag::ext_c11_alignas); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2281 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2282 | continue; |
| 2283 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2284 | // friend |
| 2285 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2286 | if (DSContext == DSC_class) |
| 2287 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 2288 | else { |
| 2289 | PrevSpec = ""; // not actually used by the diagnostic |
| 2290 | DiagID = diag::err_friend_invalid_in_context; |
| 2291 | isInvalid = true; |
| 2292 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2293 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2294 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2295 | // Modules |
| 2296 | case tok::kw___module_private__: |
| 2297 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 2298 | break; |
| 2299 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2300 | // constexpr |
| 2301 | case tok::kw_constexpr: |
| 2302 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 2303 | break; |
| 2304 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2305 | // type-specifier |
| 2306 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2307 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 2308 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2309 | break; |
| 2310 | case tok::kw_long: |
| 2311 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2312 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2313 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2314 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2315 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2316 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2317 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2318 | case tok::kw___int64: |
| 2319 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2320 | DiagID); |
| 2321 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2322 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2323 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 2324 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2325 | break; |
| 2326 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2327 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2328 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2329 | break; |
| 2330 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2331 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2332 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2333 | break; |
| 2334 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2335 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2336 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2337 | break; |
| 2338 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2339 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 2340 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2341 | break; |
| 2342 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2343 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 2344 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2345 | break; |
| 2346 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2347 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 2348 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2349 | break; |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 2350 | case tok::kw___int128: |
| 2351 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
| 2352 | DiagID); |
| 2353 | break; |
| 2354 | case tok::kw_half: |
| 2355 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
| 2356 | DiagID); |
| 2357 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2358 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2359 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 2360 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2361 | break; |
| 2362 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2363 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 2364 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2365 | break; |
| 2366 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2367 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 2368 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2369 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2370 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2371 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 2372 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2373 | break; |
| 2374 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2375 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 2376 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2377 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2378 | case tok::kw_bool: |
| 2379 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2380 | if (Tok.is(tok::kw_bool) && |
| 2381 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 2382 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 2383 | PrevSpec = ""; // Not used by the diagnostic. |
| 2384 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2385 | // For better error recovery. |
| 2386 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2387 | isInvalid = true; |
| 2388 | } else { |
| 2389 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 2390 | DiagID); |
| 2391 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2392 | break; |
| 2393 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2394 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2395 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2396 | break; |
| 2397 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2398 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2399 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2400 | break; |
| 2401 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2402 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2403 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2404 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2405 | case tok::kw___vector: |
| 2406 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2407 | break; |
| 2408 | case tok::kw___pixel: |
| 2409 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2410 | break; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 2411 | case tok::kw___unknown_anytype: |
| 2412 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 2413 | PrevSpec, DiagID); |
| 2414 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2415 | |
| 2416 | // class-specifier: |
| 2417 | case tok::kw_class: |
| 2418 | case tok::kw_struct: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2419 | case tok::kw_union: { |
| 2420 | tok::TokenKind Kind = Tok.getKind(); |
| 2421 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2422 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
| 2423 | EnteringContext, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2424 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2425 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2426 | |
| 2427 | // enum-specifier: |
| 2428 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2429 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2430 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2431 | continue; |
| 2432 | |
| 2433 | // cv-qualifier: |
| 2434 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2435 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2436 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2437 | break; |
| 2438 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2439 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2440 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2441 | break; |
| 2442 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2443 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2444 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2445 | break; |
| 2446 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2447 | // C++ typename-specifier: |
| 2448 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2449 | if (TryAnnotateTypeOrScopeToken()) { |
| 2450 | DS.SetTypeSpecError(); |
| 2451 | goto DoneWithDeclSpec; |
| 2452 | } |
| 2453 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2454 | continue; |
| 2455 | break; |
| 2456 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2457 | // GNU typeof support. |
| 2458 | case tok::kw_typeof: |
| 2459 | ParseTypeofSpecifier(DS); |
| 2460 | continue; |
| 2461 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2462 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2463 | ParseDecltypeSpecifier(DS); |
| 2464 | continue; |
| 2465 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2466 | case tok::kw___underlying_type: |
| 2467 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2468 | continue; |
| 2469 | |
| 2470 | case tok::kw__Atomic: |
| 2471 | ParseAtomicSpecifier(DS); |
| 2472 | continue; |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2473 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2474 | // OpenCL qualifiers: |
| 2475 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2476 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2477 | goto DoneWithDeclSpec; |
| 2478 | case tok::kw___private: |
| 2479 | case tok::kw___global: |
| 2480 | case tok::kw___local: |
| 2481 | case tok::kw___constant: |
| 2482 | case tok::kw___read_only: |
| 2483 | case tok::kw___write_only: |
| 2484 | case tok::kw___read_write: |
| 2485 | ParseOpenCLQualifiers(DS); |
| 2486 | break; |
| 2487 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 2488 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2489 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2490 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 2491 | // but we support it. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2492 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2493 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2494 | |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 2495 | if (!ParseObjCProtocolQualifiers(DS)) |
| 2496 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 2497 | << FixItHint::CreateInsertion(Loc, "id") |
| 2498 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2499 | |
| 2500 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2501 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2502 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2503 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2504 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2505 | if (isInvalid) { |
| 2506 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2507 | assert(DiagID); |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 2508 | |
| 2509 | if (DiagID == diag::ext_duplicate_declspec) |
| 2510 | Diag(Tok, DiagID) |
| 2511 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 2512 | else |
| 2513 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2514 | } |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2515 | |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 2516 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2517 | if (DiagID != diag::err_bool_redeclaration) |
| 2518 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2519 | } |
| 2520 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 2521 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2522 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 2523 | /// semicolon. |
| 2524 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2525 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2526 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2527 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2528 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2529 | /// struct-declarator-list: |
| 2530 | /// struct-declarator |
| 2531 | /// struct-declarator-list ',' struct-declarator |
| 2532 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 2533 | /// struct-declarator: |
| 2534 | /// declarator |
| 2535 | /// [GNU] declarator attributes[opt] |
| 2536 | /// declarator[opt] ':' constant-expression |
| 2537 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 2538 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2539 | void Parser:: |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2540 | ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 2541 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2542 | if (Tok.is(tok::kw___extension__)) { |
| 2543 | // __extension__ silences extension warnings in the subexpression. |
| 2544 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2545 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2546 | return ParseStructDeclaration(DS, Fields); |
| 2547 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2548 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2549 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2550 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2551 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 2552 | // If there are no declarators, this is a free-standing declaration |
| 2553 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2554 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2555 | Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2556 | return; |
| 2557 | } |
| 2558 | |
| 2559 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2560 | bool FirstDeclarator = true; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2561 | SourceLocation CommaLoc; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2562 | while (1) { |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame^] | 2563 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2564 | FieldDeclarator DeclaratorInfo(DS); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2565 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2566 | |
| 2567 | // Attributes are only allowed here on successive declarators. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2568 | if (!FirstDeclarator) |
| 2569 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2570 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2571 | /// struct-declarator: declarator |
| 2572 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2573 | if (Tok.isNot(tok::colon)) { |
| 2574 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 2575 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2576 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2577 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2578 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2579 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2580 | ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2581 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2582 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2583 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 2584 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2585 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2586 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2587 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2588 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2589 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2590 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2591 | // We're done with this declarator; invoke the callback. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2592 | Decl *D = Fields.invoke(DeclaratorInfo); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2593 | PD.complete(D); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2594 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2595 | // If we don't have a comma, it is either the end of the list (a ';') |
| 2596 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2597 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2598 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2599 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2600 | // Consume the comma. |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2601 | CommaLoc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2602 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2603 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2604 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2605 | } |
| 2606 | |
| 2607 | /// ParseStructUnionBody |
| 2608 | /// struct-contents: |
| 2609 | /// struct-declaration-list |
| 2610 | /// [EXT] empty |
| 2611 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 2612 | /// struct-declaration-list: |
| 2613 | /// struct-declaration |
| 2614 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2615 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2616 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2617 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2618 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2619 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 2620 | "parsing struct/union body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2621 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2622 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 2623 | if (T.consumeOpen()) |
| 2624 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2625 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 2626 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2627 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2628 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2629 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 2630 | // C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2631 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) { |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 2632 | Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union); |
| 2633 | Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union); |
| 2634 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2635 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2636 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2637 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2638 | // 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] | 2639 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2640 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2641 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2642 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2643 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 2644 | Diag(Tok, diag::ext_extra_struct_semi) |
Douglas Gregor | f13ca06 | 2010-06-16 23:08:59 +0000 | [diff] [blame] | 2645 | << DeclSpec::getSpecifierName((DeclSpec::TST)TagType) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 2646 | << FixItHint::CreateRemoval(Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2647 | ConsumeToken(); |
| 2648 | continue; |
| 2649 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2650 | |
| 2651 | // Parse all the comma separated declarators. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2652 | DeclSpec DS(AttrFactory); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2653 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2654 | if (!Tok.is(tok::at)) { |
| 2655 | struct CFieldCallback : FieldCallback { |
| 2656 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2657 | Decl *TagDecl; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2658 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2659 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2660 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2661 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2662 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 2663 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2664 | virtual Decl *invoke(FieldDeclarator &FD) { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2665 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2666 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 2667 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 2668 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2669 | FieldDecls.push_back(Field); |
| 2670 | return Field; |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 2671 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2672 | } Callback(*this, TagDecl, FieldDecls); |
| 2673 | |
| 2674 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2675 | } else { // Handle @defs |
| 2676 | ConsumeToken(); |
| 2677 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 2678 | Diag(Tok, diag::err_unexpected_at); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2679 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2680 | continue; |
| 2681 | } |
| 2682 | ConsumeToken(); |
| 2683 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 2684 | if (!Tok.is(tok::identifier)) { |
| 2685 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2686 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2687 | continue; |
| 2688 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2689 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2690 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2691 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2692 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 2693 | ConsumeToken(); |
| 2694 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2695 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2696 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2697 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2698 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2699 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2700 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2701 | break; |
| 2702 | } else { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2703 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 2704 | // 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] | 2705 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2706 | // If we stopped at a ';', eat it. |
| 2707 | if (Tok.is(tok::semi)) ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2708 | } |
| 2709 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2710 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2711 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2712 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2713 | ParsedAttributes attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2714 | // If attributes exist after struct contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2715 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 2716 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2717 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 2718 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2719 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2720 | attrs.getList()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2721 | StructScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2722 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 2723 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2726 | /// ParseEnumSpecifier |
| 2727 | /// enum-specifier: [C99 6.7.2.2] |
| 2728 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2729 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2730 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 2731 | /// '}' attributes[opt] |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 2732 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 2733 | /// '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2734 | /// 'enum' identifier |
| 2735 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2736 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2737 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 2738 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2739 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2740 | /// enum-head: [C++11] |
| 2741 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 2742 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 2743 | /// identifier enum-base[opt] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2744 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2745 | /// enum-key: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2746 | /// 'enum' |
| 2747 | /// 'enum' 'class' |
| 2748 | /// 'enum' 'struct' |
| 2749 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2750 | /// enum-base: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2751 | /// ':' type-specifier-seq |
| 2752 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2753 | /// [C++] elaborated-type-specifier: |
| 2754 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 2755 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2756 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 2757 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2758 | AccessSpecifier AS, DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2759 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2760 | if (Tok.is(tok::code_completion)) { |
| 2761 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2762 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2763 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2764 | } |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2765 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2766 | SourceLocation ScopedEnumKWLoc; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2767 | bool IsScopedUsingClassTag = false; |
| 2768 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2769 | if (getLangOpts().CPlusPlus0x && |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2770 | (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2771 | Diag(Tok, diag::warn_cxx98_compat_scoped_enum); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2772 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2773 | ScopedEnumKWLoc = ConsumeToken(); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2774 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2775 | |
| 2776 | // C++11 [temp.explicit]p12: The usual access controls do not apply to names |
| 2777 | // used to specify explicit instantiations. We extend this to also cover |
| 2778 | // explicit specializations. |
| 2779 | Sema::SuppressAccessChecksRAII SuppressAccess(Actions, |
| 2780 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 2781 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 2782 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2783 | // If attributes exist after tag, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2784 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2785 | MaybeParseGNUAttributes(attrs); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2786 | |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 2787 | // If declspecs exist after tag, parse them. |
| 2788 | while (Tok.is(tok::kw___declspec)) |
| 2789 | ParseMicrosoftDeclSpec(attrs); |
| 2790 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2791 | // Enum definitions should not be parsed in a trailing-return-type. |
| 2792 | bool AllowDeclaration = DSC != DSC_trailing; |
| 2793 | |
| 2794 | bool AllowFixedUnderlyingType = AllowDeclaration && |
| 2795 | (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt || |
| 2796 | getLangOpts().ObjC2); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2797 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2798 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2799 | if (getLangOpts().CPlusPlus) { |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2800 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 2801 | // if a fixed underlying type is allowed. |
| 2802 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
| 2803 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2804 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
| 2805 | /*EnteringContext=*/false)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2806 | return; |
| 2807 | |
| 2808 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2809 | Diag(Tok, diag::err_expected_ident); |
| 2810 | if (Tok.isNot(tok::l_brace)) { |
| 2811 | // Has no name and is not a definition. |
| 2812 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2813 | SkipUntil(tok::comma, true); |
| 2814 | return; |
| 2815 | } |
| 2816 | } |
| 2817 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2818 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2819 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2820 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2821 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2822 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2823 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2824 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2825 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2826 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2827 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2828 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2829 | // If an identifier is present, consume and remember it. |
| 2830 | IdentifierInfo *Name = 0; |
| 2831 | SourceLocation NameLoc; |
| 2832 | if (Tok.is(tok::identifier)) { |
| 2833 | Name = Tok.getIdentifierInfo(); |
| 2834 | NameLoc = ConsumeToken(); |
| 2835 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2836 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2837 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2838 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 2839 | // declaration of a scoped enumeration. |
| 2840 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2841 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2842 | IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2843 | } |
| 2844 | |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2845 | // Stop suppressing access control now we've parsed the enum name. |
| 2846 | SuppressAccess.done(); |
| 2847 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2848 | TypeResult BaseType; |
| 2849 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2850 | // Parse the fixed underlying type. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2851 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2852 | bool PossibleBitfield = false; |
| 2853 | if (getCurScope()->getFlags() & Scope::ClassScope) { |
| 2854 | // If we're in class scope, this can either be an enum declaration with |
| 2855 | // an underlying type, or a declaration of a bitfield member. We try to |
| 2856 | // use a simple disambiguation scheme first to catch the common cases |
| 2857 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 2858 | // anything that's a simple-type-specifier followed by '(' as an |
| 2859 | // expression. This suffices because function types are not valid |
| 2860 | // underlying types anyway. |
| 2861 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
| 2862 | // If the next token starts an expression, we know we're parsing a |
| 2863 | // bit-field. This is the common case. |
| 2864 | if (TPR == TPResult::True()) |
| 2865 | PossibleBitfield = true; |
| 2866 | // If the next token starts a type-specifier-seq, it may be either a |
| 2867 | // a fixed underlying type or the start of a function-style cast in C++; |
| 2868 | // lookahead one more token to see if it's obvious that we have a |
| 2869 | // fixed underlying type. |
| 2870 | else if (TPR == TPResult::False() && |
| 2871 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 2872 | // Consume the ':'. |
| 2873 | ConsumeToken(); |
| 2874 | } else { |
| 2875 | // We have the start of a type-specifier-seq, so we have to perform |
| 2876 | // tentative parsing to determine whether we have an expression or a |
| 2877 | // type. |
| 2878 | TentativeParsingAction TPA(*this); |
| 2879 | |
| 2880 | // Consume the ':'. |
| 2881 | ConsumeToken(); |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 2882 | |
| 2883 | // If we see a type specifier followed by an open-brace, we have an |
| 2884 | // ambiguity between an underlying type and a C++11 braced |
| 2885 | // function-style cast. Resolve this by always treating it as an |
| 2886 | // underlying type. |
| 2887 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 2888 | // this case. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2889 | if ((getLangOpts().CPlusPlus && |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 2890 | isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2891 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2892 | // We'll parse this as a bitfield later. |
| 2893 | PossibleBitfield = true; |
| 2894 | TPA.Revert(); |
| 2895 | } else { |
| 2896 | // We have a type-specifier-seq. |
| 2897 | TPA.Commit(); |
| 2898 | } |
| 2899 | } |
| 2900 | } else { |
| 2901 | // Consume the ':'. |
| 2902 | ConsumeToken(); |
| 2903 | } |
| 2904 | |
| 2905 | if (!PossibleBitfield) { |
| 2906 | SourceRange Range; |
| 2907 | BaseType = ParseTypeName(&Range); |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 2908 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2909 | if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2) |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 2910 | Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type) |
| 2911 | << Range; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2912 | if (getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2913 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2914 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2915 | } |
| 2916 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2917 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 2918 | // friend declaration, and cannot have an accompanying definition. If we have |
| 2919 | // 'enum foo;', then this is a forward declaration. If we have |
| 2920 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 2921 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2922 | // |
| 2923 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 2924 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 2925 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 2926 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2927 | Sema::TagUseKind TUK; |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2928 | if (DS.isFriendSpecified()) |
| 2929 | TUK = Sema::TUK_Friend; |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2930 | else if (!AllowDeclaration) |
| 2931 | TUK = Sema::TUK_Reference; |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2932 | else if (Tok.is(tok::l_brace)) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2933 | TUK = Sema::TUK_Definition; |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2934 | else if (Tok.is(tok::semi) && DSC != DSC_type_specifier) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2935 | TUK = Sema::TUK_Declaration; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2936 | else |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2937 | TUK = Sema::TUK_Reference; |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2938 | |
| 2939 | MultiTemplateParamsArg TParams; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 2940 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2941 | TUK != Sema::TUK_Reference) { |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2942 | if (!getLangOpts().CPlusPlus0x || !SS.isSet()) { |
| 2943 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2944 | Diag(Tok, diag::err_enum_template); |
| 2945 | SkipUntil(tok::comma, true); |
| 2946 | return; |
| 2947 | } |
| 2948 | |
| 2949 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 2950 | // Enumerations can't be explicitly instantiated. |
| 2951 | DS.SetTypeSpecError(); |
| 2952 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 2953 | return; |
| 2954 | } |
| 2955 | |
| 2956 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 2957 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 2958 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 2959 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2960 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2961 | if (!Name && TUK != Sema::TUK_Definition) { |
| 2962 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2963 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2964 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2965 | SkipUntil(tok::comma, true); |
| 2966 | return; |
| 2967 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2968 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 2969 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 2970 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2971 | const char *PrevSpec = 0; |
| 2972 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2973 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2974 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2975 | AS, DS.getModulePrivateSpecLoc(), TParams, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2976 | Owned, IsDependent, ScopedEnumKWLoc, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2977 | IsScopedUsingClassTag, BaseType); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2978 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2979 | if (IsDependent) { |
| 2980 | // This enum has a dependent nested-name-specifier. Handle it as a |
| 2981 | // dependent tag. |
| 2982 | if (!Name) { |
| 2983 | DS.SetTypeSpecError(); |
| 2984 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 2985 | return; |
| 2986 | } |
| 2987 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2988 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2989 | TUK, SS, Name, StartLoc, |
| 2990 | NameLoc); |
| 2991 | if (Type.isInvalid()) { |
| 2992 | DS.SetTypeSpecError(); |
| 2993 | return; |
| 2994 | } |
| 2995 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 2996 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 2997 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 2998 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 2999 | Diag(StartLoc, DiagID) << PrevSpec; |
| 3000 | |
| 3001 | return; |
| 3002 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3003 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3004 | if (!TagDecl) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3005 | // The action failed to produce an enumeration tag. If this is a |
| 3006 | // definition, consume the entire definition. |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3007 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3008 | ConsumeBrace(); |
| 3009 | SkipUntil(tok::r_brace); |
| 3010 | } |
| 3011 | |
| 3012 | DS.SetTypeSpecError(); |
| 3013 | return; |
| 3014 | } |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3015 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3016 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3017 | if (TUK == Sema::TUK_Friend) { |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3018 | Diag(Tok, diag::err_friend_decl_defines_type) |
| 3019 | << SourceRange(DS.getFriendSpecLoc()); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3020 | ConsumeBrace(); |
| 3021 | SkipUntil(tok::r_brace); |
| 3022 | } else { |
| 3023 | ParseEnumBody(StartLoc, TagDecl); |
| 3024 | } |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3025 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3026 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3027 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3028 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3029 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3030 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3031 | } |
| 3032 | |
| 3033 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3034 | /// enumerator-list: |
| 3035 | /// enumerator |
| 3036 | /// enumerator-list ',' enumerator |
| 3037 | /// enumerator: |
| 3038 | /// enumeration-constant |
| 3039 | /// enumeration-constant '=' constant-expression |
| 3040 | /// enumeration-constant: |
| 3041 | /// identifier |
| 3042 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3043 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3044 | // Enter the scope of the enum body and start the definition. |
| 3045 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3046 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3047 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3048 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3049 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3050 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3051 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3052 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3053 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3054 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3055 | SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3056 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3057 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3058 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3059 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3060 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3061 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3062 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3063 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3064 | // If attributes exist after the enumerator, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3065 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3066 | MaybeParseGNUAttributes(attrs); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3067 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3068 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3069 | ExprResult AssignedVal; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame^] | 3070 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3071 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3072 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3073 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3074 | AssignedVal = ParseConstantExpression(); |
| 3075 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3076 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3077 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3078 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3079 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3080 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3081 | LastEnumConstDecl, |
| 3082 | IdentLoc, Ident, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3083 | attrs.getList(), EqualLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3084 | AssignedVal.release()); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3085 | PD.complete(EnumConstDecl); |
| 3086 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3087 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3088 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3089 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3090 | if (Tok.is(tok::identifier)) { |
| 3091 | // We're missing a comma between enumerators. |
| 3092 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
| 3093 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
| 3094 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3095 | continue; |
| 3096 | } |
| 3097 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3098 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3099 | break; |
| 3100 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3101 | |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3102 | if (Tok.isNot(tok::identifier)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3103 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3104 | Diag(CommaLoc, diag::ext_enumerator_list_comma) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3105 | << getLangOpts().CPlusPlus |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3106 | << FixItHint::CreateRemoval(CommaLoc); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3107 | else if (getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3108 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3109 | << FixItHint::CreateRemoval(CommaLoc); |
| 3110 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3111 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3112 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3113 | // Eat the }. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3114 | T.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3115 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3116 | // If attributes exist after the identifier list, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3117 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3118 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3119 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3120 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
| 3121 | EnumDecl, EnumConstantDecls.data(), |
| 3122 | EnumConstantDecls.size(), getCurScope(), |
| 3123 | attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3124 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3125 | EnumScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3126 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3127 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3128 | } |
| 3129 | |
| 3130 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3131 | /// start of a type-qualifier-list. |
| 3132 | bool Parser::isTypeQualifier() const { |
| 3133 | switch (Tok.getKind()) { |
| 3134 | default: return false; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3135 | |
| 3136 | // type-qualifier only in OpenCL |
| 3137 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3138 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3139 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3140 | // type-qualifier |
| 3141 | case tok::kw_const: |
| 3142 | case tok::kw_volatile: |
| 3143 | case tok::kw_restrict: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3144 | case tok::kw___private: |
| 3145 | case tok::kw___local: |
| 3146 | case tok::kw___global: |
| 3147 | case tok::kw___constant: |
| 3148 | case tok::kw___read_only: |
| 3149 | case tok::kw___read_write: |
| 3150 | case tok::kw___write_only: |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3151 | return true; |
| 3152 | } |
| 3153 | } |
| 3154 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3155 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3156 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3157 | /// specifier or if we're not sure. |
| 3158 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3159 | switch (Tok.getKind()) { |
| 3160 | default: return false; |
| 3161 | // type-specifiers |
| 3162 | case tok::kw_short: |
| 3163 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3164 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3165 | case tok::kw___int128: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3166 | case tok::kw_signed: |
| 3167 | case tok::kw_unsigned: |
| 3168 | case tok::kw__Complex: |
| 3169 | case tok::kw__Imaginary: |
| 3170 | case tok::kw_void: |
| 3171 | case tok::kw_char: |
| 3172 | case tok::kw_wchar_t: |
| 3173 | case tok::kw_char16_t: |
| 3174 | case tok::kw_char32_t: |
| 3175 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3176 | case tok::kw_half: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3177 | case tok::kw_float: |
| 3178 | case tok::kw_double: |
| 3179 | case tok::kw_bool: |
| 3180 | case tok::kw__Bool: |
| 3181 | case tok::kw__Decimal32: |
| 3182 | case tok::kw__Decimal64: |
| 3183 | case tok::kw__Decimal128: |
| 3184 | case tok::kw___vector: |
| 3185 | |
| 3186 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3187 | case tok::kw_class: |
| 3188 | case tok::kw_struct: |
| 3189 | case tok::kw_union: |
| 3190 | // enum-specifier |
| 3191 | case tok::kw_enum: |
| 3192 | |
| 3193 | // typedef-name |
| 3194 | case tok::annot_typename: |
| 3195 | return true; |
| 3196 | } |
| 3197 | } |
| 3198 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3199 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3200 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3201 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3202 | switch (Tok.getKind()) { |
| 3203 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3204 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3205 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3206 | if (TryAltiVecVectorToken()) |
| 3207 | return true; |
| 3208 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3209 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3210 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3211 | // recurse to handle whatever we get. |
| 3212 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3213 | return true; |
| 3214 | if (Tok.is(tok::identifier)) |
| 3215 | return false; |
| 3216 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3217 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3218 | case tok::coloncolon: // ::foo::bar |
| 3219 | if (NextToken().is(tok::kw_new) || // ::new |
| 3220 | NextToken().is(tok::kw_delete)) // ::delete |
| 3221 | return false; |
| 3222 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3223 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3224 | return true; |
| 3225 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3226 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3227 | // GNU attributes support. |
| 3228 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3229 | // GNU typeof support. |
| 3230 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3231 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3232 | // type-specifiers |
| 3233 | case tok::kw_short: |
| 3234 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3235 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3236 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3237 | case tok::kw_signed: |
| 3238 | case tok::kw_unsigned: |
| 3239 | case tok::kw__Complex: |
| 3240 | case tok::kw__Imaginary: |
| 3241 | case tok::kw_void: |
| 3242 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3243 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3244 | case tok::kw_char16_t: |
| 3245 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3246 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3247 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3248 | case tok::kw_float: |
| 3249 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3250 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3251 | case tok::kw__Bool: |
| 3252 | case tok::kw__Decimal32: |
| 3253 | case tok::kw__Decimal64: |
| 3254 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3255 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3256 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3257 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3258 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3259 | case tok::kw_struct: |
| 3260 | case tok::kw_union: |
| 3261 | // enum-specifier |
| 3262 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3263 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3264 | // type-qualifier |
| 3265 | case tok::kw_const: |
| 3266 | case tok::kw_volatile: |
| 3267 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3268 | |
| 3269 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 3270 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3271 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3272 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 3273 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3274 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3275 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3276 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3277 | case tok::kw___cdecl: |
| 3278 | case tok::kw___stdcall: |
| 3279 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3280 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3281 | case tok::kw___w64: |
| 3282 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3283 | case tok::kw___ptr32: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3284 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3285 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3286 | |
| 3287 | case tok::kw___private: |
| 3288 | case tok::kw___local: |
| 3289 | case tok::kw___global: |
| 3290 | case tok::kw___constant: |
| 3291 | case tok::kw___read_only: |
| 3292 | case tok::kw___read_write: |
| 3293 | case tok::kw___write_only: |
| 3294 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3295 | return true; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3296 | |
| 3297 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3298 | return getLangOpts().OpenCL; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3299 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3300 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3301 | case tok::kw__Atomic: |
| 3302 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3303 | } |
| 3304 | } |
| 3305 | |
| 3306 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 3307 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3308 | /// |
| 3309 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 3310 | /// this check is to disambiguate between an expression and a declaration. |
| 3311 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3312 | switch (Tok.getKind()) { |
| 3313 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3314 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3315 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3316 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3317 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3318 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3319 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3320 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3321 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3322 | if (TryAltiVecVectorToken()) |
| 3323 | return true; |
| 3324 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3325 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3326 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3327 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3328 | // recurse to handle whatever we get. |
| 3329 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3330 | return true; |
| 3331 | if (Tok.is(tok::identifier)) |
| 3332 | return false; |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3333 | |
| 3334 | // If we're in Objective-C and we have an Objective-C class type followed |
| 3335 | // by an identifier and then either ':' or ']', in a place where an |
| 3336 | // expression is permitted, then this is probably a class message send |
| 3337 | // missing the initial '['. In this case, we won't consider this to be |
| 3338 | // the start of a declaration. |
| 3339 | if (DisambiguatingWithExpression && |
| 3340 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 3341 | return false; |
| 3342 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3343 | return isDeclarationSpecifier(); |
| 3344 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3345 | case tok::coloncolon: // ::foo::bar |
| 3346 | if (NextToken().is(tok::kw_new) || // ::new |
| 3347 | NextToken().is(tok::kw_delete)) // ::delete |
| 3348 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3349 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3350 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3351 | // recurse to handle whatever we get. |
| 3352 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3353 | return true; |
| 3354 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3355 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3356 | // storage-class-specifier |
| 3357 | case tok::kw_typedef: |
| 3358 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 3359 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3360 | case tok::kw_static: |
| 3361 | case tok::kw_auto: |
| 3362 | case tok::kw_register: |
| 3363 | case tok::kw___thread: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3364 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 3365 | // Modules |
| 3366 | case tok::kw___module_private__: |
| 3367 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3368 | // type-specifiers |
| 3369 | case tok::kw_short: |
| 3370 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3371 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3372 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3373 | case tok::kw_signed: |
| 3374 | case tok::kw_unsigned: |
| 3375 | case tok::kw__Complex: |
| 3376 | case tok::kw__Imaginary: |
| 3377 | case tok::kw_void: |
| 3378 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3379 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3380 | case tok::kw_char16_t: |
| 3381 | case tok::kw_char32_t: |
| 3382 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3383 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3384 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3385 | case tok::kw_float: |
| 3386 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3387 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3388 | case tok::kw__Bool: |
| 3389 | case tok::kw__Decimal32: |
| 3390 | case tok::kw__Decimal64: |
| 3391 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3392 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3393 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3394 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3395 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3396 | case tok::kw_struct: |
| 3397 | case tok::kw_union: |
| 3398 | // enum-specifier |
| 3399 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3400 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3401 | // type-qualifier |
| 3402 | case tok::kw_const: |
| 3403 | case tok::kw_volatile: |
| 3404 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3405 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3406 | // function-specifier |
| 3407 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3408 | case tok::kw_virtual: |
| 3409 | case tok::kw_explicit: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3410 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 3411 | // static_assert-declaration |
| 3412 | case tok::kw__Static_assert: |
| 3413 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3414 | // GNU typeof support. |
| 3415 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3416 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3417 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3418 | case tok::kw___attribute: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3419 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3420 | |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3421 | // C++0x decltype. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3422 | case tok::annot_decltype: |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3423 | return true; |
| 3424 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3425 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3426 | case tok::kw__Atomic: |
| 3427 | return true; |
| 3428 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 3429 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3430 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3431 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3432 | |
Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 3433 | // typedef-name |
| 3434 | case tok::annot_typename: |
| 3435 | return !DisambiguatingWithExpression || |
| 3436 | !isStartOfObjCClassMessageMissingOpenBracket(); |
| 3437 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 3438 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3439 | case tok::kw___cdecl: |
| 3440 | case tok::kw___stdcall: |
| 3441 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3442 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3443 | case tok::kw___w64: |
| 3444 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3445 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3446 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3447 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3448 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3449 | |
| 3450 | case tok::kw___private: |
| 3451 | case tok::kw___local: |
| 3452 | case tok::kw___global: |
| 3453 | case tok::kw___constant: |
| 3454 | case tok::kw___read_only: |
| 3455 | case tok::kw___read_write: |
| 3456 | case tok::kw___write_only: |
| 3457 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3458 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3459 | } |
| 3460 | } |
| 3461 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3462 | bool Parser::isConstructorDeclarator() { |
| 3463 | TentativeParsingAction TPA(*this); |
| 3464 | |
| 3465 | // Parse the C++ scope specifier. |
| 3466 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3467 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
| 3468 | /*EnteringContext=*/true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3469 | TPA.Revert(); |
| 3470 | return false; |
| 3471 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3472 | |
| 3473 | // Parse the constructor name. |
| 3474 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 3475 | // We already know that we have a constructor name; just consume |
| 3476 | // the token. |
| 3477 | ConsumeToken(); |
| 3478 | } else { |
| 3479 | TPA.Revert(); |
| 3480 | return false; |
| 3481 | } |
| 3482 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 3483 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3484 | if (Tok.isNot(tok::l_paren)) { |
| 3485 | TPA.Revert(); |
| 3486 | return false; |
| 3487 | } |
| 3488 | ConsumeParen(); |
| 3489 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 3490 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 3491 | // that we have a constructor. |
| 3492 | if (Tok.is(tok::r_paren) || |
| 3493 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3494 | TPA.Revert(); |
| 3495 | return true; |
| 3496 | } |
| 3497 | |
| 3498 | // If we need to, enter the specified scope. |
| 3499 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3500 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3501 | DeclScopeObj.EnterDeclaratorScope(); |
| 3502 | |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3503 | // Optionally skip Microsoft attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3504 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3505 | MaybeParseMicrosoftAttributes(Attrs); |
| 3506 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3507 | // Check whether the next token(s) are part of a declaration |
| 3508 | // specifier, in which case we have the start of a parameter and, |
| 3509 | // therefore, we know that this is a constructor. |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 3510 | bool IsConstructor = false; |
| 3511 | if (isDeclarationSpecifier()) |
| 3512 | IsConstructor = true; |
| 3513 | else if (Tok.is(tok::identifier) || |
| 3514 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 3515 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 3516 | // This might be a parenthesized member name, but is more likely to |
| 3517 | // be a constructor declaration with an invalid argument type. Keep |
| 3518 | // looking. |
| 3519 | if (Tok.is(tok::annot_cxxscope)) |
| 3520 | ConsumeToken(); |
| 3521 | ConsumeToken(); |
| 3522 | |
| 3523 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 3524 | // which must have one of the following syntactic forms (see the |
| 3525 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 3526 | switch (Tok.getKind()) { |
| 3527 | case tok::l_paren: |
| 3528 | // C(X ( int)); |
| 3529 | case tok::l_square: |
| 3530 | // C(X [ 5]); |
| 3531 | // C(X [ [attribute]]); |
| 3532 | case tok::coloncolon: |
| 3533 | // C(X :: Y); |
| 3534 | // C(X :: *p); |
| 3535 | case tok::r_paren: |
| 3536 | // C(X ) |
| 3537 | // Assume this isn't a constructor, rather than assuming it's a |
| 3538 | // constructor with an unnamed parameter of an ill-formed type. |
| 3539 | break; |
| 3540 | |
| 3541 | default: |
| 3542 | IsConstructor = true; |
| 3543 | break; |
| 3544 | } |
| 3545 | } |
| 3546 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3547 | TPA.Revert(); |
| 3548 | return IsConstructor; |
| 3549 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3550 | |
| 3551 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3552 | /// type-qualifier-list: [C99 6.7.5] |
| 3553 | /// type-qualifier |
| 3554 | /// [vendor] attributes |
| 3555 | /// [ only if VendorAttributesAllowed=true ] |
| 3556 | /// type-qualifier-list type-qualifier |
| 3557 | /// [vendor] type-qualifier-list attributes |
| 3558 | /// [ only if VendorAttributesAllowed=true ] |
| 3559 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
| 3560 | /// [ only if CXX0XAttributesAllowed=true ] |
| 3561 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3562 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3563 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 3564 | bool VendorAttributesAllowed, |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3565 | bool CXX11AttributesAllowed) { |
| 3566 | if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed && |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3567 | isCXX11AttributeSpecifier()) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3568 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3569 | ParseCXX11Attributes(attrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3570 | DS.takeAttributesFrom(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3571 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3572 | |
| 3573 | SourceLocation EndLoc; |
| 3574 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3575 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3576 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3577 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3578 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3579 | SourceLocation Loc = Tok.getLocation(); |
| 3580 | |
| 3581 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3582 | case tok::code_completion: |
| 3583 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3584 | return cutOffParsing(); |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3585 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3586 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3587 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3588 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3589 | break; |
| 3590 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3591 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3592 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3593 | break; |
| 3594 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3595 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3596 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3597 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3598 | |
| 3599 | // OpenCL qualifiers: |
| 3600 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3601 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3602 | goto DoneWithTypeQuals; |
| 3603 | case tok::kw___private: |
| 3604 | case tok::kw___global: |
| 3605 | case tok::kw___local: |
| 3606 | case tok::kw___constant: |
| 3607 | case tok::kw___read_only: |
| 3608 | case tok::kw___write_only: |
| 3609 | case tok::kw___read_write: |
| 3610 | ParseOpenCLQualifiers(DS); |
| 3611 | break; |
| 3612 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3613 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 3614 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3615 | case tok::kw___ptr32: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3616 | case tok::kw___cdecl: |
| 3617 | case tok::kw___stdcall: |
| 3618 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3619 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3620 | case tok::kw___unaligned: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3621 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3622 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3623 | continue; |
| 3624 | } |
| 3625 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3626 | case tok::kw___pascal: |
| 3627 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3628 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3629 | continue; |
| 3630 | } |
| 3631 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3632 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3633 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3634 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3635 | continue; // do *not* consume the next token! |
| 3636 | } |
| 3637 | // otherwise, FALL THROUGH! |
| 3638 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3639 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3640 | // If this is not a type-qualifier token, we're done reading type |
| 3641 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 3642 | DS.Finish(Diags, PP); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3643 | if (EndLoc.isValid()) |
| 3644 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3645 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3646 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 3647 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3648 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 3649 | if (isInvalid) { |
| 3650 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3651 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3652 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3653 | EndLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3654 | } |
| 3655 | } |
| 3656 | |
| 3657 | |
| 3658 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 3659 | /// |
| 3660 | void Parser::ParseDeclarator(Declarator &D) { |
| 3661 | /// This implements the 'declarator' production in the C grammar, then checks |
| 3662 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3663 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3664 | } |
| 3665 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3666 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) { |
| 3667 | if (Kind == tok::star || Kind == tok::caret) |
| 3668 | return true; |
| 3669 | |
| 3670 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 3671 | if (!Lang.CPlusPlus) |
| 3672 | return false; |
| 3673 | |
| 3674 | return Kind == tok::amp || Kind == tok::ampamp; |
| 3675 | } |
| 3676 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3677 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 3678 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 3679 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3680 | /// ptr-operator production. |
| 3681 | /// |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 3682 | /// 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] | 3683 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 3684 | /// isConstructorDeclarator. |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 3685 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3686 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 3687 | /// [C] pointer[opt] direct-declarator |
| 3688 | /// [C++] direct-declarator |
| 3689 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3690 | /// |
| 3691 | /// pointer: [C99 6.7.5] |
| 3692 | /// '*' type-qualifier-list[opt] |
| 3693 | /// '*' type-qualifier-list[opt] pointer |
| 3694 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3695 | /// ptr-operator: |
| 3696 | /// '*' cv-qualifier-seq[opt] |
| 3697 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3698 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3699 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3700 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3701 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3702 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 3703 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3704 | if (Diags.hasAllExtensionsSilenced()) |
| 3705 | D.setExtension(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3706 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3707 | // C++ member pointers start with a '::' or a nested-name. |
| 3708 | // Member pointers get special handling, since there's no place for the |
| 3709 | // scope spec in the generic path below. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3710 | if (getLangOpts().CPlusPlus && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 3711 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 3712 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3713 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 3714 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3715 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3716 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3717 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 3718 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3719 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3720 | // The scope spec really belongs to the direct-declarator. |
| 3721 | D.getCXXScopeSpec() = SS; |
| 3722 | if (DirectDeclParser) |
| 3723 | (this->*DirectDeclParser)(D); |
| 3724 | return; |
| 3725 | } |
| 3726 | |
| 3727 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3728 | D.SetRangeEnd(Loc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3729 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3730 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3731 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3732 | |
| 3733 | // Recurse to parse whatever is left. |
| 3734 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 3735 | |
| 3736 | // Sema will have to catch (syntactically invalid) pointers into global |
| 3737 | // scope. It has to catch pointers into namespace scope anyway. |
| 3738 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3739 | Loc), |
| 3740 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3741 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3742 | return; |
| 3743 | } |
| 3744 | } |
| 3745 | |
| 3746 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3747 | // Not a pointer, C++ reference, or block. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3748 | if (!isPtrOperatorToken(Kind, getLangOpts())) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3749 | if (DirectDeclParser) |
| 3750 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3751 | return; |
| 3752 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3753 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3754 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 3755 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3756 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3757 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3758 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 3759 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 3760 | // Is a pointer. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3761 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3762 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3763 | // FIXME: GNU attributes are not allowed here in a new-type-id. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3764 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3765 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3766 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3767 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3768 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3769 | if (Kind == tok::star) |
| 3770 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 3771 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 3772 | DS.getConstSpecLoc(), |
| 3773 | DS.getVolatileSpecLoc(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3774 | DS.getRestrictSpecLoc()), |
| 3775 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3776 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3777 | else |
| 3778 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3779 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3780 | Loc), |
| 3781 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3782 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3783 | } else { |
| 3784 | // Is a reference |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3785 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3786 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3787 | // Complain about rvalue references in C++03, but then go on and build |
| 3788 | // the declarator. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3789 | if (Kind == tok::ampamp) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3790 | Diag(Loc, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3791 | diag::warn_cxx98_compat_rvalue_reference : |
| 3792 | diag::ext_rvalue_reference); |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3793 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3794 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 3795 | ParseTypeQualifierListOpt(DS); |
| 3796 | D.ExtendWithDeclSpec(DS); |
| 3797 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3798 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 3799 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 3800 | // template type argument, in which case the cv-qualifiers are ignored. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3801 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 3802 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 3803 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3804 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3805 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 3806 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3807 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3808 | } |
| 3809 | |
| 3810 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3811 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3812 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3813 | if (D.getNumTypeObjects() > 0) { |
| 3814 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 3815 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 3816 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 3817 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 3818 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 3819 | << II; |
| 3820 | else |
| 3821 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 3822 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3823 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3824 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3825 | // can go ahead and build the (technically ill-formed) |
| 3826 | // declarator: reference collapsing will take care of it. |
| 3827 | } |
| 3828 | } |
| 3829 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3830 | // 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] | 3831 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3832 | Kind == tok::amp), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3833 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3834 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3835 | } |
| 3836 | } |
| 3837 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3838 | static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D, |
| 3839 | SourceLocation EllipsisLoc) { |
| 3840 | if (EllipsisLoc.isValid()) { |
| 3841 | FixItHint Insertion; |
| 3842 | if (!D.getEllipsisLoc().isValid()) { |
| 3843 | Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "..."); |
| 3844 | D.setEllipsisLoc(EllipsisLoc); |
| 3845 | } |
| 3846 | P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) |
| 3847 | << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName(); |
| 3848 | } |
| 3849 | } |
| 3850 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3851 | /// ParseDirectDeclarator |
| 3852 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3853 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3854 | /// '(' declarator ')' |
| 3855 | /// [GNU] '(' attributes declarator ')' |
| 3856 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 3857 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 3858 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 3859 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 3860 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3861 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 3862 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3863 | /// direct-declarator '(' parameter-type-list ')' |
| 3864 | /// direct-declarator '(' identifier-list[opt] ')' |
| 3865 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 3866 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3867 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 3868 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3869 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 3870 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 3871 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3872 | /// [C++] declarator-id |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3873 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3874 | /// |
| 3875 | /// declarator-id: [C++ 8] |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3876 | /// '...'[opt] id-expression |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3877 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 3878 | /// |
| 3879 | /// id-expression: [C++ 5.1] |
| 3880 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3881 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3882 | /// |
| 3883 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3884 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3885 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3886 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3887 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3888 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 3889 | /// |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 3890 | /// Note, any additional constructs added here may need corresponding changes |
| 3891 | /// in isConstructorDeclarator. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3892 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3893 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3894 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3895 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3896 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3897 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3898 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 3899 | D.getContext() == Declarator::MemberContext; |
| 3900 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
| 3901 | EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3902 | } |
| 3903 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3904 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3905 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 3906 | // Change the declaration context for name lookup, until this function |
| 3907 | // is exited (and the declarator has been parsed). |
| 3908 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3909 | } |
| 3910 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3911 | // C++0x [dcl.fct]p14: |
| 3912 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
| 3913 | // of a parameter-declaration-clause without a preceding comma. In |
| 3914 | // this case, the ellipsis is parsed as part of the |
| 3915 | // abstract-declarator if the type of the parameter names a template |
| 3916 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 3917 | // as part of the parameter-declaration-clause. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3918 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3919 | !((D.getContext() == Declarator::PrototypeContext || |
| 3920 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3921 | NextToken().is(tok::r_paren) && |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3922 | !Actions.containsUnexpandedParameterPacks(D))) { |
| 3923 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 3924 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) { |
| 3925 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 3926 | // the user what they should have done. |
| 3927 | ParseDeclarator(D); |
| 3928 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 3929 | return; |
| 3930 | } else |
| 3931 | D.setEllipsisLoc(EllipsisLoc); |
| 3932 | |
| 3933 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 3934 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 3935 | // the l_paren token. |
| 3936 | } |
| 3937 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3938 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 3939 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 3940 | // We found something that indicates the start of an unqualified-id. |
| 3941 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 3942 | bool AllowConstructorName; |
| 3943 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 3944 | AllowConstructorName = false; |
| 3945 | else if (D.getCXXScopeSpec().isSet()) |
| 3946 | AllowConstructorName = |
| 3947 | (D.getContext() == Declarator::FileContext || |
| 3948 | (D.getContext() == Declarator::MemberContext && |
| 3949 | D.getDeclSpec().isFriendSpecified())); |
| 3950 | else |
| 3951 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 3952 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3953 | SourceLocation TemplateKWLoc; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3954 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 3955 | /*EnteringContext=*/true, |
| 3956 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3957 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3958 | ParsedType(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 3959 | TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3960 | D.getName()) || |
| 3961 | // Once we're past the identifier, if the scope was bad, mark the |
| 3962 | // whole declarator bad. |
| 3963 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3964 | D.SetIdentifier(0, Tok.getLocation()); |
| 3965 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3966 | } else { |
| 3967 | // Parsed the unqualified-id; update range information and move along. |
| 3968 | if (D.getSourceRange().getBegin().isInvalid()) |
| 3969 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 3970 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3971 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3972 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 3973 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3974 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3975 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3976 | "There's a C++-specific check for tok::identifier above"); |
| 3977 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 3978 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 3979 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3980 | goto PastIdentifier; |
| 3981 | } |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3982 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3983 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3984 | // direct-declarator: '(' declarator ')' |
| 3985 | // direct-declarator: '(' attributes declarator ')' |
| 3986 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 3987 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3988 | |
| 3989 | // If the declarator was parenthesized, we entered the declarator |
| 3990 | // scope when parsing the parenthesized declarator, then exited |
| 3991 | // the scope already. Re-enter the scope, if we need to. |
| 3992 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 3993 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3994 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 3995 | if (!D.isInvalidType() && |
| 3996 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3997 | // Change the declaration context for name lookup, until this function |
| 3998 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 3999 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4000 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4001 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4002 | // This could be something simple like "int" (in which case the declarator |
| 4003 | // portion is empty), if an abstract-declarator is allowed. |
| 4004 | D.SetIdentifier(0, Tok.getLocation()); |
| 4005 | } else { |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 4006 | if (D.getContext() == Declarator::MemberContext) |
| 4007 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 4008 | << D.getDeclSpec().getSourceRange(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4009 | else if (getLangOpts().CPlusPlus) |
| 4010 | Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4011 | else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4012 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4013 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 4014 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4015 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4016 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4017 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4018 | assert(D.isPastIdentifier() && |
| 4019 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4020 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4021 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 4022 | if (D.hasName() && !D.getNumTypeObjects()) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4023 | MaybeParseCXX0XAttributes(D); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4024 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4025 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4026 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4027 | // Enter function-declaration scope, limiting any declarators to the |
| 4028 | // function prototype scope, including parameter declarators. |
| 4029 | ParseScope PrototypeScope(this, |
| 4030 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4031 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 4032 | // In such a case, check if we actually have a function declarator; if it |
| 4033 | // is not, the declarator has been fully parsed. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4034 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4035 | // When not in file scope, warn for ambiguous function declarators, just |
| 4036 | // in case the author intended it as a variable definition. |
| 4037 | bool warnIfAmbiguous = D.getContext() != Declarator::FileContext; |
| 4038 | if (!isCXXFunctionDeclarator(warnIfAmbiguous)) |
| 4039 | break; |
| 4040 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4041 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4042 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4043 | T.consumeOpen(); |
| 4044 | ParseFunctionDeclarator(D, attrs, T); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4045 | PrototypeScope.Exit(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4046 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4047 | ParseBracketDeclarator(D); |
| 4048 | } else { |
| 4049 | break; |
| 4050 | } |
| 4051 | } |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4052 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4053 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4054 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 4055 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4056 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4057 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 4058 | /// |
| 4059 | /// direct-declarator: |
| 4060 | /// '(' declarator ')' |
| 4061 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4062 | /// direct-declarator '(' parameter-type-list ')' |
| 4063 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4064 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4065 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4066 | /// |
| 4067 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4068 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4069 | T.consumeOpen(); |
| 4070 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4071 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4072 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4073 | // Eat any attributes before we look at whether this is a grouping or function |
| 4074 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 4075 | // the type being built up, for example: |
| 4076 | // int (__attribute__(()) *x)(long y) |
| 4077 | // If this ends up not being a grouping paren, the attribute applies to the |
| 4078 | // first argument, for example: |
| 4079 | // int (__attribute__(()) int x) |
| 4080 | // In either case, we need to eat any attributes to be able to determine what |
| 4081 | // sort of paren this is. |
| 4082 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4083 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4084 | bool RequiresArg = false; |
| 4085 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4086 | ParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4087 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4088 | // We require that the argument list (if this is a non-grouping paren) be |
| 4089 | // present even if the attribute list was empty. |
| 4090 | RequiresArg = true; |
| 4091 | } |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4092 | // Eat any Microsoft extensions. |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4093 | if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4094 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4095 | Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4096 | Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4097 | ParseMicrosoftTypeAttributes(attrs); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4098 | } |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4099 | // Eat any Borland extensions. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 4100 | if (Tok.is(tok::kw___pascal)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4101 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4102 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4103 | // If we haven't past the identifier yet (or where the identifier would be |
| 4104 | // stored, if this is an abstract declarator), then this is probably just |
| 4105 | // grouping parens. However, if this could be an abstract-declarator, then |
| 4106 | // this could also be the start of function arguments (consider 'void()'). |
| 4107 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4108 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4109 | if (!D.mayOmitIdentifier()) { |
| 4110 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 4111 | // paren, because we haven't seen the identifier yet. |
| 4112 | isGrouping = true; |
| 4113 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4114 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 4115 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4116 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 4117 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4118 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 4119 | // considered to be a type, not a K&R identifier-list. |
| 4120 | isGrouping = false; |
| 4121 | } else { |
| 4122 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 4123 | isGrouping = true; |
| 4124 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4125 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4126 | // If this is a grouping paren, handle: |
| 4127 | // direct-declarator: '(' declarator ')' |
| 4128 | // direct-declarator: '(' attributes declarator ')' |
| 4129 | if (isGrouping) { |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4130 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 4131 | D.setEllipsisLoc(SourceLocation()); |
| 4132 | |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4133 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4134 | D.setGroupingParens(true); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4135 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4136 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4137 | T.consumeClose(); |
| 4138 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
| 4139 | T.getCloseLocation()), |
| 4140 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4141 | |
| 4142 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4143 | |
| 4144 | // An ellipsis cannot be placed outside parentheses. |
| 4145 | if (EllipsisLoc.isValid()) |
| 4146 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4147 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4148 | return; |
| 4149 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4150 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4151 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 4152 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4153 | // identifier (and remember where it would have been), then call into |
| 4154 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4155 | D.SetIdentifier(0, Tok.getLocation()); |
| 4156 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4157 | // Enter function-declaration scope, limiting any declarators to the |
| 4158 | // function prototype scope, including parameter declarators. |
| 4159 | ParseScope PrototypeScope(this, |
| 4160 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4161 | ParseFunctionDeclarator(D, attrs, T, RequiresArg); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4162 | PrototypeScope.Exit(); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4163 | } |
| 4164 | |
| 4165 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 4166 | /// declarator D up to a paren, which indicates that we are parsing function |
| 4167 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4168 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4169 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 4170 | /// immediately after the open paren - they should be considered to be the |
| 4171 | /// first argument of a parameter. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4172 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4173 | /// If RequiresArg is true, then the first argument of the function is required |
| 4174 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4175 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4176 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 4177 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 4178 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 4179 | /// |
| 4180 | /// [C++11] exception-specification: |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4181 | /// dynamic-exception-specification |
| 4182 | /// noexcept-specification |
| 4183 | /// |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4184 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4185 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4186 | BalancedDelimiterTracker &Tracker, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4187 | bool RequiresArg) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4188 | assert(getCurScope()->isFunctionPrototypeScope() && |
| 4189 | "Should call from a Function scope"); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4190 | // lparen is already consumed! |
| 4191 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 4192 | |
| 4193 | // This should be true when the function has typed arguments. |
| 4194 | // Otherwise, it is treated as a K&R-style function. |
| 4195 | bool HasProto = false; |
| 4196 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4197 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4198 | // Remember where we see an ellipsis, if any. |
| 4199 | SourceLocation EllipsisLoc; |
| 4200 | |
| 4201 | DeclSpec DS(AttrFactory); |
| 4202 | bool RefQualifierIsLValueRef = true; |
| 4203 | SourceLocation RefQualifierLoc; |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4204 | SourceLocation ConstQualifierLoc; |
| 4205 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4206 | ExceptionSpecificationType ESpecType = EST_None; |
| 4207 | SourceRange ESpecRange; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4208 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 4209 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4210 | ExprResult NoexceptExpr; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4211 | ParsedAttributes FnAttrs(AttrFactory); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4212 | ParsedType TrailingReturnType; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4213 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4214 | Actions.ActOnStartFunctionDeclarator(); |
| 4215 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4216 | SourceLocation EndLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4217 | if (isFunctionDeclaratorIdentifierList()) { |
| 4218 | if (RequiresArg) |
| 4219 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4220 | |
| 4221 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 4222 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4223 | Tracker.consumeClose(); |
| 4224 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4225 | } else { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4226 | if (Tok.isNot(tok::r_paren)) |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4227 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4228 | else if (RequiresArg) |
| 4229 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4230 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4231 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4232 | |
| 4233 | // If we have the closing ')', eat it. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4234 | Tracker.consumeClose(); |
| 4235 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4236 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4237 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4238 | // FIXME: Accept these components in any order, and produce fixits to |
| 4239 | // correct the order if the user gets it wrong. Ideally we should deal |
| 4240 | // with the virt-specifier-seq and pure-specifier in the same way. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4241 | |
| 4242 | // Parse cv-qualifier-seq[opt]. |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4243 | ParseTypeQualifierListOpt(DS, false /*no attributes*/, false); |
| 4244 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 4245 | EndLoc = DS.getSourceRange().getEnd(); |
| 4246 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 4247 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 4248 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4249 | |
| 4250 | // Parse ref-qualifier[opt]. |
| 4251 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4252 | Diag(Tok, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4253 | diag::warn_cxx98_compat_ref_qualifier : |
| 4254 | diag::ext_ref_qualifier); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4255 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4256 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 4257 | RefQualifierLoc = ConsumeToken(); |
| 4258 | EndLoc = RefQualifierLoc; |
| 4259 | } |
| 4260 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4261 | // C++11 [expr.prim.general]p3: |
| 4262 | // If a declaration declares a member function or member function |
| 4263 | // template of a class X, the expression this is a prvalue of type |
| 4264 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
| 4265 | // and the end of the function-definition, member-declarator, or |
| 4266 | // declarator. |
| 4267 | bool IsCXX11MemberFunction = |
| 4268 | getLangOpts().CPlusPlus0x && |
| 4269 | (D.getContext() == Declarator::MemberContext || |
| 4270 | (D.getContext() == Declarator::FileContext && |
| 4271 | D.getCXXScopeSpec().isValid() && |
| 4272 | Actions.CurContext->isRecord())); |
| 4273 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 4274 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
| 4275 | DS.getTypeQualifiers(), |
| 4276 | IsCXX11MemberFunction); |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 4277 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4278 | // Parse exception-specification[opt]. |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 4279 | ESpecType = tryParseExceptionSpecification(ESpecRange, |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 4280 | DynamicExceptions, |
| 4281 | DynamicExceptionRanges, |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 4282 | NoexceptExpr); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4283 | if (ESpecType != EST_None) |
| 4284 | EndLoc = ESpecRange.getEnd(); |
| 4285 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4286 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 4287 | // after the exception-specification. |
| 4288 | MaybeParseCXX0XAttributes(FnAttrs); |
| 4289 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4290 | // Parse trailing-return-type[opt]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4291 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4292 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 4293 | SourceRange Range; |
| 4294 | TrailingReturnType = ParseTrailingReturnType(Range).get(); |
| 4295 | if (Range.getEnd().isValid()) |
| 4296 | EndLoc = Range.getEnd(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4297 | } |
| 4298 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4299 | } |
| 4300 | |
| 4301 | // Remember that we parsed a function type, and remember the attributes. |
| 4302 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
| 4303 | /*isVariadic=*/EllipsisLoc.isValid(), |
| 4304 | EllipsisLoc, |
| 4305 | ParamInfo.data(), ParamInfo.size(), |
| 4306 | DS.getTypeQualifiers(), |
| 4307 | RefQualifierIsLValueRef, |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4308 | RefQualifierLoc, ConstQualifierLoc, |
| 4309 | VolatileQualifierLoc, |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 4310 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4311 | ESpecType, ESpecRange.getBegin(), |
| 4312 | DynamicExceptions.data(), |
| 4313 | DynamicExceptionRanges.data(), |
| 4314 | DynamicExceptions.size(), |
| 4315 | NoexceptExpr.isUsable() ? |
| 4316 | NoexceptExpr.get() : 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4317 | Tracker.getOpenLocation(), |
| 4318 | EndLoc, D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4319 | TrailingReturnType), |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4320 | FnAttrs, EndLoc); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4321 | |
| 4322 | Actions.ActOnEndFunctionDeclarator(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4323 | } |
| 4324 | |
| 4325 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 4326 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 4327 | /// |
| 4328 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 4329 | /// abstract-declarators. |
| 4330 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4331 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4332 | && Tok.is(tok::identifier) |
| 4333 | && !TryAltiVecVectorToken() |
| 4334 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 4335 | // 6.7.5.3p11. |
| 4336 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 4337 | // Identifier lists follow a really simple grammar: the identifiers can |
| 4338 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 4339 | // identifier lists are really rare in the brave new modern world, and |
| 4340 | // it is very common for someone to typo a type in a non-K&R style |
| 4341 | // list. If we are presented with something like: "void foo(intptr x, |
| 4342 | // float y)", we don't want to start parsing the function declarator as |
| 4343 | // though it is a K&R style declarator just because intptr is an |
| 4344 | // invalid type. |
| 4345 | // |
| 4346 | // To handle this, we check to see if the token after the first |
| 4347 | // identifier is a "," or ")". Only then do we parse it as an |
| 4348 | // identifier list. |
| 4349 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 4350 | } |
| 4351 | |
| 4352 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 4353 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 4354 | /// |
| 4355 | /// After returning, ParamInfo will hold the parsed parameters. |
| 4356 | /// |
| 4357 | /// identifier-list: [C99 6.7.5] |
| 4358 | /// identifier |
| 4359 | /// identifier-list ',' identifier |
| 4360 | /// |
| 4361 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 4362 | Declarator &D, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4363 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4364 | // If there was no identifier specified for the declarator, either we are in |
| 4365 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 4366 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 4367 | // diagnose this. |
| 4368 | if (!D.getIdentifier()) |
| 4369 | Diag(Tok, diag::ext_ident_list_in_param); |
| 4370 | |
| 4371 | // Maintain an efficient lookup of params we have seen so far. |
| 4372 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 4373 | |
| 4374 | while (1) { |
| 4375 | // If this isn't an identifier, report the error and skip until ')'. |
| 4376 | if (Tok.isNot(tok::identifier)) { |
| 4377 | Diag(Tok, diag::err_expected_ident); |
| 4378 | SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 4379 | // Forget we parsed anything. |
| 4380 | ParamInfo.clear(); |
| 4381 | return; |
| 4382 | } |
| 4383 | |
| 4384 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 4385 | |
| 4386 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 4387 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 4388 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 4389 | |
| 4390 | // Verify that the argument identifier has not already been mentioned. |
| 4391 | if (!ParamsSoFar.insert(ParmII)) { |
| 4392 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 4393 | } else { |
| 4394 | // Remember this identifier in ParamInfo. |
| 4395 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4396 | Tok.getLocation(), |
| 4397 | 0)); |
| 4398 | } |
| 4399 | |
| 4400 | // Eat the identifier. |
| 4401 | ConsumeToken(); |
| 4402 | |
| 4403 | // The list continues if we see a comma. |
| 4404 | if (Tok.isNot(tok::comma)) |
| 4405 | break; |
| 4406 | ConsumeToken(); |
| 4407 | } |
| 4408 | } |
| 4409 | |
| 4410 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 4411 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 4412 | /// identifier list. |
| 4413 | /// |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4414 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 4415 | /// caller parsed those arguments immediately after the open paren - they should |
| 4416 | /// be considered to be part of the first parameter. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4417 | /// |
| 4418 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 4419 | /// be the location of the ellipsis, if any was parsed. |
| 4420 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4421 | /// parameter-type-list: [C99 6.7.5] |
| 4422 | /// parameter-list |
| 4423 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4424 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4425 | /// |
| 4426 | /// parameter-list: [C99 6.7.5] |
| 4427 | /// parameter-declaration |
| 4428 | /// parameter-list ',' parameter-declaration |
| 4429 | /// |
| 4430 | /// parameter-declaration: [C99 6.7.5] |
| 4431 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4432 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4433 | /// [C++11] initializer-clause |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4434 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 4435 | /// declaration-specifiers abstract-declarator[opt] |
| 4436 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 4437 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4438 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4439 | /// [C++11] attribute-specifier-seq parameter-declaration |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4440 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4441 | void Parser::ParseParameterDeclarationClause( |
| 4442 | Declarator &D, |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4443 | ParsedAttributes &FirstArgAttrs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4444 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4445 | SourceLocation &EllipsisLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4446 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4447 | while (1) { |
| 4448 | if (Tok.is(tok::ellipsis)) { |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4449 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 4450 | // before deciding this was a parameter-declaration-clause. |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 4451 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4452 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4453 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4454 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4455 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4456 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4457 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4458 | |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4459 | // Parse any C++11 attributes. |
| 4460 | MaybeParseCXX0XAttributes(DS.getAttributes()); |
| 4461 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4462 | // Skip any Microsoft attributes before a param. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4463 | if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4464 | ParseMicrosoftAttributes(DS.getAttributes()); |
| 4465 | |
| 4466 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4467 | |
| 4468 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4469 | // 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] | 4470 | // 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] | 4471 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 4472 | // too much hassle. |
| 4473 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4474 | |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4475 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4476 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4477 | // Parse the declarator. This is "PrototypeContext", because we must |
| 4478 | // accept either 'declarator' or 'abstract-declarator' here. |
| 4479 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 4480 | ParseDeclarator(ParmDecl); |
| 4481 | |
| 4482 | // Parse GNU attributes, if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4483 | MaybeParseGNUAttributes(ParmDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4484 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4485 | // Remember this parsed parameter in ParamInfo. |
| 4486 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4487 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4488 | // DefArgToks is used when the parsing of default arguments needs |
| 4489 | // to be delayed. |
| 4490 | CachedTokens *DefArgToks = 0; |
| 4491 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4492 | // If no parameter was specified, verify that *something* was specified, |
| 4493 | // otherwise we have a missing type and identifier. |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4494 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 4495 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4496 | // Completely missing, emit error. |
| 4497 | Diag(DSStart, diag::err_missing_param); |
| 4498 | } else { |
| 4499 | // Otherwise, we have something. Add it and let semantic analysis try |
| 4500 | // 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] | 4501 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4502 | // Inform the actions module about the parameter declarator, so it gets |
| 4503 | // added to the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4504 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4505 | |
| 4506 | // Parse the default argument, if any. We parse the default |
| 4507 | // arguments in all dialects; the semantic analysis in |
| 4508 | // ActOnParamDefaultArgument will reject the default argument in |
| 4509 | // C. |
| 4510 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4511 | SourceLocation EqualLoc = Tok.getLocation(); |
| 4512 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4513 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4514 | if (D.getContext() == Declarator::MemberContext) { |
| 4515 | // If we're inside a class definition, cache the tokens |
| 4516 | // corresponding to the default argument. We'll actually parse |
| 4517 | // them when we see the end of the class definition. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4518 | // FIXME: Can we use a smart pointer for Toks? |
| 4519 | DefArgToks = new CachedTokens; |
| 4520 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4521 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 4522 | /*StopAtSemi=*/true, |
| 4523 | /*ConsumeFinalToken=*/false)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4524 | delete DefArgToks; |
| 4525 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4526 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4527 | } else { |
| 4528 | // Mark the end of the default argument so that we know when to |
| 4529 | // stop when we parse it later on. |
| 4530 | Token DefArgEnd; |
| 4531 | DefArgEnd.startToken(); |
| 4532 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 4533 | DefArgEnd.setLocation(Tok.getLocation()); |
| 4534 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4535 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4536 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4537 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4538 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4539 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4540 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4541 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4542 | // The argument isn't actually potentially evaluated unless it is |
| 4543 | // used. |
| 4544 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 4545 | Sema::PotentiallyEvaluatedIfUsed, |
| 4546 | Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4547 | |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4548 | ExprResult DefArgResult; |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 4549 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) { |
| 4550 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4551 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 4552 | } else |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4553 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4554 | if (DefArgResult.isInvalid()) { |
| 4555 | Actions.ActOnParamDefaultArgumentError(Param); |
| 4556 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 4557 | } else { |
| 4558 | // Inform the actions module about the default argument |
| 4559 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4560 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4561 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4562 | } |
| 4563 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4564 | |
| 4565 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4566 | ParmDecl.getIdentifierLoc(), Param, |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4567 | DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4568 | } |
| 4569 | |
| 4570 | // 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] | 4571 | if (Tok.isNot(tok::comma)) { |
| 4572 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4573 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
| 4574 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4575 | if (!getLangOpts().CPlusPlus) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4576 | // We have ellipsis without a preceding ',', which is ill-formed |
| 4577 | // in C. Complain and provide the fix. |
| 4578 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4579 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4580 | } |
| 4581 | } |
| 4582 | |
| 4583 | break; |
| 4584 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4585 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4586 | // Consume the comma. |
| 4587 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4588 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4589 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 4590 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4591 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4592 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4593 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4594 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4595 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4596 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4597 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 4598 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4599 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4600 | if (CheckProhibitedCXX11Attribute()) |
| 4601 | return; |
| 4602 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4603 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 4604 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4605 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4606 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 4607 | // This code does a fast path to handle some of the most obvious cases. |
| 4608 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4609 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4610 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4611 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4612 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4613 | // Remember that we parsed the empty array type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4614 | ExprResult NumElements; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4615 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4616 | T.getOpenLocation(), |
| 4617 | T.getCloseLocation()), |
| 4618 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4619 | return; |
| 4620 | } else if (Tok.getKind() == tok::numeric_constant && |
| 4621 | GetLookAheadToken(1).is(tok::r_square)) { |
| 4622 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 4623 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4624 | ConsumeToken(); |
| 4625 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4626 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4627 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4628 | MaybeParseCXX0XAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4629 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4630 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4631 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4632 | ExprRes.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4633 | T.getOpenLocation(), |
| 4634 | T.getCloseLocation()), |
| 4635 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4636 | return; |
| 4637 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4638 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4639 | // If valid, this location is the position where we read the 'static' keyword. |
| 4640 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4641 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4642 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4643 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4644 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4645 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4646 | DeclSpec DS(AttrFactory); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4647 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4648 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4649 | // If we haven't already read 'static', check to see if there is one after the |
| 4650 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4651 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4652 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4653 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4654 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 4655 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4656 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4657 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4658 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 4659 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
| 4660 | // the the token after the star is a ']'. Since stars in arrays are |
| 4661 | // infrequent, use of lookahead is not costly here. |
| 4662 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 4663 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4664 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4665 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4666 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4667 | StaticLoc = SourceLocation(); // Drop the static. |
| 4668 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4669 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4670 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4671 | // Note, in C89, this production uses the constant-expr production instead |
| 4672 | // of assignment-expr. The only difference is that assignment-expr allows |
| 4673 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 4674 | // 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] | 4675 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4676 | // Parse the constant-expression or assignment-expression now (depending |
| 4677 | // on dialect). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4678 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4679 | NumElements = ParseConstantExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4680 | } else { |
| 4681 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 4682 | Sema::ConstantEvaluated); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4683 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4684 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4685 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4686 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4687 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 4688 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 4689 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4690 | // If the expression was invalid, skip it. |
| 4691 | SkipUntil(tok::r_square); |
| 4692 | return; |
| 4693 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4694 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4695 | T.consumeClose(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4696 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4697 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4698 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4699 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4700 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4701 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4702 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4703 | NumElements.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4704 | T.getOpenLocation(), |
| 4705 | T.getCloseLocation()), |
| 4706 | attrs, T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4707 | } |
| 4708 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4709 | /// [GNU] typeof-specifier: |
| 4710 | /// typeof ( expressions ) |
| 4711 | /// typeof ( type-name ) |
| 4712 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4713 | /// |
| 4714 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4715 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4716 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4717 | SourceLocation StartLoc = ConsumeToken(); |
| 4718 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 4719 | const bool hasParens = Tok.is(tok::l_paren); |
| 4720 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4721 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
| 4722 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4723 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4724 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4725 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4726 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 4727 | CastTy, CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 4728 | if (hasParens) |
| 4729 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4730 | |
| 4731 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4732 | // FIXME: Not accurate, the range gets one token more than it should. |
| 4733 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4734 | else |
| 4735 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4736 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4737 | if (isCastExpr) { |
| 4738 | if (!CastTy) { |
| 4739 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4740 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 4741 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4742 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4743 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4744 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4745 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 4746 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4747 | DiagID, CastTy)) |
| 4748 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4749 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4750 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4751 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4752 | // If we get here, the operand to the typeof was an expresion. |
| 4753 | if (Operand.isInvalid()) { |
| 4754 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 4755 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4756 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4757 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4758 | // We might need to transform the operand if it is potentially evaluated. |
| 4759 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 4760 | if (Operand.isInvalid()) { |
| 4761 | DS.SetTypeSpecError(); |
| 4762 | return; |
| 4763 | } |
| 4764 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4765 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4766 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4767 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 4768 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4769 | DiagID, Operand.get())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4770 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4771 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4772 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 4773 | /// [C11] atomic-specifier: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4774 | /// _Atomic ( type-name ) |
| 4775 | /// |
| 4776 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
| 4777 | assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier"); |
| 4778 | |
| 4779 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4780 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4781 | if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) { |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4782 | SkipUntil(tok::r_paren); |
| 4783 | return; |
| 4784 | } |
| 4785 | |
| 4786 | TypeResult Result = ParseTypeName(); |
| 4787 | if (Result.isInvalid()) { |
| 4788 | SkipUntil(tok::r_paren); |
| 4789 | return; |
| 4790 | } |
| 4791 | |
| 4792 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4793 | T.consumeClose(); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4794 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4795 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4796 | return; |
| 4797 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4798 | DS.setTypeofParensRange(T.getRange()); |
| 4799 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4800 | |
| 4801 | const char *PrevSpec = 0; |
| 4802 | unsigned DiagID; |
| 4803 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
| 4804 | DiagID, Result.release())) |
| 4805 | Diag(StartLoc, DiagID) << PrevSpec; |
| 4806 | } |
| 4807 | |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4808 | |
| 4809 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 4810 | /// from TryAltiVecVectorToken. |
| 4811 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 4812 | Token Next = NextToken(); |
| 4813 | switch (Next.getKind()) { |
| 4814 | default: return false; |
| 4815 | case tok::kw_short: |
| 4816 | case tok::kw_long: |
| 4817 | case tok::kw_signed: |
| 4818 | case tok::kw_unsigned: |
| 4819 | case tok::kw_void: |
| 4820 | case tok::kw_char: |
| 4821 | case tok::kw_int: |
| 4822 | case tok::kw_float: |
| 4823 | case tok::kw_double: |
| 4824 | case tok::kw_bool: |
| 4825 | case tok::kw___pixel: |
| 4826 | Tok.setKind(tok::kw___vector); |
| 4827 | return true; |
| 4828 | case tok::identifier: |
| 4829 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 4830 | Tok.setKind(tok::kw___vector); |
| 4831 | return true; |
| 4832 | } |
| 4833 | return false; |
| 4834 | } |
| 4835 | } |
| 4836 | |
| 4837 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 4838 | const char *&PrevSpec, unsigned &DiagID, |
| 4839 | bool &isInvalid) { |
| 4840 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 4841 | Token Next = NextToken(); |
| 4842 | switch (Next.getKind()) { |
| 4843 | case tok::kw_short: |
| 4844 | case tok::kw_long: |
| 4845 | case tok::kw_signed: |
| 4846 | case tok::kw_unsigned: |
| 4847 | case tok::kw_void: |
| 4848 | case tok::kw_char: |
| 4849 | case tok::kw_int: |
| 4850 | case tok::kw_float: |
| 4851 | case tok::kw_double: |
| 4852 | case tok::kw_bool: |
| 4853 | case tok::kw___pixel: |
| 4854 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 4855 | return true; |
| 4856 | case tok::identifier: |
| 4857 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 4858 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 4859 | return true; |
| 4860 | } |
| 4861 | break; |
| 4862 | default: |
| 4863 | break; |
| 4864 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 4865 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4866 | DS.isTypeAltiVecVector()) { |
| 4867 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 4868 | return true; |
| 4869 | } |
| 4870 | return false; |
| 4871 | } |