Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- ParseDecl.cpp - Declaration Parsing ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Declaration portions of the Parser interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Parse/Parser.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 15 | #include "clang/Parse/ParseDiagnostic.h" |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 16 | #include "clang/Basic/OpenCL.h" |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 17 | #include "clang/Sema/Lookup.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 18 | #include "clang/Sema/Scope.h" |
| 19 | #include "clang/Sema/ParsedTemplate.h" |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 20 | #include "clang/Sema/PrettyDeclStackTrace.h" |
Chris Lattner | d167ca0 | 2009-12-10 00:21:05 +0000 | [diff] [blame] | 21 | #include "RAIIObjectsForParser.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/SmallSet.h" |
Benjamin Kramer | 8fe83e1 | 2012-02-04 13:45:25 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/SmallString.h" |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/StringSwitch.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | // C99 6.7: Declarations. |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
| 31 | /// ParseTypeName |
| 32 | /// type-name: [C99 6.7.6] |
| 33 | /// specifier-qualifier-list abstract-declarator[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 34 | /// |
| 35 | /// Called type-id in C++. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 36 | TypeResult Parser::ParseTypeName(SourceRange *Range, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 37 | Declarator::TheContext Context, |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 38 | AccessSpecifier AS, |
| 39 | Decl **OwnedType) { |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 40 | DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context); |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 41 | if (DSC == DSC_normal) |
| 42 | DSC = DSC_type_specifier; |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 43 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | // Parse the common declaration-specifiers piece. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 45 | DeclSpec DS(AttrFactory); |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 46 | ParseSpecifierQualifierList(DS, AS, DSC); |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 47 | if (OwnedType) |
| 48 | *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0; |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 49 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 50 | // Parse the abstract-declarator, if present. |
Douglas Gregor | 683a81f | 2011-01-31 16:09:46 +0000 | [diff] [blame] | 51 | Declarator DeclaratorInfo(DS, Context); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 52 | ParseDeclarator(DeclaratorInfo); |
Sebastian Redl | ef65f06 | 2009-05-29 18:02:33 +0000 | [diff] [blame] | 53 | if (Range) |
| 54 | *Range = DeclaratorInfo.getSourceRange(); |
| 55 | |
Chris Lattner | eaaebc7 | 2009-04-25 08:06:05 +0000 | [diff] [blame] | 56 | if (DeclaratorInfo.isInvalidType()) |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 57 | return true; |
| 58 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 59 | return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 62 | |
| 63 | /// isAttributeLateParsed - Return true if the attribute has arguments that |
| 64 | /// require late parsing. |
| 65 | static bool isAttributeLateParsed(const IdentifierInfo &II) { |
| 66 | return llvm::StringSwitch<bool>(II.getName()) |
| 67 | #include "clang/Parse/AttrLateParsed.inc" |
| 68 | .Default(false); |
| 69 | } |
| 70 | |
| 71 | |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 72 | /// ParseGNUAttributes - Parse a non-empty attributes list. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 73 | /// |
| 74 | /// [GNU] attributes: |
| 75 | /// attribute |
| 76 | /// attributes attribute |
| 77 | /// |
| 78 | /// [GNU] attribute: |
| 79 | /// '__attribute__' '(' '(' attribute-list ')' ')' |
| 80 | /// |
| 81 | /// [GNU] attribute-list: |
| 82 | /// attrib |
| 83 | /// attribute_list ',' attrib |
| 84 | /// |
| 85 | /// [GNU] attrib: |
| 86 | /// empty |
| 87 | /// attrib-name |
| 88 | /// attrib-name '(' identifier ')' |
| 89 | /// attrib-name '(' identifier ',' nonempty-expr-list ')' |
| 90 | /// attrib-name '(' argument-expression-list [C99 6.5.2] ')' |
| 91 | /// |
| 92 | /// [GNU] attrib-name: |
| 93 | /// identifier |
| 94 | /// typespec |
| 95 | /// typequal |
| 96 | /// storageclass |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 97 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 98 | /// FIXME: The GCC grammar/code for this construct implies we need two |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 99 | /// token lookahead. Comment from gcc: "If they start with an identifier |
| 100 | /// which is followed by a comma or close parenthesis, then the arguments |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 101 | /// start with that identifier; otherwise they are an expression list." |
| 102 | /// |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 103 | /// GCC does not require the ',' between attribs in an attribute-list. |
| 104 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 105 | /// At the moment, I am not doing 2 token lookahead. I am also unaware of |
| 106 | /// any attributes that don't work (based on my limited testing). Most |
| 107 | /// attributes are very simple in practice. Until we find a bug, I don't see |
| 108 | /// a pressing need to implement the 2 token lookahead. |
| 109 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 110 | void Parser::ParseGNUAttributes(ParsedAttributes &attrs, |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 111 | SourceLocation *endLoc, |
| 112 | LateParsedAttrList *LateAttrs) { |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 113 | assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 114 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 115 | while (Tok.is(tok::kw___attribute)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 116 | ConsumeToken(); |
| 117 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 118 | "attribute")) { |
| 119 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 120 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 121 | } |
| 122 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) { |
| 123 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 124 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 125 | } |
| 126 | // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") )) |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 127 | while (Tok.is(tok::identifier) || isDeclarationSpecifier() || |
| 128 | Tok.is(tok::comma)) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 129 | if (Tok.is(tok::comma)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 130 | // allows for empty/non-empty attributes. ((__vector_size__(16),,,,)) |
| 131 | ConsumeToken(); |
| 132 | continue; |
| 133 | } |
| 134 | // we have an identifier or declaration specifier (const, int, etc.) |
| 135 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 136 | SourceLocation AttrNameLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 137 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 138 | if (Tok.is(tok::l_paren)) { |
| 139 | // handle "parameterized" attributes |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 140 | if (LateAttrs && isAttributeLateParsed(*AttrName)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 141 | LateParsedAttribute *LA = |
| 142 | new LateParsedAttribute(this, *AttrName, AttrNameLoc); |
| 143 | LateAttrs->push_back(LA); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 144 | |
| 145 | // Attributes in a class are parsed at the end of the class, along |
| 146 | // with other late-parsed declarations. |
| 147 | if (!ClassStack.empty()) |
| 148 | getCurrentClass().LateParsedDeclarations.push_back(LA); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 150 | // consume everything up to and including the matching right parens |
| 151 | ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 153 | Token Eof; |
| 154 | Eof.startToken(); |
| 155 | Eof.setLocation(Tok.getLocation()); |
| 156 | LA->Toks.push_back(Eof); |
| 157 | } else { |
| 158 | ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 159 | } |
| 160 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 161 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 162 | 0, SourceLocation(), 0, 0); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 166 | SkipUntil(tok::r_paren, false); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 167 | SourceLocation Loc = Tok.getLocation(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 168 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 169 | SkipUntil(tok::r_paren, false); |
| 170 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 171 | if (endLoc) |
| 172 | *endLoc = Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 173 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 176 | |
| 177 | /// Parse the arguments to a parameterized GNU attribute |
| 178 | void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName, |
| 179 | SourceLocation AttrNameLoc, |
| 180 | ParsedAttributes &Attrs, |
| 181 | SourceLocation *EndLoc) { |
| 182 | |
| 183 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
| 184 | |
| 185 | // Availability attributes have their own grammar. |
| 186 | if (AttrName->isStr("availability")) { |
| 187 | ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 188 | return; |
| 189 | } |
| 190 | // Thread safety attributes fit into the FIXME case above, so we |
| 191 | // just parse the arguments as a list of expressions |
| 192 | if (IsThreadSafetyAttribute(AttrName->getName())) { |
| 193 | ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc); |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | ConsumeParen(); // ignore the left paren loc for now |
| 198 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 199 | IdentifierInfo *ParmName = 0; |
| 200 | SourceLocation ParmLoc; |
| 201 | bool BuiltinType = false; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 202 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 203 | switch (Tok.getKind()) { |
| 204 | case tok::kw_char: |
| 205 | case tok::kw_wchar_t: |
| 206 | case tok::kw_char16_t: |
| 207 | case tok::kw_char32_t: |
| 208 | case tok::kw_bool: |
| 209 | case tok::kw_short: |
| 210 | case tok::kw_int: |
| 211 | case tok::kw_long: |
| 212 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 213 | case tok::kw___int128: |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 214 | case tok::kw_signed: |
| 215 | case tok::kw_unsigned: |
| 216 | case tok::kw_float: |
| 217 | case tok::kw_double: |
| 218 | case tok::kw_void: |
| 219 | case tok::kw_typeof: |
| 220 | // __attribute__(( vec_type_hint(char) )) |
| 221 | // FIXME: Don't just discard the builtin type token. |
| 222 | ConsumeToken(); |
| 223 | BuiltinType = true; |
| 224 | break; |
| 225 | |
| 226 | case tok::identifier: |
| 227 | ParmName = Tok.getIdentifierInfo(); |
| 228 | ParmLoc = ConsumeToken(); |
| 229 | break; |
| 230 | |
| 231 | default: |
| 232 | break; |
| 233 | } |
| 234 | |
| 235 | ExprVector ArgExprs(Actions); |
| 236 | |
| 237 | if (!BuiltinType && |
| 238 | (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) { |
| 239 | // Eat the comma. |
| 240 | if (ParmLoc.isValid()) |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 241 | ConsumeToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 242 | |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 243 | // Parse the non-empty comma-separated list of expressions. |
| 244 | while (1) { |
| 245 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 246 | if (ArgExpr.isInvalid()) { |
| 247 | SkipUntil(tok::r_paren); |
| 248 | return; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 249 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 250 | ArgExprs.push_back(ArgExpr.release()); |
| 251 | if (Tok.isNot(tok::comma)) |
| 252 | break; |
| 253 | ConsumeToken(); // Eat the comma, move to the next argument |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 254 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 255 | } |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 256 | else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) { |
| 257 | if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<", |
| 258 | tok::greater)) { |
Fariborz Jahanian | b224343 | 2011-10-18 23:13:50 +0000 | [diff] [blame] | 259 | while (Tok.is(tok::identifier)) { |
| 260 | ConsumeToken(); |
| 261 | if (Tok.is(tok::greater)) |
| 262 | break; |
| 263 | if (Tok.is(tok::comma)) { |
| 264 | ConsumeToken(); |
| 265 | continue; |
| 266 | } |
| 267 | } |
| 268 | if (Tok.isNot(tok::greater)) |
| 269 | Diag(Tok, diag::err_iboutletcollection_with_protocol); |
Fariborz Jahanian | 7a81e41 | 2011-10-18 17:11:10 +0000 | [diff] [blame] | 270 | SkipUntil(tok::r_paren, false, true); // skip until ')' |
| 271 | } |
| 272 | } |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 273 | |
| 274 | SourceLocation RParen = Tok.getLocation(); |
| 275 | if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) { |
| 276 | AttributeList *attr = |
Argyrios Kyrtzidis | ffcc310 | 2011-09-13 16:05:53 +0000 | [diff] [blame] | 277 | Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc, |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 278 | ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size()); |
Michael Han | e53ac8a | 2012-03-07 00:12:16 +0000 | [diff] [blame] | 279 | if (BuiltinType && attr->getKind() == AttributeList::AT_iboutletcollection) |
Richard Smith | fe0a0fb | 2011-10-17 21:20:17 +0000 | [diff] [blame] | 280 | Diag(Tok, diag::err_iboutletcollection_builtintype); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | |
| 284 | |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 285 | /// ParseMicrosoftDeclSpec - Parse an __declspec construct |
| 286 | /// |
| 287 | /// [MS] decl-specifier: |
| 288 | /// __declspec ( extended-decl-modifier-seq ) |
| 289 | /// |
| 290 | /// [MS] extended-decl-modifier-seq: |
| 291 | /// extended-decl-modifier[opt] |
| 292 | /// extended-decl-modifier extended-decl-modifier-seq |
| 293 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 294 | void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &attrs) { |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 295 | assert(Tok.is(tok::kw___declspec) && "Not a declspec!"); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 296 | |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 297 | ConsumeToken(); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 298 | if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, |
| 299 | "declspec")) { |
| 300 | SkipUntil(tok::r_paren, true); // skip until ) or ; |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 301 | return; |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 302 | } |
Francois Pichet | 373197b | 2011-05-07 19:04:49 +0000 | [diff] [blame] | 303 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 304 | while (Tok.getIdentifierInfo()) { |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 305 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 306 | SourceLocation AttrNameLoc = ConsumeToken(); |
Francois Pichet | 373197b | 2011-05-07 19:04:49 +0000 | [diff] [blame] | 307 | |
| 308 | // FIXME: Remove this when we have proper __declspec(property()) support. |
| 309 | // Just skip everything inside property(). |
| 310 | if (AttrName->getName() == "property") { |
| 311 | ConsumeParen(); |
| 312 | SkipUntil(tok::r_paren); |
| 313 | } |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 314 | if (Tok.is(tok::l_paren)) { |
| 315 | ConsumeParen(); |
| 316 | // FIXME: This doesn't parse __declspec(property(get=get_func_name)) |
| 317 | // correctly. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 318 | ExprResult ArgExpr(ParseAssignmentExpression()); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 319 | if (!ArgExpr.isInvalid()) { |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 320 | Expr *ExprList = ArgExpr.take(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 321 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 322 | SourceLocation(), &ExprList, 1, true); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 323 | } |
| 324 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 325 | SkipUntil(tok::r_paren, false); |
| 326 | } else { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 327 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, |
| 328 | 0, SourceLocation(), 0, 0, true); |
Eli Friedman | a23b485 | 2009-06-08 07:21:15 +0000 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) |
| 332 | SkipUntil(tok::r_paren, false); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 333 | return; |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 334 | } |
| 335 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 336 | void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 337 | // Treat these like attributes |
| 338 | // FIXME: Allow Sema to distinguish between these and real attributes! |
| 339 | while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 340 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 341 | Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 342 | Tok.is(tok::kw___ptr32) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 343 | Tok.is(tok::kw___unaligned)) { |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 344 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 345 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 346 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 347 | SourceLocation(), 0, 0, true); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 348 | } |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 349 | } |
| 350 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 351 | void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) { |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 352 | // Treat these like attributes |
| 353 | while (Tok.is(tok::kw___pascal)) { |
| 354 | IdentifierInfo *AttrName = Tok.getIdentifierInfo(); |
| 355 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 356 | attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, |
| 357 | SourceLocation(), 0, 0, true); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 358 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 359 | } |
| 360 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 361 | void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) { |
| 362 | // Treat these like attributes |
| 363 | while (Tok.is(tok::kw___kernel)) { |
| 364 | SourceLocation AttrNameLoc = ConsumeToken(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 365 | attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"), |
| 366 | AttrNameLoc, 0, AttrNameLoc, 0, |
| 367 | SourceLocation(), 0, 0, false); |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 371 | void Parser::ParseOpenCLQualifiers(DeclSpec &DS) { |
| 372 | SourceLocation Loc = Tok.getLocation(); |
| 373 | switch(Tok.getKind()) { |
| 374 | // OpenCL qualifiers: |
| 375 | case tok::kw___private: |
| 376 | case tok::kw_private: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 377 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 378 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 379 | PP.getIdentifierInfo("address_space"), Loc, 0); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 380 | break; |
| 381 | |
| 382 | case tok::kw___global: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 383 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 384 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 385 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 386 | break; |
| 387 | |
| 388 | case tok::kw___local: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 389 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 390 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 391 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 392 | break; |
| 393 | |
| 394 | case tok::kw___constant: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 395 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 396 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 397 | PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 398 | break; |
| 399 | |
| 400 | case tok::kw___read_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 401 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 402 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 403 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 404 | break; |
| 405 | |
| 406 | case tok::kw___write_only: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 407 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 408 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 409 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 410 | break; |
| 411 | |
| 412 | case tok::kw___read_write: |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 413 | DS.getAttributes().addNewInteger( |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 414 | Actions.getASTContext(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 415 | PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write); |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 416 | break; |
| 417 | default: break; |
| 418 | } |
| 419 | } |
| 420 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 421 | /// \brief Parse a version number. |
| 422 | /// |
| 423 | /// version: |
| 424 | /// simple-integer |
| 425 | /// simple-integer ',' simple-integer |
| 426 | /// simple-integer ',' simple-integer ',' simple-integer |
| 427 | VersionTuple Parser::ParseVersionTuple(SourceRange &Range) { |
| 428 | Range = Tok.getLocation(); |
| 429 | |
| 430 | if (!Tok.is(tok::numeric_constant)) { |
| 431 | Diag(Tok, diag::err_expected_version); |
| 432 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 433 | return VersionTuple(); |
| 434 | } |
| 435 | |
| 436 | // Parse the major (and possibly minor and subminor) versions, which |
| 437 | // are stored in the numeric constant. We utilize a quirk of the |
| 438 | // lexer, which is that it handles something like 1.2.3 as a single |
| 439 | // numeric constant, rather than two separate tokens. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 440 | SmallString<512> Buffer; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 441 | Buffer.resize(Tok.getLength()+1); |
| 442 | const char *ThisTokBegin = &Buffer[0]; |
| 443 | |
| 444 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| 445 | bool Invalid = false; |
| 446 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 447 | if (Invalid) |
| 448 | return VersionTuple(); |
| 449 | |
| 450 | // Parse the major version. |
| 451 | unsigned AfterMajor = 0; |
| 452 | unsigned Major = 0; |
| 453 | while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) { |
| 454 | Major = Major * 10 + ThisTokBegin[AfterMajor] - '0'; |
| 455 | ++AfterMajor; |
| 456 | } |
| 457 | |
| 458 | if (AfterMajor == 0) { |
| 459 | Diag(Tok, diag::err_expected_version); |
| 460 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 461 | return VersionTuple(); |
| 462 | } |
| 463 | |
| 464 | if (AfterMajor == ActualLength) { |
| 465 | ConsumeToken(); |
| 466 | |
| 467 | // We only had a single version component. |
| 468 | if (Major == 0) { |
| 469 | Diag(Tok, diag::err_zero_version); |
| 470 | return VersionTuple(); |
| 471 | } |
| 472 | |
| 473 | return VersionTuple(Major); |
| 474 | } |
| 475 | |
| 476 | if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) { |
| 477 | Diag(Tok, diag::err_expected_version); |
| 478 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 479 | return VersionTuple(); |
| 480 | } |
| 481 | |
| 482 | // Parse the minor version. |
| 483 | unsigned AfterMinor = AfterMajor + 1; |
| 484 | unsigned Minor = 0; |
| 485 | while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) { |
| 486 | Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0'; |
| 487 | ++AfterMinor; |
| 488 | } |
| 489 | |
| 490 | if (AfterMinor == ActualLength) { |
| 491 | ConsumeToken(); |
| 492 | |
| 493 | // We had major.minor. |
| 494 | if (Major == 0 && Minor == 0) { |
| 495 | Diag(Tok, diag::err_zero_version); |
| 496 | return VersionTuple(); |
| 497 | } |
| 498 | |
| 499 | return VersionTuple(Major, Minor); |
| 500 | } |
| 501 | |
| 502 | // If what follows is not a '.', we have a problem. |
| 503 | if (ThisTokBegin[AfterMinor] != '.') { |
| 504 | Diag(Tok, diag::err_expected_version); |
| 505 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 506 | return VersionTuple(); |
| 507 | } |
| 508 | |
| 509 | // Parse the subminor version. |
| 510 | unsigned AfterSubminor = AfterMinor + 1; |
| 511 | unsigned Subminor = 0; |
| 512 | while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) { |
| 513 | Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0'; |
| 514 | ++AfterSubminor; |
| 515 | } |
| 516 | |
| 517 | if (AfterSubminor != ActualLength) { |
| 518 | Diag(Tok, diag::err_expected_version); |
| 519 | SkipUntil(tok::comma, tok::r_paren, true, true, true); |
| 520 | return VersionTuple(); |
| 521 | } |
| 522 | ConsumeToken(); |
| 523 | return VersionTuple(Major, Minor, Subminor); |
| 524 | } |
| 525 | |
| 526 | /// \brief Parse the contents of the "availability" attribute. |
| 527 | /// |
| 528 | /// availability-attribute: |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 529 | /// 'availability' '(' platform ',' version-arg-list, opt-message')' |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 530 | /// |
| 531 | /// platform: |
| 532 | /// identifier |
| 533 | /// |
| 534 | /// version-arg-list: |
| 535 | /// version-arg |
| 536 | /// version-arg ',' version-arg-list |
| 537 | /// |
| 538 | /// version-arg: |
| 539 | /// 'introduced' '=' version |
| 540 | /// 'deprecated' '=' version |
Douglas Gregor | 93a7067 | 2012-03-11 04:53:21 +0000 | [diff] [blame] | 541 | /// 'obsoleted' = version |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 542 | /// 'unavailable' |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 543 | /// opt-message: |
| 544 | /// 'message' '=' <string> |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 545 | void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability, |
| 546 | SourceLocation AvailabilityLoc, |
| 547 | ParsedAttributes &attrs, |
| 548 | SourceLocation *endLoc) { |
| 549 | SourceLocation PlatformLoc; |
| 550 | IdentifierInfo *Platform = 0; |
| 551 | |
| 552 | enum { Introduced, Deprecated, Obsoleted, Unknown }; |
| 553 | AvailabilityChange Changes[Unknown]; |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 554 | ExprResult MessageExpr; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 555 | |
| 556 | // Opening '('. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 557 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 558 | if (T.consumeOpen()) { |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 559 | Diag(Tok, diag::err_expected_lparen); |
| 560 | return; |
| 561 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 562 | |
| 563 | // Parse the platform name, |
| 564 | if (Tok.isNot(tok::identifier)) { |
| 565 | Diag(Tok, diag::err_availability_expected_platform); |
| 566 | SkipUntil(tok::r_paren); |
| 567 | return; |
| 568 | } |
| 569 | Platform = Tok.getIdentifierInfo(); |
| 570 | PlatformLoc = ConsumeToken(); |
| 571 | |
| 572 | // Parse the ',' following the platform name. |
| 573 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren)) |
| 574 | return; |
| 575 | |
| 576 | // If we haven't grabbed the pointers for the identifiers |
| 577 | // "introduced", "deprecated", and "obsoleted", do so now. |
| 578 | if (!Ident_introduced) { |
| 579 | Ident_introduced = PP.getIdentifierInfo("introduced"); |
| 580 | Ident_deprecated = PP.getIdentifierInfo("deprecated"); |
| 581 | Ident_obsoleted = PP.getIdentifierInfo("obsoleted"); |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 582 | Ident_unavailable = PP.getIdentifierInfo("unavailable"); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 583 | Ident_message = PP.getIdentifierInfo("message"); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 584 | } |
| 585 | |
| 586 | // Parse the set of introductions/deprecations/removals. |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 587 | SourceLocation UnavailableLoc; |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 588 | do { |
| 589 | if (Tok.isNot(tok::identifier)) { |
| 590 | Diag(Tok, diag::err_availability_expected_change); |
| 591 | SkipUntil(tok::r_paren); |
| 592 | return; |
| 593 | } |
| 594 | IdentifierInfo *Keyword = Tok.getIdentifierInfo(); |
| 595 | SourceLocation KeywordLoc = ConsumeToken(); |
| 596 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 597 | if (Keyword == Ident_unavailable) { |
| 598 | if (UnavailableLoc.isValid()) { |
| 599 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 600 | << Keyword << SourceRange(UnavailableLoc); |
| 601 | } |
| 602 | UnavailableLoc = KeywordLoc; |
| 603 | |
| 604 | if (Tok.isNot(tok::comma)) |
| 605 | break; |
| 606 | |
| 607 | ConsumeToken(); |
| 608 | continue; |
| 609 | } |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 610 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 611 | if (Tok.isNot(tok::equal)) { |
| 612 | Diag(Tok, diag::err_expected_equal_after) |
| 613 | << Keyword; |
| 614 | SkipUntil(tok::r_paren); |
| 615 | return; |
| 616 | } |
| 617 | ConsumeToken(); |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 618 | if (Keyword == Ident_message) { |
| 619 | if (!isTokenStringLiteral()) { |
| 620 | Diag(Tok, diag::err_expected_string_literal); |
| 621 | SkipUntil(tok::r_paren); |
| 622 | return; |
| 623 | } |
| 624 | MessageExpr = ParseStringLiteralExpression(); |
| 625 | break; |
| 626 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 627 | |
| 628 | SourceRange VersionRange; |
| 629 | VersionTuple Version = ParseVersionTuple(VersionRange); |
| 630 | |
| 631 | if (Version.empty()) { |
| 632 | SkipUntil(tok::r_paren); |
| 633 | return; |
| 634 | } |
| 635 | |
| 636 | unsigned Index; |
| 637 | if (Keyword == Ident_introduced) |
| 638 | Index = Introduced; |
| 639 | else if (Keyword == Ident_deprecated) |
| 640 | Index = Deprecated; |
| 641 | else if (Keyword == Ident_obsoleted) |
| 642 | Index = Obsoleted; |
| 643 | else |
| 644 | Index = Unknown; |
| 645 | |
| 646 | if (Index < Unknown) { |
| 647 | if (!Changes[Index].KeywordLoc.isInvalid()) { |
| 648 | Diag(KeywordLoc, diag::err_availability_redundant) |
| 649 | << Keyword |
| 650 | << SourceRange(Changes[Index].KeywordLoc, |
| 651 | Changes[Index].VersionRange.getEnd()); |
| 652 | } |
| 653 | |
| 654 | Changes[Index].KeywordLoc = KeywordLoc; |
| 655 | Changes[Index].Version = Version; |
| 656 | Changes[Index].VersionRange = VersionRange; |
| 657 | } else { |
| 658 | Diag(KeywordLoc, diag::err_availability_unknown_change) |
| 659 | << Keyword << VersionRange; |
| 660 | } |
| 661 | |
| 662 | if (Tok.isNot(tok::comma)) |
| 663 | break; |
| 664 | |
| 665 | ConsumeToken(); |
| 666 | } while (true); |
| 667 | |
| 668 | // Closing ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 669 | if (T.consumeClose()) |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 670 | return; |
| 671 | |
| 672 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 673 | *endLoc = T.getCloseLocation(); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 674 | |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 675 | // The 'unavailable' availability cannot be combined with any other |
| 676 | // availability changes. Make sure that hasn't happened. |
| 677 | if (UnavailableLoc.isValid()) { |
| 678 | bool Complained = false; |
| 679 | for (unsigned Index = Introduced; Index != Unknown; ++Index) { |
| 680 | if (Changes[Index].KeywordLoc.isValid()) { |
| 681 | if (!Complained) { |
| 682 | Diag(UnavailableLoc, diag::warn_availability_and_unavailable) |
| 683 | << SourceRange(Changes[Index].KeywordLoc, |
| 684 | Changes[Index].VersionRange.getEnd()); |
| 685 | Complained = true; |
| 686 | } |
| 687 | |
| 688 | // Clear out the availability. |
| 689 | Changes[Index] = AvailabilityChange(); |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 694 | // Record this attribute |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 695 | attrs.addNew(&Availability, |
| 696 | SourceRange(AvailabilityLoc, T.getCloseLocation()), |
Fariborz Jahanian | f96708d | 2012-01-23 23:38:32 +0000 | [diff] [blame] | 697 | 0, AvailabilityLoc, |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 698 | Platform, PlatformLoc, |
| 699 | Changes[Introduced], |
| 700 | Changes[Deprecated], |
Douglas Gregor | b53e417 | 2011-03-26 03:35:55 +0000 | [diff] [blame] | 701 | Changes[Obsoleted], |
Fariborz Jahanian | 006e42f | 2011-12-10 00:28:41 +0000 | [diff] [blame] | 702 | UnavailableLoc, MessageExpr.take(), |
| 703 | false, false); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 706 | |
| 707 | // Late Parsed Attributes: |
| 708 | // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods |
| 709 | |
| 710 | void Parser::LateParsedDeclaration::ParseLexedAttributes() {} |
| 711 | |
| 712 | void Parser::LateParsedClass::ParseLexedAttributes() { |
| 713 | Self->ParseLexedAttributes(*Class); |
| 714 | } |
| 715 | |
| 716 | void Parser::LateParsedAttribute::ParseLexedAttributes() { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 717 | Self->ParseLexedAttribute(*this, true, false); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | /// Wrapper class which calls ParseLexedAttribute, after setting up the |
| 721 | /// scope appropriately. |
| 722 | void Parser::ParseLexedAttributes(ParsingClass &Class) { |
| 723 | // Deal with templates |
| 724 | // FIXME: Test cases to make sure this does the right thing for templates. |
| 725 | bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope; |
| 726 | ParseScope ClassTemplateScope(this, Scope::TemplateParamScope, |
| 727 | HasTemplateScope); |
| 728 | if (HasTemplateScope) |
| 729 | Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate); |
| 730 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 731 | // Set or update the scope flags. |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 732 | bool AlreadyHasClassScope = Class.TopLevelClass; |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 733 | unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope; |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 734 | ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope); |
| 735 | ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope); |
| 736 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 737 | // Enter the scope of nested classes |
| 738 | if (!AlreadyHasClassScope) |
| 739 | Actions.ActOnStartDelayedMemberDeclarations(getCurScope(), |
| 740 | Class.TagOrTemplate); |
Benjamin Kramer | 268efba | 2012-05-17 12:01:52 +0000 | [diff] [blame] | 741 | if (!Class.LateParsedDeclarations.empty()) { |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 742 | // Allow 'this' within late-parsed attributes. |
| 743 | Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate, |
| 744 | /*TypeQuals=*/0); |
| 745 | |
| 746 | for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){ |
| 747 | Class.LateParsedDeclarations[i]->ParseLexedAttributes(); |
| 748 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 749 | } |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 750 | |
DeLesley Hutchins | cf2fa2f | 2012-04-06 15:10:17 +0000 | [diff] [blame] | 751 | if (!AlreadyHasClassScope) |
| 752 | Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(), |
| 753 | Class.TagOrTemplate); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 754 | } |
| 755 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 756 | |
| 757 | /// \brief Parse all attributes in LAs, and attach them to Decl D. |
| 758 | void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D, |
| 759 | bool EnterScope, bool OnDefinition) { |
| 760 | for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 761 | LAs[i]->addDecl(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 762 | ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition); |
Benjamin Kramer | d306cf7 | 2012-04-14 12:44:47 +0000 | [diff] [blame] | 763 | delete LAs[i]; |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 764 | } |
| 765 | LAs.clear(); |
| 766 | } |
| 767 | |
| 768 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 769 | /// \brief Finish parsing an attribute for which parsing was delayed. |
| 770 | /// This will be called at the end of parsing a class declaration |
| 771 | /// for each LateParsedAttribute. We consume the saved tokens and |
| 772 | /// create an attribute with the arguments filled in. We add this |
| 773 | /// to the Attribute list for the decl. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 774 | void Parser::ParseLexedAttribute(LateParsedAttribute &LA, |
| 775 | bool EnterScope, bool OnDefinition) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 776 | // Save the current token position. |
| 777 | SourceLocation OrigLoc = Tok.getLocation(); |
| 778 | |
| 779 | // Append the current token at the end of the new token stream so that it |
| 780 | // doesn't get lost. |
| 781 | LA.Toks.push_back(Tok); |
| 782 | PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false); |
| 783 | // Consume the previously pushed token. |
| 784 | ConsumeAnyToken(); |
| 785 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 786 | if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) { |
| 787 | Diag(Tok, diag::warn_attribute_on_function_definition) |
| 788 | << LA.AttrName.getName(); |
| 789 | } |
| 790 | |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 791 | ParsedAttributes Attrs(AttrFactory); |
| 792 | SourceLocation endLoc; |
| 793 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 794 | if (LA.Decls.size() == 1) { |
| 795 | Decl *D = LA.Decls[0]; |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 796 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 797 | // If the Decl is templatized, add template parameters to scope. |
| 798 | bool HasTemplateScope = EnterScope && D->isTemplateDecl(); |
| 799 | ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope); |
| 800 | if (HasTemplateScope) |
| 801 | Actions.ActOnReenterTemplateScope(Actions.CurScope, D); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 802 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 803 | // If the Decl is on a function, add function parameters to the scope. |
| 804 | bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate(); |
| 805 | ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope); |
| 806 | if (HasFunctionScope) |
| 807 | Actions.ActOnReenterFunctionContext(Actions.CurScope, D); |
| 808 | |
| 809 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc); |
| 810 | |
| 811 | if (HasFunctionScope) { |
| 812 | Actions.ActOnExitFunctionContext(); |
| 813 | FnScope.Exit(); // Pop scope, and remove Decls from IdResolver |
| 814 | } |
| 815 | if (HasTemplateScope) { |
| 816 | TempScope.Exit(); |
| 817 | } |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 818 | } else if (LA.Decls.size() > 0) { |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 819 | // If there are multiple decls, then the decl cannot be within the |
| 820 | // function scope. |
| 821 | ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc); |
DeLesley Hutchins | 7ec419a | 2012-03-02 22:29:50 +0000 | [diff] [blame] | 822 | } else { |
| 823 | Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName(); |
Caitlin Sadowski | ed9d84a | 2011-09-08 17:42:31 +0000 | [diff] [blame] | 824 | } |
| 825 | |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 826 | for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) { |
| 827 | Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs); |
| 828 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 829 | |
| 830 | if (Tok.getLocation() != OrigLoc) { |
| 831 | // Due to a parsing error, we either went over the cached tokens or |
| 832 | // there are still cached tokens left, so we skip the leftover tokens. |
| 833 | // Since this is an uncommon situation that should be avoided, use the |
| 834 | // expensive isBeforeInTranslationUnit call. |
| 835 | if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(), |
| 836 | OrigLoc)) |
| 837 | while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof)) |
Douglas Gregor | d78ef5b | 2012-03-08 01:00:17 +0000 | [diff] [blame] | 838 | ConsumeAnyToken(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 839 | } |
| 840 | } |
| 841 | |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 842 | /// \brief Wrapper around a case statement checking if AttrName is |
| 843 | /// one of the thread safety attributes |
| 844 | bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){ |
| 845 | return llvm::StringSwitch<bool>(AttrName) |
| 846 | .Case("guarded_by", true) |
| 847 | .Case("guarded_var", true) |
| 848 | .Case("pt_guarded_by", true) |
| 849 | .Case("pt_guarded_var", true) |
| 850 | .Case("lockable", true) |
| 851 | .Case("scoped_lockable", true) |
| 852 | .Case("no_thread_safety_analysis", true) |
| 853 | .Case("acquired_after", true) |
| 854 | .Case("acquired_before", true) |
| 855 | .Case("exclusive_lock_function", true) |
| 856 | .Case("shared_lock_function", true) |
| 857 | .Case("exclusive_trylock_function", true) |
| 858 | .Case("shared_trylock_function", true) |
| 859 | .Case("unlock_function", true) |
| 860 | .Case("lock_returned", true) |
| 861 | .Case("locks_excluded", true) |
| 862 | .Case("exclusive_locks_required", true) |
| 863 | .Case("shared_locks_required", true) |
| 864 | .Default(false); |
| 865 | } |
| 866 | |
| 867 | /// \brief Parse the contents of thread safety attributes. These |
| 868 | /// should always be parsed as an expression list. |
| 869 | /// |
| 870 | /// We need to special case the parsing due to the fact that if the first token |
| 871 | /// of the first argument is an identifier, the main parse loop will store |
| 872 | /// that token as a "parameter" and the rest of |
| 873 | /// the arguments will be added to a list of "arguments". However, |
| 874 | /// subsequent tokens in the first argument are lost. We instead parse each |
| 875 | /// argument as an expression and add all arguments to the list of "arguments". |
| 876 | /// In future, we will take advantage of this special case to also |
| 877 | /// deal with some argument scoping issues here (for example, referring to a |
| 878 | /// function parameter in the attribute on that function). |
| 879 | void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName, |
| 880 | SourceLocation AttrNameLoc, |
| 881 | ParsedAttributes &Attrs, |
| 882 | SourceLocation *EndLoc) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 883 | assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('"); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 884 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 885 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 886 | T.consumeOpen(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 887 | |
| 888 | ExprVector ArgExprs(Actions); |
| 889 | bool ArgExprsOk = true; |
| 890 | |
| 891 | // now parse the list of expressions |
DeLesley Hutchins | 4805f15 | 2011-12-14 19:36:06 +0000 | [diff] [blame] | 892 | while (Tok.isNot(tok::r_paren)) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 893 | ExprResult ArgExpr(ParseAssignmentExpression()); |
| 894 | if (ArgExpr.isInvalid()) { |
| 895 | ArgExprsOk = false; |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 896 | T.consumeClose(); |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 897 | break; |
| 898 | } else { |
| 899 | ArgExprs.push_back(ArgExpr.release()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 900 | } |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 901 | if (Tok.isNot(tok::comma)) |
| 902 | break; |
| 903 | ConsumeToken(); // Eat the comma, move to the next argument |
| 904 | } |
| 905 | // Match the ')'. |
DeLesley Hutchins | 23323e0 | 2012-01-20 22:50:54 +0000 | [diff] [blame] | 906 | if (ArgExprsOk && !T.consumeClose()) { |
Caitlin Sadowski | eff98fc | 2011-09-08 17:42:22 +0000 | [diff] [blame] | 907 | Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(), |
| 908 | ArgExprs.take(), ArgExprs.size()); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 909 | } |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 910 | if (EndLoc) |
| 911 | *EndLoc = T.getCloseLocation(); |
Caitlin Sadowski | b51e031 | 2011-08-09 17:59:31 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 914 | /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets |
| 915 | /// of a C++11 attribute-specifier in a location where an attribute is not |
| 916 | /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this |
| 917 | /// situation. |
| 918 | /// |
| 919 | /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if |
| 920 | /// this doesn't appear to actually be an attribute-specifier, and the caller |
| 921 | /// should try to parse it. |
| 922 | bool Parser::DiagnoseProhibitedCXX11Attribute() { |
| 923 | assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square)); |
| 924 | |
| 925 | switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) { |
| 926 | case CAK_NotAttributeSpecifier: |
| 927 | // No diagnostic: we're in Obj-C++11 and this is not actually an attribute. |
| 928 | return false; |
| 929 | |
| 930 | case CAK_InvalidAttributeSpecifier: |
| 931 | Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute); |
| 932 | return false; |
| 933 | |
| 934 | case CAK_AttributeSpecifier: |
| 935 | // Parse and discard the attributes. |
| 936 | SourceLocation BeginLoc = ConsumeBracket(); |
| 937 | ConsumeBracket(); |
| 938 | SkipUntil(tok::r_square, /*StopAtSemi*/ false); |
| 939 | assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied"); |
| 940 | SourceLocation EndLoc = ConsumeBracket(); |
| 941 | Diag(BeginLoc, diag::err_attributes_not_allowed) |
| 942 | << SourceRange(BeginLoc, EndLoc); |
| 943 | return true; |
| 944 | } |
Chandler Carruth | 2c6dbd7 | 2012-04-10 16:03:08 +0000 | [diff] [blame] | 945 | llvm_unreachable("All cases handled above."); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 946 | } |
| 947 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 948 | void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) { |
| 949 | Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed) |
| 950 | << attrs.Range; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 953 | /// ParseDeclaration - Parse a full 'declaration', which consists of |
| 954 | /// declaration-specifiers, some number of declarators, and a semicolon. |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 955 | /// 'Context' should be a Declarator::TheContext value. This returns the |
| 956 | /// location of the semicolon in DeclEnd. |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 957 | /// |
| 958 | /// declaration: [C99 6.7] |
| 959 | /// block-declaration -> |
| 960 | /// simple-declaration |
| 961 | /// others [FIXME] |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 962 | /// [C++] template-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 963 | /// [C++] namespace-definition |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 964 | /// [C++] using-directive |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 965 | /// [C++] using-declaration |
Richard Smith | 534986f | 2012-04-14 00:33:13 +0000 | [diff] [blame] | 966 | /// [C++11/C11] static_assert-declaration |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 967 | /// others... [FIXME] |
| 968 | /// |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 969 | Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts, |
| 970 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 971 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 972 | ParsedAttributesWithRange &attrs) { |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 973 | ParenBraceBracketBalancer BalancerRAIIObj(*this); |
Fariborz Jahanian | e8cff36 | 2011-08-30 17:10:52 +0000 | [diff] [blame] | 974 | // Must temporarily exit the objective-c container scope for |
| 975 | // parsing c none objective-c decls. |
| 976 | ObjCDeclContextSwitch ObjCDC(*this); |
Argyrios Kyrtzidis | 36d3680 | 2010-06-17 10:52:18 +0000 | [diff] [blame] | 977 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 978 | Decl *SingleDecl = 0; |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 979 | Decl *OwnedType = 0; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 980 | switch (Tok.getKind()) { |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 981 | case tok::kw_template: |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 982 | case tok::kw_export: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 983 | ProhibitAttributes(attrs); |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 984 | SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 985 | break; |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 986 | case tok::kw_inline: |
Sebastian Redl | 88e64ca | 2010-08-31 00:36:45 +0000 | [diff] [blame] | 987 | // 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] | 988 | if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 989 | ProhibitAttributes(attrs); |
Sebastian Redl | d078e64 | 2010-08-27 23:12:46 +0000 | [diff] [blame] | 990 | SourceLocation InlineLoc = ConsumeToken(); |
| 991 | SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc); |
| 992 | break; |
| 993 | } |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 994 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 995 | true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 996 | case tok::kw_namespace: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 997 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 998 | SingleDecl = ParseNamespace(Context, DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 999 | break; |
Douglas Gregor | f780abc | 2008-12-30 03:27:21 +0000 | [diff] [blame] | 1000 | case tok::kw_using: |
John McCall | 78b8105 | 2010-11-10 02:40:36 +0000 | [diff] [blame] | 1001 | SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(), |
Richard Smith | c89edf5 | 2011-07-01 19:46:12 +0000 | [diff] [blame] | 1002 | DeclEnd, attrs, &OwnedType); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1003 | break; |
Anders Carlsson | 511d7ab | 2009-03-11 16:27:10 +0000 | [diff] [blame] | 1004 | case tok::kw_static_assert: |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 1005 | case tok::kw__Static_assert: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1006 | ProhibitAttributes(attrs); |
Chris Lattner | 97144fc | 2009-04-02 04:16:50 +0000 | [diff] [blame] | 1007 | SingleDecl = ParseStaticAssertDeclaration(DeclEnd); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1008 | break; |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1009 | default: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1010 | return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1011 | } |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1012 | |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1013 | // 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] | 1014 | // single decl, convert it now. Alias declarations can also declare a type; |
| 1015 | // include that too if it is present. |
| 1016 | return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType); |
Chris Lattner | 8f08cb7 | 2007-08-25 06:57:03 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | /// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl] |
| 1020 | /// declaration-specifiers init-declarator-list[opt] ';' |
| 1021 | ///[C90/C++]init-declarator-list ';' [TODO] |
| 1022 | /// [OMP] threadprivate-directive [TODO] |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1023 | /// |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1024 | /// for-range-declaration: [C++0x 6.5p1: stmt.ranged] |
| 1025 | /// attribute-specifier-seq[opt] type-specifier-seq declarator |
| 1026 | /// |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1027 | /// 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] | 1028 | /// declaration. If it is true, it checks for and eats it. |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1029 | /// |
| 1030 | /// If FRI is non-null, we might be parsing a for-range-declaration instead |
| 1031 | /// of a simple-declaration. If we find that we are, we also parse the |
| 1032 | /// for-range-initializer, and place it here. |
Fariborz Jahanian | c5be7b0 | 2010-09-28 20:42:35 +0000 | [diff] [blame] | 1033 | Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts, |
| 1034 | unsigned Context, |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 1035 | SourceLocation &DeclEnd, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1036 | ParsedAttributes &attrs, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1037 | bool RequireSemi, |
| 1038 | ForRangeInit *FRI) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1039 | // Parse the common declaration-specifiers piece. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1040 | ParsingDeclSpec DS(*this); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1041 | DS.takeAttributesFrom(attrs); |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1042 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1043 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1044 | getDeclSpecContextFromDeclaratorContext(Context)); |
Abramo Bagnara | 06284c1 | 2012-01-07 10:52:36 +0000 | [diff] [blame] | 1045 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1046 | // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };" |
| 1047 | // declaration-specifiers init-declarator-list[opt] ';' |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 1048 | if (Tok.is(tok::semi)) { |
Argyrios Kyrtzidis | 5641b0d | 2012-05-16 23:49:15 +0000 | [diff] [blame] | 1049 | DeclEnd = Tok.getLocation(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 1050 | if (RequireSemi) ConsumeToken(); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1051 | Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1052 | DS); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1053 | DS.complete(TheDecl); |
Chris Lattner | 682bf92 | 2009-03-29 16:50:03 +0000 | [diff] [blame] | 1054 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1055 | } |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1056 | |
| 1057 | return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1058 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1059 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1060 | /// Returns true if this might be the start of a declarator, or a common typo |
| 1061 | /// for a declarator. |
| 1062 | bool Parser::MightBeDeclarator(unsigned Context) { |
| 1063 | switch (Tok.getKind()) { |
| 1064 | case tok::annot_cxxscope: |
| 1065 | case tok::annot_template_id: |
| 1066 | case tok::caret: |
| 1067 | case tok::code_completion: |
| 1068 | case tok::coloncolon: |
| 1069 | case tok::ellipsis: |
| 1070 | case tok::kw___attribute: |
| 1071 | case tok::kw_operator: |
| 1072 | case tok::l_paren: |
| 1073 | case tok::star: |
| 1074 | return true; |
| 1075 | |
| 1076 | case tok::amp: |
| 1077 | case tok::ampamp: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1078 | return getLangOpts().CPlusPlus; |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1079 | |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1080 | case tok::l_square: // Might be an attribute on an unnamed bit-field. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1081 | return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x && |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1082 | NextToken().is(tok::l_square); |
| 1083 | |
| 1084 | case tok::colon: // Might be a typo for '::' or an unnamed bit-field. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1085 | return Context == Declarator::MemberContext || getLangOpts().CPlusPlus; |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1086 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1087 | case tok::identifier: |
| 1088 | switch (NextToken().getKind()) { |
| 1089 | case tok::code_completion: |
| 1090 | case tok::coloncolon: |
| 1091 | case tok::comma: |
| 1092 | case tok::equal: |
| 1093 | case tok::equalequal: // Might be a typo for '='. |
| 1094 | case tok::kw_alignas: |
| 1095 | case tok::kw_asm: |
| 1096 | case tok::kw___attribute: |
| 1097 | case tok::l_brace: |
| 1098 | case tok::l_paren: |
| 1099 | case tok::l_square: |
| 1100 | case tok::less: |
| 1101 | case tok::r_brace: |
| 1102 | case tok::r_paren: |
| 1103 | case tok::r_square: |
| 1104 | case tok::semi: |
| 1105 | return true; |
| 1106 | |
| 1107 | case tok::colon: |
| 1108 | // At namespace scope, 'identifier:' is probably a typo for 'identifier::' |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1109 | // and in block scope it's probably a label. Inside a class definition, |
| 1110 | // this is a bit-field. |
| 1111 | return Context == Declarator::MemberContext || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1112 | (getLangOpts().CPlusPlus && Context == Declarator::FileContext); |
Richard Smith | 1c94c16 | 2012-01-09 22:31:44 +0000 | [diff] [blame] | 1113 | |
| 1114 | case tok::identifier: // Possible virt-specifier. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1115 | return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken()); |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1116 | |
| 1117 | default: |
| 1118 | return false; |
| 1119 | } |
| 1120 | |
| 1121 | default: |
| 1122 | return false; |
| 1123 | } |
| 1124 | } |
| 1125 | |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1126 | /// Skip until we reach something which seems like a sensible place to pick |
| 1127 | /// up parsing after a malformed declaration. This will sometimes stop sooner |
| 1128 | /// than SkipUntil(tok::r_brace) would, but will never stop later. |
| 1129 | void Parser::SkipMalformedDecl() { |
| 1130 | while (true) { |
| 1131 | switch (Tok.getKind()) { |
| 1132 | case tok::l_brace: |
| 1133 | // Skip until matching }, then stop. We've probably skipped over |
| 1134 | // a malformed class or function definition or similar. |
| 1135 | ConsumeBrace(); |
| 1136 | SkipUntil(tok::r_brace, /*StopAtSemi*/false); |
| 1137 | if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) { |
| 1138 | // This declaration isn't over yet. Keep skipping. |
| 1139 | continue; |
| 1140 | } |
| 1141 | if (Tok.is(tok::semi)) |
| 1142 | ConsumeToken(); |
| 1143 | return; |
| 1144 | |
| 1145 | case tok::l_square: |
| 1146 | ConsumeBracket(); |
| 1147 | SkipUntil(tok::r_square, /*StopAtSemi*/false); |
| 1148 | continue; |
| 1149 | |
| 1150 | case tok::l_paren: |
| 1151 | ConsumeParen(); |
| 1152 | SkipUntil(tok::r_paren, /*StopAtSemi*/false); |
| 1153 | continue; |
| 1154 | |
| 1155 | case tok::r_brace: |
| 1156 | return; |
| 1157 | |
| 1158 | case tok::semi: |
| 1159 | ConsumeToken(); |
| 1160 | return; |
| 1161 | |
| 1162 | case tok::kw_inline: |
| 1163 | // 'inline namespace' at the start of a line is almost certainly |
| 1164 | // a good place to pick back up parsing. |
| 1165 | if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace)) |
| 1166 | return; |
| 1167 | break; |
| 1168 | |
| 1169 | case tok::kw_namespace: |
| 1170 | // 'namespace' at the start of a line is almost certainly a good |
| 1171 | // place to pick back up parsing. |
| 1172 | if (Tok.isAtStartOfLine()) |
| 1173 | return; |
| 1174 | break; |
| 1175 | |
| 1176 | case tok::eof: |
| 1177 | return; |
| 1178 | |
| 1179 | default: |
| 1180 | break; |
| 1181 | } |
| 1182 | |
| 1183 | ConsumeAnyToken(); |
| 1184 | } |
| 1185 | } |
| 1186 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1187 | /// ParseDeclGroup - Having concluded that this is either a function |
| 1188 | /// definition or a group of object declarations, actually parse the |
| 1189 | /// result. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1190 | Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS, |
| 1191 | unsigned Context, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1192 | bool AllowFunctionDefinitions, |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1193 | SourceLocation *DeclEnd, |
| 1194 | ForRangeInit *FRI) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1195 | // Parse the first declarator. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1196 | ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context)); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1197 | ParseDeclarator(D); |
Chris Lattner | cd14775 | 2009-03-29 17:27:48 +0000 | [diff] [blame] | 1198 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1199 | // Bail out if the first declarator didn't seem well-formed. |
| 1200 | if (!D.hasName() && !D.mayOmitIdentifier()) { |
Richard Smith | 994d73f | 2012-04-11 20:59:20 +0000 | [diff] [blame] | 1201 | SkipMalformedDecl(); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1202 | return DeclGroupPtrTy(); |
Chris Lattner | 23c4b18 | 2009-03-29 17:18:04 +0000 | [diff] [blame] | 1203 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1204 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1205 | // Save late-parsed attributes for now; they need to be parsed in the |
| 1206 | // appropriate function scope after the function Decl has been constructed. |
| 1207 | LateParsedAttrList LateParsedAttrs; |
| 1208 | if (D.isFunctionDeclarator()) |
| 1209 | MaybeParseGNUAttributes(D, &LateParsedAttrs); |
| 1210 | |
Chris Lattner | c82daef | 2010-07-11 22:24:20 +0000 | [diff] [blame] | 1211 | // Check to see if we have a function *definition* which must have a body. |
| 1212 | if (AllowFunctionDefinitions && D.isFunctionDeclarator() && |
| 1213 | // Look at the next token to make sure that this isn't a function |
| 1214 | // declaration. We have to check this because __attribute__ might be the |
| 1215 | // start of a function definition in GCC-extended K&R C. |
| 1216 | !isDeclarationAfterDeclarator()) { |
Richard Smith | 58196dc | 2011-11-30 23:45:35 +0000 | [diff] [blame] | 1217 | |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1218 | if (isStartOfFunctionDefinition(D)) { |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1219 | if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 1220 | Diag(Tok, diag::err_function_declared_typedef); |
| 1221 | |
| 1222 | // Recover by treating the 'typedef' as spurious. |
| 1223 | DS.ClearStorageClassSpecs(); |
| 1224 | } |
| 1225 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1226 | Decl *TheDecl = |
| 1227 | ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1228 | return Actions.ConvertDeclToDeclGroup(TheDecl); |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1229 | } |
| 1230 | |
| 1231 | if (isDeclarationSpecifier()) { |
| 1232 | // If there is an invalid declaration specifier right after the function |
| 1233 | // prototype, then we must be in a missing semicolon case where this isn't |
| 1234 | // actually a body. Just fall through into the code that handles it as a |
| 1235 | // prototype, and let the top-level code handle the erroneous declspec |
| 1236 | // where it would otherwise expect a comma or semicolon. |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1237 | } else { |
| 1238 | Diag(Tok, diag::err_expected_fn_body); |
| 1239 | SkipUntil(tok::semi); |
| 1240 | return DeclGroupPtrTy(); |
| 1241 | } |
| 1242 | } |
| 1243 | |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1244 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1245 | return DeclGroupPtrTy(); |
| 1246 | |
| 1247 | // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we |
| 1248 | // must parse and analyze the for-range-initializer before the declaration is |
| 1249 | // analyzed. |
| 1250 | if (FRI && Tok.is(tok::colon)) { |
| 1251 | FRI->ColonLoc = ConsumeToken(); |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1252 | if (Tok.is(tok::l_brace)) |
| 1253 | FRI->RangeExpr = ParseBraceInitializer(); |
| 1254 | else |
| 1255 | FRI->RangeExpr = ParseExpression(); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1256 | Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
| 1257 | Actions.ActOnCXXForRangeDecl(ThisDecl); |
| 1258 | Actions.FinalizeDeclaration(ThisDecl); |
John McCall | 6895a64 | 2012-01-27 01:29:43 +0000 | [diff] [blame] | 1259 | D.complete(ThisDecl); |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1260 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1); |
| 1261 | } |
| 1262 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1263 | SmallVector<Decl *, 8> DeclsInGroup; |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1264 | Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D); |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1265 | if (LateParsedAttrs.size() > 0) |
| 1266 | ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 1267 | D.complete(FirstDecl); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1268 | if (FirstDecl) |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1269 | DeclsInGroup.push_back(FirstDecl); |
| 1270 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1271 | bool ExpectSemi = Context != Declarator::ForContext; |
| 1272 | |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1273 | // If we don't have a comma, it is either the end of the list (a ';') or an |
| 1274 | // error, bail out. |
| 1275 | while (Tok.is(tok::comma)) { |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1276 | SourceLocation CommaLoc = ConsumeToken(); |
| 1277 | |
| 1278 | if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) { |
| 1279 | // This comma was followed by a line-break and something which can't be |
| 1280 | // the start of a declarator. The comma was probably a typo for a |
| 1281 | // semicolon. |
| 1282 | Diag(CommaLoc, diag::err_expected_semi_declaration) |
| 1283 | << FixItHint::CreateReplacement(CommaLoc, ";"); |
| 1284 | ExpectSemi = false; |
| 1285 | break; |
| 1286 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1287 | |
| 1288 | // Parse the next declarator. |
| 1289 | D.clear(); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 1290 | D.setCommaLoc(CommaLoc); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1291 | |
| 1292 | // Accept attributes in an init-declarator. In the first declarator in a |
| 1293 | // declaration, these would be part of the declspec. In subsequent |
| 1294 | // declarators, they become part of the declarator itself, so that they |
| 1295 | // don't apply to declarators after *this* one. Examples: |
| 1296 | // short __attribute__((common)) var; -> declspec |
| 1297 | // short var __attribute__((common)); -> declarator |
| 1298 | // short x, __attribute__((common)) var; -> declarator |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 1299 | MaybeParseGNUAttributes(D); |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1300 | |
| 1301 | ParseDeclarator(D); |
Fariborz Jahanian | 9baf39d | 2012-01-13 00:14:12 +0000 | [diff] [blame] | 1302 | if (!D.isInvalidType()) { |
| 1303 | Decl *ThisDecl = ParseDeclarationAfterDeclarator(D); |
| 1304 | D.complete(ThisDecl); |
| 1305 | if (ThisDecl) |
| 1306 | DeclsInGroup.push_back(ThisDecl); |
| 1307 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | if (DeclEnd) |
| 1311 | *DeclEnd = Tok.getLocation(); |
| 1312 | |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 1313 | if (ExpectSemi && |
Chris Lattner | 8bb21d3 | 2012-04-28 16:12:17 +0000 | [diff] [blame] | 1314 | ExpectAndConsumeSemi(Context == Declarator::FileContext |
| 1315 | ? diag::err_invalid_token_after_toplevel_declarator |
| 1316 | : diag::err_expected_semi_declaration)) { |
Chris Lattner | 004659a | 2010-07-11 22:42:07 +0000 | [diff] [blame] | 1317 | // Okay, there was no semicolon and one was expected. If we see a |
| 1318 | // declaration specifier, just assume it was missing and continue parsing. |
| 1319 | // Otherwise things are very confused and we skip to recover. |
| 1320 | if (!isDeclarationSpecifier()) { |
| 1321 | SkipUntil(tok::r_brace, true, true); |
| 1322 | if (Tok.is(tok::semi)) |
| 1323 | ConsumeToken(); |
| 1324 | } |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1327 | return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, |
John McCall | d8ac057 | 2009-11-03 19:26:08 +0000 | [diff] [blame] | 1328 | DeclsInGroup.data(), |
| 1329 | DeclsInGroup.size()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1330 | } |
| 1331 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1332 | /// Parse an optional simple-asm-expr and attributes, and attach them to a |
| 1333 | /// declarator. Returns true on an error. |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1334 | bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) { |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1335 | // If a simple-asm-expr is present, parse it. |
| 1336 | if (Tok.is(tok::kw_asm)) { |
| 1337 | SourceLocation Loc; |
| 1338 | ExprResult AsmLabel(ParseSimpleAsm(&Loc)); |
| 1339 | if (AsmLabel.isInvalid()) { |
| 1340 | SkipUntil(tok::semi, true, true); |
| 1341 | return true; |
| 1342 | } |
| 1343 | |
| 1344 | D.setAsmLabel(AsmLabel.release()); |
| 1345 | D.SetRangeEnd(Loc); |
| 1346 | } |
| 1347 | |
| 1348 | MaybeParseGNUAttributes(D); |
| 1349 | return false; |
| 1350 | } |
| 1351 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1352 | /// \brief Parse 'declaration' after parsing 'declaration-specifiers |
| 1353 | /// declarator'. This method parses the remainder of the declaration |
| 1354 | /// (including any attributes or initializer, among other things) and |
| 1355 | /// finalizes the declaration. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1356 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1357 | /// init-declarator: [C99 6.7] |
| 1358 | /// declarator |
| 1359 | /// declarator '=' initializer |
| 1360 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] |
| 1361 | /// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 1362 | /// [C++] declarator initializer[opt] |
| 1363 | /// |
| 1364 | /// [C++] initializer: |
| 1365 | /// [C++] '=' initializer-clause |
| 1366 | /// [C++] '(' expression-list ')' |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1367 | /// [C++0x] '=' 'default' [TODO] |
| 1368 | /// [C++0x] '=' 'delete' |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1369 | /// [C++0x] braced-init-list |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 1370 | /// |
| 1371 | /// According to the standard grammar, =default and =delete are function |
| 1372 | /// definitions, but that definitely doesn't fit with the parser here. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1373 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1374 | Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1375 | const ParsedTemplateInfo &TemplateInfo) { |
DeLesley Hutchins | c24a233 | 2012-02-16 16:50:43 +0000 | [diff] [blame] | 1376 | if (ParseAsmAttributesAfterDeclarator(D)) |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1377 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1378 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1379 | return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo); |
| 1380 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1381 | |
Richard Smith | ad762fc | 2011-04-14 22:09:26 +0000 | [diff] [blame] | 1382 | Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D, |
| 1383 | const ParsedTemplateInfo &TemplateInfo) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1384 | // Inform the current actions module that we just parsed this declarator. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1385 | Decl *ThisDecl = 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1386 | switch (TemplateInfo.Kind) { |
| 1387 | case ParsedTemplateInfo::NonTemplate: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1388 | ThisDecl = Actions.ActOnDeclarator(getCurScope(), D); |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1389 | break; |
| 1390 | |
| 1391 | case ParsedTemplateInfo::Template: |
| 1392 | case ParsedTemplateInfo::ExplicitSpecialization: |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1393 | ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(), |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1394 | MultiTemplateParamsArg(Actions, |
Douglas Gregor | e542c86 | 2009-06-23 23:11:28 +0000 | [diff] [blame] | 1395 | TemplateInfo.TemplateParams->data(), |
| 1396 | TemplateInfo.TemplateParams->size()), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1397 | D); |
| 1398 | break; |
| 1399 | |
| 1400 | case ParsedTemplateInfo::ExplicitInstantiation: { |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1401 | DeclResult ThisRes |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1402 | = Actions.ActOnExplicitInstantiation(getCurScope(), |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1403 | TemplateInfo.ExternLoc, |
| 1404 | TemplateInfo.TemplateLoc, |
| 1405 | D); |
| 1406 | if (ThisRes.isInvalid()) { |
| 1407 | SkipUntil(tok::semi, true, true); |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 1408 | return 0; |
Douglas Gregor | d5a423b | 2009-09-25 18:43:00 +0000 | [diff] [blame] | 1409 | } |
| 1410 | |
| 1411 | ThisDecl = ThisRes.get(); |
| 1412 | break; |
| 1413 | } |
| 1414 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1415 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1416 | bool TypeContainsAuto = |
| 1417 | D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto; |
| 1418 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1419 | // Parse declarator '=' initializer. |
Richard Trieu | d6c7c67 | 2012-01-18 22:54:52 +0000 | [diff] [blame] | 1420 | // If a '==' or '+=' is found, suggest a fixit to '='. |
Richard Trieu | fcaf27e | 2012-01-19 22:01:51 +0000 | [diff] [blame] | 1421 | if (isTokenEqualOrEqualTypo()) { |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1422 | ConsumeToken(); |
Anders Carlsson | 37bf9d2 | 2010-09-24 21:25:25 +0000 | [diff] [blame] | 1423 | if (Tok.is(tok::kw_delete)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1424 | if (D.isFunctionDeclarator()) |
| 1425 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1426 | << 1 /* delete */; |
| 1427 | else |
| 1428 | Diag(ConsumeToken(), diag::err_deleted_non_function); |
Sean Hunt | fe2695e | 2011-05-06 01:42:00 +0000 | [diff] [blame] | 1429 | } else if (Tok.is(tok::kw_default)) { |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1430 | if (D.isFunctionDeclarator()) |
Sebastian Redl | ecfcd56 | 2012-02-11 23:51:21 +0000 | [diff] [blame] | 1431 | Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration) |
| 1432 | << 0 /* default */; |
Sean Hunt | e4246a6 | 2011-05-12 06:15:49 +0000 | [diff] [blame] | 1433 | else |
| 1434 | Diag(ConsumeToken(), diag::err_default_special_members); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1435 | } else { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1436 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1437 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1438 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1439 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1440 | |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1441 | if (Tok.is(tok::code_completion)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1442 | Actions.CodeCompleteInitializer(getCurScope(), ThisDecl); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1443 | cutOffParsing(); |
| 1444 | return 0; |
Douglas Gregor | 5ac3bdb | 2010-05-30 01:49:25 +0000 | [diff] [blame] | 1445 | } |
| 1446 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1447 | ExprResult Init(ParseInitializer()); |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1448 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1449 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1450 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
John McCall | 731ad84 | 2009-12-19 09:28:58 +0000 | [diff] [blame] | 1451 | ExitScope(); |
| 1452 | } |
Argyrios Kyrtzidis | 0ffd9ff | 2009-06-17 22:50:06 +0000 | [diff] [blame] | 1453 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1454 | if (Init.isInvalid()) { |
Douglas Gregor | 0022554 | 2010-03-01 18:27:54 +0000 | [diff] [blame] | 1455 | SkipUntil(tok::comma, true, true); |
| 1456 | Actions.ActOnInitializerError(ThisDecl); |
| 1457 | } else |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1458 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1459 | /*DirectInit=*/false, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1460 | } |
| 1461 | } else if (Tok.is(tok::l_paren)) { |
| 1462 | // Parse C++ direct initializer: '(' expression-list ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1463 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1464 | T.consumeOpen(); |
| 1465 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1466 | ExprVector Exprs(Actions); |
| 1467 | CommaLocsTy CommaLocs; |
| 1468 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1469 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1470 | EnterScope(0); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1471 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1474 | if (ParseExpressionList(Exprs, CommaLocs)) { |
| 1475 | SkipUntil(tok::r_paren); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1476 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1477 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1478 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1479 | ExitScope(); |
| 1480 | } |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1481 | } else { |
| 1482 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1483 | T.consumeClose(); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1484 | |
| 1485 | assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() && |
| 1486 | "Unexpected number of commas!"); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1487 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1488 | if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1489 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
Douglas Gregor | b4debae | 2009-12-22 17:47:17 +0000 | [diff] [blame] | 1490 | ExitScope(); |
| 1491 | } |
| 1492 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 1493 | ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(), |
| 1494 | T.getCloseLocation(), |
| 1495 | move_arg(Exprs)); |
| 1496 | Actions.AddInitializerToDecl(ThisDecl, Initializer.take(), |
| 1497 | /*DirectInit=*/true, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1498 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1499 | } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) { |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1500 | // Parse C++0x braced-init-list. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 1501 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
| 1502 | |
Sebastian Redl | dbef1bb | 2011-06-05 12:23:16 +0000 | [diff] [blame] | 1503 | if (D.getCXXScopeSpec().isSet()) { |
| 1504 | EnterScope(0); |
| 1505 | Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl); |
| 1506 | } |
| 1507 | |
| 1508 | ExprResult Init(ParseBraceInitializer()); |
| 1509 | |
| 1510 | if (D.getCXXScopeSpec().isSet()) { |
| 1511 | Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl); |
| 1512 | ExitScope(); |
| 1513 | } |
| 1514 | |
| 1515 | if (Init.isInvalid()) { |
| 1516 | Actions.ActOnInitializerError(ThisDecl); |
| 1517 | } else |
| 1518 | Actions.AddInitializerToDecl(ThisDecl, Init.take(), |
| 1519 | /*DirectInit=*/true, TypeContainsAuto); |
| 1520 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1521 | } else { |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 1522 | Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto); |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1523 | } |
| 1524 | |
Richard Smith | 483b9f3 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 1525 | Actions.FinalizeDeclaration(ThisDecl); |
| 1526 | |
Douglas Gregor | 1426e53 | 2009-05-12 21:31:51 +0000 | [diff] [blame] | 1527 | return ThisDecl; |
| 1528 | } |
| 1529 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1530 | /// ParseSpecifierQualifierList |
| 1531 | /// specifier-qualifier-list: |
| 1532 | /// type-specifier specifier-qualifier-list[opt] |
| 1533 | /// type-qualifier specifier-qualifier-list[opt] |
| 1534 | /// [GNU] attributes specifier-qualifier-list[opt] |
| 1535 | /// |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1536 | void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS, |
| 1537 | DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1538 | /// specifier-qualifier-list is a subset of declaration-specifiers. Just |
| 1539 | /// parse declaration-specifiers and complain about extra stuff. |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1540 | /// TODO: diagnose attribute-specifiers and alignment-specifiers. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1541 | ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1542 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1543 | // Validate declspec for type-name. |
| 1544 | unsigned Specs = DS.getParsedSpecifiers(); |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 1545 | if ((DSC == DSC_type_specifier || DSC == DSC_trailing) && |
| 1546 | !DS.hasTypeSpecifier()) { |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 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 | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 1638 | // error, do lookahead to try to do better recovery. This never applies |
| 1639 | // within a type specifier. Outside of C++, we allow this even if the |
| 1640 | // language doesn't "officially" support implicit int -- we support |
| 1641 | // implicit int as an extension in C99 and C11. Allegedly, MS also |
| 1642 | // supports implicit int in C++ mode. |
Richard Smith | a971d24 | 2012-05-09 20:55:26 +0000 | [diff] [blame] | 1643 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 1644 | (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) && |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1645 | isValidAfterIdentifierInDeclarator(NextToken())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1646 | // If this token is valid for implicit int, e.g. "static x = 4", then |
| 1647 | // we just avoid eating the identifier, so it will be parsed as the |
| 1648 | // identifier in the declarator. |
| 1649 | return false; |
| 1650 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1651 | |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 1652 | if (getLangOpts().CPlusPlus && |
| 1653 | DS.getStorageClassSpec() == DeclSpec::SCS_auto) { |
| 1654 | // Don't require a type specifier if we have the 'auto' storage class |
| 1655 | // specifier in C++98 -- we'll promote it to a type specifier. |
| 1656 | return false; |
| 1657 | } |
| 1658 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1659 | // Otherwise, if we don't consume this token, we are going to emit an |
| 1660 | // error anyway. Try to recover from various common problems. Check |
| 1661 | // to see if this was a reference to a tag name without a tag specified. |
| 1662 | // 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] | 1663 | // |
| 1664 | // C++ doesn't need this, and isTagName doesn't take SS. |
| 1665 | if (SS == 0) { |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1666 | const char *TagName = 0, *FixitTagName = 0; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1667 | tok::TokenKind TagKind = tok::unknown; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1668 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1669 | switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) { |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1670 | default: break; |
Argyrios Kyrtzidis | b8a9d3b | 2011-04-21 17:29:47 +0000 | [diff] [blame] | 1671 | case DeclSpec::TST_enum: |
| 1672 | TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break; |
| 1673 | case DeclSpec::TST_union: |
| 1674 | TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break; |
| 1675 | case DeclSpec::TST_struct: |
| 1676 | TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break; |
| 1677 | case DeclSpec::TST_class: |
| 1678 | TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break; |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1679 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1680 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1681 | if (TagName) { |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 1682 | IdentifierInfo *TokenName = Tok.getIdentifierInfo(); |
| 1683 | LookupResult R(Actions, TokenName, SourceLocation(), |
| 1684 | Sema::LookupOrdinaryName); |
| 1685 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1686 | Diag(Loc, diag::err_use_of_tag_name_without_tag) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 1687 | << TokenName << TagName << getLangOpts().CPlusPlus |
| 1688 | << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName); |
| 1689 | |
| 1690 | if (Actions.LookupParsedName(R, getCurScope(), SS)) { |
| 1691 | for (LookupResult::iterator I = R.begin(), IEnd = R.end(); |
| 1692 | I != IEnd; ++I) |
Kaelyn Uhrain | 392b3f5 | 2012-04-27 18:26:49 +0000 | [diff] [blame] | 1693 | Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type) |
Kaelyn Uhrain | aec2ac6 | 2012-04-26 23:36:17 +0000 | [diff] [blame] | 1694 | << TokenName << TagName; |
| 1695 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1696 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1697 | // Parse this as a tag as if the missing tag were present. |
| 1698 | if (TagKind == tok::kw_enum) |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1699 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1700 | else |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1701 | ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS, |
| 1702 | /*EnteringContext*/ false, DSC_normal); |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 1703 | return true; |
| 1704 | } |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1705 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1706 | |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 1707 | // Determine whether this identifier could plausibly be the name of something |
Richard Smith | 7514db2 | 2012-05-15 21:42:17 +0000 | [diff] [blame] | 1708 | // being declared (with a missing type). |
Richard Smith | 8f0a7e7 | 2012-05-15 21:29:55 +0000 | [diff] [blame] | 1709 | if (DSC != DSC_type_specifier && DSC != DSC_trailing && |
| 1710 | (!SS || DSC == DSC_top_level || DSC == DSC_class)) { |
Richard Smith | 827adaf | 2012-05-15 21:01:51 +0000 | [diff] [blame] | 1711 | // Look ahead to the next token to try to figure out what this declaration |
| 1712 | // was supposed to be. |
| 1713 | switch (NextToken().getKind()) { |
| 1714 | case tok::comma: |
| 1715 | case tok::equal: |
| 1716 | case tok::kw_asm: |
| 1717 | case tok::l_brace: |
| 1718 | case tok::l_square: |
| 1719 | case tok::semi: |
| 1720 | // This looks like a variable declaration. The type is probably missing. |
| 1721 | // We're done parsing decl-specifiers. |
| 1722 | return false; |
| 1723 | |
| 1724 | case tok::l_paren: { |
| 1725 | // static x(4); // 'x' is not a type |
| 1726 | // x(int n); // 'x' is not a type |
| 1727 | // x (*p)[]; // 'x' is a type |
| 1728 | // |
| 1729 | // Since we're in an error case (or the rare 'implicit int in C++' MS |
| 1730 | // extension), we can afford to perform a tentative parse to determine |
| 1731 | // which case we're in. |
| 1732 | TentativeParsingAction PA(*this); |
| 1733 | ConsumeToken(); |
| 1734 | TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false); |
| 1735 | PA.Revert(); |
| 1736 | if (TPR == TPResult::False()) |
| 1737 | return false; |
| 1738 | // The identifier is followed by a parenthesized declarator. |
| 1739 | // It's supposed to be a type. |
| 1740 | break; |
| 1741 | } |
| 1742 | |
| 1743 | default: |
| 1744 | // This is probably supposed to be a type. This includes cases like: |
| 1745 | // int f(itn); |
| 1746 | // struct S { unsinged : 4; }; |
| 1747 | break; |
| 1748 | } |
| 1749 | } |
| 1750 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1751 | // This is almost certainly an invalid type name. Let the action emit a |
| 1752 | // diagnostic and attempt to recover. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1753 | ParsedType T; |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1754 | if (Actions.DiagnoseUnknownTypeName(*Tok.getIdentifierInfo(), Loc, |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 1755 | getCurScope(), SS, T)) { |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1756 | // The action emitted a diagnostic, so we don't have to. |
| 1757 | if (T) { |
| 1758 | // The action has suggested that the type T could be used. Set that as |
| 1759 | // the type in the declaration specifiers, consume the would-be type |
| 1760 | // name token, and we're done. |
| 1761 | const char *PrevSpec; |
| 1762 | unsigned DiagID; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 1763 | DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T); |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1764 | DS.SetRangeEnd(Tok.getLocation()); |
| 1765 | ConsumeToken(); |
| 1766 | |
| 1767 | // There may be other declaration specifiers after this. |
| 1768 | return true; |
| 1769 | } |
| 1770 | |
| 1771 | // Fall through; the action had no suggestion for us. |
| 1772 | } else { |
| 1773 | // The action did not emit a diagnostic, so emit one now. |
| 1774 | SourceRange R; |
| 1775 | if (SS) R = SS->getRange(); |
| 1776 | Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R; |
| 1777 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1778 | |
Douglas Gregor | a786fdb | 2009-10-13 23:27:22 +0000 | [diff] [blame] | 1779 | // Mark this as an error. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 1780 | DS.SetTypeSpecError(); |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1781 | DS.SetRangeEnd(Tok.getLocation()); |
| 1782 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1783 | |
Chris Lattner | e40c295 | 2009-04-14 21:34:55 +0000 | [diff] [blame] | 1784 | // TODO: Could inject an invalid typedef decl in an enclosing scope to |
| 1785 | // avoid rippling error messages on subsequent uses of the same type, |
| 1786 | // could be useful if #include was forgotten. |
| 1787 | return false; |
| 1788 | } |
| 1789 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1790 | /// \brief Determine the declaration specifier context from the declarator |
| 1791 | /// context. |
| 1792 | /// |
| 1793 | /// \param Context the declarator context, which is one of the |
| 1794 | /// Declarator::TheContext enumerator values. |
| 1795 | Parser::DeclSpecContext |
| 1796 | Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) { |
| 1797 | if (Context == Declarator::MemberContext) |
| 1798 | return DSC_class; |
| 1799 | if (Context == Declarator::FileContext) |
| 1800 | return DSC_top_level; |
Richard Smith | 6d96d3a | 2012-03-15 01:02:11 +0000 | [diff] [blame] | 1801 | if (Context == Declarator::TrailingReturnContext) |
| 1802 | return DSC_trailing; |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1803 | return DSC_normal; |
| 1804 | } |
| 1805 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1806 | /// ParseAlignArgument - Parse the argument to an alignment-specifier. |
| 1807 | /// |
| 1808 | /// FIXME: Simply returns an alignof() expression if the argument is a |
| 1809 | /// type. Ideally, the type should be propagated directly into Sema. |
| 1810 | /// |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1811 | /// [C11] type-id |
| 1812 | /// [C11] constant-expression |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1813 | /// [C++0x] type-id ...[opt] |
| 1814 | /// [C++0x] assignment-expression ...[opt] |
| 1815 | ExprResult Parser::ParseAlignArgument(SourceLocation Start, |
| 1816 | SourceLocation &EllipsisLoc) { |
| 1817 | ExprResult ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1818 | if (isTypeIdInParens()) { |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1819 | SourceLocation TypeLoc = Tok.getLocation(); |
| 1820 | ParsedType Ty = ParseTypeName().get(); |
| 1821 | SourceRange TypeRange(Start, Tok.getLocation()); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1822 | ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true, |
| 1823 | Ty.getAsOpaquePtr(), TypeRange); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1824 | } else |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1825 | ER = ParseConstantExpression(); |
| 1826 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1827 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis)) |
Peter Collingbourne | fe9b2a8 | 2011-10-24 17:56:00 +0000 | [diff] [blame] | 1828 | EllipsisLoc = ConsumeToken(); |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1829 | |
| 1830 | return ER; |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the |
| 1834 | /// attribute to Attrs. |
| 1835 | /// |
| 1836 | /// alignment-specifier: |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1837 | /// [C11] '_Alignas' '(' type-id ')' |
| 1838 | /// [C11] '_Alignas' '(' constant-expression ')' |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1839 | /// [C++0x] 'alignas' '(' type-id ...[opt] ')' |
| 1840 | /// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')' |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1841 | void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs, |
| 1842 | SourceLocation *endLoc) { |
| 1843 | assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) && |
| 1844 | "Not an alignment-specifier!"); |
| 1845 | |
| 1846 | SourceLocation KWLoc = Tok.getLocation(); |
| 1847 | ConsumeToken(); |
| 1848 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1849 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 1850 | if (T.expectAndConsume(diag::err_expected_lparen)) |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1851 | return; |
| 1852 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1853 | SourceLocation EllipsisLoc; |
| 1854 | ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1855 | if (ArgExpr.isInvalid()) { |
| 1856 | SkipUntil(tok::r_paren); |
| 1857 | return; |
| 1858 | } |
| 1859 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1860 | T.consumeClose(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1861 | if (endLoc) |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1862 | *endLoc = T.getCloseLocation(); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1863 | |
Peter Collingbourne | 0b64ba9 | 2011-10-23 20:07:52 +0000 | [diff] [blame] | 1864 | // FIXME: Handle pack-expansions here. |
| 1865 | if (EllipsisLoc.isValid()) { |
| 1866 | Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported); |
| 1867 | return; |
| 1868 | } |
| 1869 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1870 | ExprVector ArgExprs(Actions); |
| 1871 | ArgExprs.push_back(ArgExpr.release()); |
| 1872 | Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 1873 | 0, T.getOpenLocation(), ArgExprs.take(), 1, false, true); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1876 | /// ParseDeclarationSpecifiers |
| 1877 | /// declaration-specifiers: [C99 6.7] |
| 1878 | /// storage-class-specifier declaration-specifiers[opt] |
| 1879 | /// type-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1880 | /// [C99] function-specifier declaration-specifiers[opt] |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1881 | /// [C11] alignment-specifier declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1882 | /// [GNU] attributes declaration-specifiers[opt] |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 1883 | /// [Clang] '__module_private__' declaration-specifiers[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1884 | /// |
| 1885 | /// storage-class-specifier: [C99 6.7.1] |
| 1886 | /// 'typedef' |
| 1887 | /// 'extern' |
| 1888 | /// 'static' |
| 1889 | /// 'auto' |
| 1890 | /// 'register' |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 1891 | /// [C++] 'mutable' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1892 | /// [GNU] '__thread' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1893 | /// function-specifier: [C99 6.7.4] |
| 1894 | /// [C99] 'inline' |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 1895 | /// [C++] 'virtual' |
| 1896 | /// [C++] 'explicit' |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 1897 | /// [OpenCL] '__kernel' |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1898 | /// 'friend': [C++ dcl.friend] |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 1899 | /// 'constexpr': [C++0x dcl.constexpr] |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 1900 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1901 | /// |
Douglas Gregor | c4b4e7b | 2008-12-24 02:52:09 +0000 | [diff] [blame] | 1902 | void Parser::ParseDeclarationSpecifiers(DeclSpec &DS, |
Douglas Gregor | 4d9a16f | 2009-05-12 23:25:50 +0000 | [diff] [blame] | 1903 | const ParsedTemplateInfo &TemplateInfo, |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 1904 | AccessSpecifier AS, |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 1905 | DeclSpecContext DSContext, |
| 1906 | LateParsedAttrList *LateAttrs) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1907 | if (DS.getSourceRange().isInvalid()) { |
| 1908 | DS.SetRangeStart(Tok.getLocation()); |
| 1909 | DS.SetRangeEnd(Tok.getLocation()); |
| 1910 | } |
| 1911 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 1912 | bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1913 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1914 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1915 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 1916 | unsigned DiagID = 0; |
| 1917 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1918 | SourceLocation Loc = Tok.getLocation(); |
Douglas Gregor | 12e083c | 2008-11-07 15:42:26 +0000 | [diff] [blame] | 1919 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1920 | switch (Tok.getKind()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1921 | default: |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 1922 | DoneWithDeclSpec: |
Peter Collingbourne | f190768 | 2011-09-29 18:03:57 +0000 | [diff] [blame] | 1923 | // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt] |
| 1924 | MaybeParseCXX0XAttributes(DS.getAttributes()); |
| 1925 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1926 | // If this is not a declaration specifier token, we're done reading decl |
| 1927 | // specifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 1928 | DS.Finish(Diags, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1929 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1930 | |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1931 | case tok::code_completion: { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1932 | Sema::ParserCompletionContext CCC = Sema::PCC_Namespace; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1933 | if (DS.hasTypeSpecifier()) { |
| 1934 | bool AllowNonIdentifiers |
| 1935 | = (getCurScope()->getFlags() & (Scope::ControlScope | |
| 1936 | Scope::BlockScope | |
| 1937 | Scope::TemplateParamScope | |
| 1938 | Scope::FunctionPrototypeScope | |
| 1939 | Scope::AtCatchScope)) == 0; |
| 1940 | bool AllowNestedNameSpecifiers |
| 1941 | = DSContext == DSC_top_level || |
| 1942 | (DSContext == DSC_class && DS.isFriendSpecified()); |
| 1943 | |
Douglas Gregor | c7b6d88 | 2010-09-16 15:14:18 +0000 | [diff] [blame] | 1944 | Actions.CodeCompleteDeclSpec(getCurScope(), DS, |
| 1945 | AllowNonIdentifiers, |
| 1946 | AllowNestedNameSpecifiers); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1947 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1948 | } |
| 1949 | |
Douglas Gregor | 68e3c2e | 2011-02-15 20:33:25 +0000 | [diff] [blame] | 1950 | if (getCurScope()->getFnParent() || getCurScope()->getBlockParent()) |
| 1951 | CCC = Sema::PCC_LocalDeclarationSpecifiers; |
| 1952 | else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1953 | CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate |
| 1954 | : Sema::PCC_Template; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1955 | else if (DSContext == DSC_class) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1956 | CCC = Sema::PCC_Class; |
Argyrios Kyrtzidis | 849639d | 2012-02-07 16:50:53 +0000 | [diff] [blame] | 1957 | else if (CurParsedObjCImpl) |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 1958 | CCC = Sema::PCC_ObjCImplementation; |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1959 | |
| 1960 | Actions.CodeCompleteOrdinaryName(getCurScope(), CCC); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 1961 | return cutOffParsing(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 1964 | case tok::coloncolon: // ::foo::bar |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1965 | // C++ scope specifier. Annotate and loop, or bail out on error. |
| 1966 | if (TryAnnotateCXXScopeToken(true)) { |
| 1967 | if (!DS.hasTypeSpecifier()) |
| 1968 | DS.SetTypeSpecError(); |
| 1969 | goto DoneWithDeclSpec; |
| 1970 | } |
John McCall | 2e0a715 | 2010-03-01 18:20:46 +0000 | [diff] [blame] | 1971 | if (Tok.is(tok::coloncolon)) // ::new or ::delete |
| 1972 | goto DoneWithDeclSpec; |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 1973 | continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1974 | |
| 1975 | case tok::annot_cxxscope: { |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 1976 | if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector()) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1977 | goto DoneWithDeclSpec; |
| 1978 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1979 | CXXScopeSpec SS; |
Douglas Gregor | c34348a | 2011-02-24 17:54:50 +0000 | [diff] [blame] | 1980 | Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(), |
| 1981 | Tok.getAnnotationRange(), |
| 1982 | SS); |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 1983 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 1984 | // We are looking for a qualified typename. |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1985 | Token Next = NextToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1986 | if (Next.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1987 | static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue()) |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 1988 | ->Kind == TNK_Type_template) { |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 1989 | // We have a qualified template-id, e.g., N::A<int> |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 1990 | |
| 1991 | // C++ [class.qual]p2: |
| 1992 | // In a lookup in which the constructor is an acceptable lookup |
| 1993 | // result and the nested-name-specifier nominates a class C: |
| 1994 | // |
| 1995 | // - if the name specified after the |
| 1996 | // nested-name-specifier, when looked up in C, is the |
| 1997 | // injected-class-name of C (Clause 9), or |
| 1998 | // |
| 1999 | // - if the name specified after the nested-name-specifier |
| 2000 | // is the same as the identifier or the |
| 2001 | // simple-template-id's template-name in the last |
| 2002 | // component of the nested-name-specifier, |
| 2003 | // |
| 2004 | // the name is instead considered to name the constructor of |
| 2005 | // class C. |
| 2006 | // |
| 2007 | // Thus, if the template-name is actually the constructor |
| 2008 | // name, then the code is ill-formed; this interpretation is |
| 2009 | // reinforced by the NAD status of core issue 635. |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2010 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next); |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2011 | if ((DSContext == DSC_top_level || |
| 2012 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
| 2013 | TemplateId->Name && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2014 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2015 | if (isConstructorDeclarator()) { |
| 2016 | // The user meant this to be an out-of-line constructor |
| 2017 | // definition, but template arguments are not allowed |
| 2018 | // there. Just allow this as a constructor; we'll |
| 2019 | // complain about it later. |
| 2020 | goto DoneWithDeclSpec; |
| 2021 | } |
| 2022 | |
| 2023 | // The user meant this to name a type, but it actually names |
| 2024 | // a constructor with some extraneous template |
| 2025 | // arguments. Complain, then parse it as a type as the user |
| 2026 | // intended. |
| 2027 | Diag(TemplateId->TemplateNameLoc, |
| 2028 | diag::err_out_of_line_template_id_names_constructor) |
| 2029 | << TemplateId->Name; |
| 2030 | } |
| 2031 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2032 | DS.getTypeSpecScope() = SS; |
| 2033 | ConsumeToken(); // The C++ scope. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2034 | assert(Tok.is(tok::annot_template_id) && |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2035 | "ParseOptionalCXXScopeSpecifier not working"); |
Douglas Gregor | 059101f | 2011-03-02 00:47:37 +0000 | [diff] [blame] | 2036 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2037 | continue; |
| 2038 | } |
| 2039 | |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2040 | if (Next.is(tok::annot_typename)) { |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2041 | DS.getTypeSpecScope() = SS; |
| 2042 | ConsumeToken(); // The C++ scope. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2043 | if (Tok.getAnnotationValue()) { |
| 2044 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | 253e80b | 2010-11-22 10:30:56 +0000 | [diff] [blame] | 2045 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, |
| 2046 | Tok.getAnnotationEndLoc(), |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2047 | PrevSpec, DiagID, T); |
| 2048 | } |
Douglas Gregor | 9d7b353 | 2009-09-28 07:26:33 +0000 | [diff] [blame] | 2049 | else |
| 2050 | DS.SetTypeSpecError(); |
| 2051 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2052 | ConsumeToken(); // The typename |
| 2053 | } |
| 2054 | |
Douglas Gregor | 9135c72 | 2009-03-25 15:40:00 +0000 | [diff] [blame] | 2055 | if (Next.isNot(tok::identifier)) |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2056 | goto DoneWithDeclSpec; |
| 2057 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2058 | // If we're in a context where the identifier could be a class name, |
| 2059 | // check whether this is a constructor declaration. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 2060 | if ((DSContext == DSC_top_level || |
| 2061 | (DSContext == DSC_class && DS.isFriendSpecified())) && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2062 | Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(), |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2063 | &SS)) { |
| 2064 | if (isConstructorDeclarator()) |
| 2065 | goto DoneWithDeclSpec; |
| 2066 | |
| 2067 | // As noted in C++ [class.qual]p2 (cited above), when the name |
| 2068 | // of the class is qualified in a context where it could name |
| 2069 | // a constructor, its a constructor name. However, we've |
| 2070 | // looked at the declarator, and the user probably meant this |
| 2071 | // to be a type. Complain that it isn't supposed to be treated |
| 2072 | // as a type, then proceed to parse it as a type. |
| 2073 | Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor) |
| 2074 | << Next.getIdentifierInfo(); |
| 2075 | } |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2076 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2077 | ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(), |
| 2078 | Next.getLocation(), |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2079 | getCurScope(), &SS, |
| 2080 | false, false, ParsedType(), |
Abramo Bagnara | fad03b7 | 2012-01-27 08:46:19 +0000 | [diff] [blame] | 2081 | /*IsCtorOrDtorName=*/false, |
Douglas Gregor | 9e87687 | 2011-03-01 18:12:44 +0000 | [diff] [blame] | 2082 | /*NonTrivialSourceInfo=*/true); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2083 | |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2084 | // If the referenced identifier is not a type, then this declspec is |
| 2085 | // erroneous: We already checked about that it has no type specifier, and |
| 2086 | // 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] | 2087 | // typename. |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2088 | if (TypeRep == 0) { |
| 2089 | ConsumeToken(); // Eat the scope spec so the identifier is current. |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2090 | if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2091 | goto DoneWithDeclSpec; |
Chris Lattner | f4382f5 | 2009-04-14 22:17:06 +0000 | [diff] [blame] | 2092 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2093 | |
John McCall | aa87d33 | 2009-12-12 11:40:51 +0000 | [diff] [blame] | 2094 | DS.getTypeSpecScope() = SS; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2095 | ConsumeToken(); // The C++ scope. |
| 2096 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2097 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2098 | DiagID, TypeRep); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2099 | if (isInvalid) |
| 2100 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2101 | |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2102 | DS.SetRangeEnd(Tok.getLocation()); |
| 2103 | ConsumeToken(); // The typename. |
| 2104 | |
| 2105 | continue; |
| 2106 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2107 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2108 | case tok::annot_typename: { |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2109 | if (Tok.getAnnotationValue()) { |
| 2110 | ParsedType T = getTypeAnnotation(Tok); |
Nico Weber | c43271e | 2010-11-22 12:50:03 +0000 | [diff] [blame] | 2111 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2112 | DiagID, T); |
| 2113 | } else |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2114 | DS.SetTypeSpecError(); |
Chris Lattner | 5c5db55 | 2010-04-05 18:18:31 +0000 | [diff] [blame] | 2115 | |
| 2116 | if (isInvalid) |
| 2117 | break; |
| 2118 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2119 | DS.SetRangeEnd(Tok.getAnnotationEndLoc()); |
| 2120 | ConsumeToken(); // The typename |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2121 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2122 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2123 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2124 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2125 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2126 | ParseObjCProtocolQualifiers(DS); |
| 2127 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2128 | continue; |
| 2129 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2130 | |
Douglas Gregor | bfad915 | 2011-04-28 15:48:45 +0000 | [diff] [blame] | 2131 | case tok::kw___is_signed: |
| 2132 | // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang |
| 2133 | // typically treats it as a trait. If we see __is_signed as it appears |
| 2134 | // in libstdc++, e.g., |
| 2135 | // |
| 2136 | // static const bool __is_signed; |
| 2137 | // |
| 2138 | // then treat __is_signed as an identifier rather than as a keyword. |
| 2139 | if (DS.getTypeSpecType() == TST_bool && |
| 2140 | DS.getTypeQualifiers() == DeclSpec::TQ_const && |
| 2141 | DS.getStorageClassSpec() == DeclSpec::SCS_static) { |
| 2142 | Tok.getIdentifierInfo()->RevertTokenIDToIdentifier(); |
| 2143 | Tok.setKind(tok::identifier); |
| 2144 | } |
| 2145 | |
| 2146 | // We're done with the declaration-specifiers. |
| 2147 | goto DoneWithDeclSpec; |
| 2148 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2149 | // typedef-name |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2150 | case tok::kw_decltype: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2151 | case tok::identifier: { |
Chris Lattner | 5e02c47 | 2009-01-05 00:07:25 +0000 | [diff] [blame] | 2152 | // In C++, check to see if this is a scope specifier like foo::bar::, if |
| 2153 | // so handle it as such. This is important for ctor parsing. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2154 | if (getLangOpts().CPlusPlus) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2155 | if (TryAnnotateCXXScopeToken(true)) { |
| 2156 | if (!DS.hasTypeSpecifier()) |
| 2157 | DS.SetTypeSpecError(); |
| 2158 | goto DoneWithDeclSpec; |
| 2159 | } |
| 2160 | if (!Tok.is(tok::identifier)) |
| 2161 | continue; |
| 2162 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2163 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2164 | // This identifier can only be a typedef name if we haven't already seen |
| 2165 | // a type-specifier. Without this check we misparse: |
| 2166 | // typedef int X; struct Y { short X; }; as 'short int'. |
| 2167 | if (DS.hasTypeSpecifier()) |
| 2168 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2169 | |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2170 | // Check for need to substitute AltiVec keyword tokens. |
| 2171 | if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid)) |
| 2172 | break; |
| 2173 | |
Richard Smith | f63eee7 | 2012-05-09 18:56:43 +0000 | [diff] [blame] | 2174 | // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not |
| 2175 | // allow the use of a typedef name as a type specifier. |
| 2176 | if (DS.isTypeAltiVecVector()) |
| 2177 | goto DoneWithDeclSpec; |
| 2178 | |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2179 | ParsedType TypeRep = |
| 2180 | Actions.getTypeName(*Tok.getIdentifierInfo(), |
| 2181 | Tok.getLocation(), getCurScope()); |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2182 | |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2183 | // If this is not a typedef name, don't parse it as part of the declspec, |
| 2184 | // it must be an implicit int or an error. |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2185 | if (!TypeRep) { |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2186 | if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2187 | goto DoneWithDeclSpec; |
Chris Lattner | c199ab3 | 2009-04-12 20:42:31 +0000 | [diff] [blame] | 2188 | } |
Douglas Gregor | 55f6b14 | 2009-02-09 18:46:07 +0000 | [diff] [blame] | 2189 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2190 | // If we're in a context where the identifier could be a class name, |
| 2191 | // check whether this is a constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2192 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2193 | Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2194 | isConstructorDeclarator()) |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2195 | goto DoneWithDeclSpec; |
| 2196 | |
Douglas Gregor | 1a51b4a | 2009-02-09 15:09:02 +0000 | [diff] [blame] | 2197 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2198 | DiagID, TypeRep); |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2199 | if (isInvalid) |
| 2200 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2201 | |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2202 | DS.SetRangeEnd(Tok.getLocation()); |
| 2203 | ConsumeToken(); // The identifier |
| 2204 | |
| 2205 | // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id' |
| 2206 | // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2207 | // Objective-C interface. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2208 | if (Tok.is(tok::less) && getLangOpts().ObjC1) |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2209 | ParseObjCProtocolQualifiers(DS); |
| 2210 | |
Steve Naroff | 4f9b9f1 | 2008-09-22 10:28:57 +0000 | [diff] [blame] | 2211 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2212 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2213 | continue; |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2214 | } |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2215 | |
| 2216 | // type-name |
| 2217 | case tok::annot_template_id: { |
Argyrios Kyrtzidis | 25a7676 | 2011-06-22 06:09:49 +0000 | [diff] [blame] | 2218 | TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok); |
Douglas Gregor | c45c232 | 2009-03-31 00:43:58 +0000 | [diff] [blame] | 2219 | if (TemplateId->Kind != TNK_Type_template) { |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2220 | // This template-id does not refer to a type name, so we're |
| 2221 | // done with the type-specifiers. |
| 2222 | goto DoneWithDeclSpec; |
| 2223 | } |
| 2224 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2225 | // If we're in a context where the template-id could be a |
| 2226 | // constructor name or specialization, check whether this is a |
| 2227 | // constructor declaration. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2228 | if (getLangOpts().CPlusPlus && DSContext == DSC_class && |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2229 | Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) && |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 2230 | isConstructorDeclarator()) |
| 2231 | goto DoneWithDeclSpec; |
| 2232 | |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2233 | // Turn the template-id annotation token into a type annotation |
| 2234 | // token, then try again to parse it as a type-specifier. |
Douglas Gregor | 31a19b6 | 2009-04-01 21:51:26 +0000 | [diff] [blame] | 2235 | AnnotateTemplateIdTokenAsType(); |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 2236 | continue; |
| 2237 | } |
| 2238 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2239 | // GNU attributes support. |
| 2240 | case tok::kw___attribute: |
DeLesley Hutchins | 2287c5e | 2012-03-02 22:12:59 +0000 | [diff] [blame] | 2241 | ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2242 | continue; |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2243 | |
| 2244 | // Microsoft declspec support. |
| 2245 | case tok::kw___declspec: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2246 | ParseMicrosoftDeclSpec(DS.getAttributes()); |
Steve Naroff | f59e17e | 2008-12-24 20:59:21 +0000 | [diff] [blame] | 2247 | continue; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2248 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2249 | // Microsoft single token adornments. |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2250 | case tok::kw___forceinline: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2251 | // FIXME: Add handling here! |
| 2252 | break; |
| 2253 | |
| 2254 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 2255 | case tok::kw___ptr32: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 2256 | case tok::kw___w64: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 2257 | case tok::kw___cdecl: |
| 2258 | case tok::kw___stdcall: |
| 2259 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 2260 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 2261 | case tok::kw___unaligned: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2262 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 2263 | continue; |
| 2264 | |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2265 | // Borland single token adornments. |
| 2266 | case tok::kw___pascal: |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2267 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 2268 | continue; |
| 2269 | |
Peter Collingbourne | f315fa8 | 2011-02-14 01:42:53 +0000 | [diff] [blame] | 2270 | // OpenCL single token adornments. |
| 2271 | case tok::kw___kernel: |
| 2272 | ParseOpenCLAttributes(DS.getAttributes()); |
| 2273 | continue; |
| 2274 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2275 | // storage-class-specifier |
| 2276 | case tok::kw_typedef: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2277 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc, |
| 2278 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2279 | break; |
| 2280 | case tok::kw_extern: |
| 2281 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2282 | Diag(Tok, diag::ext_thread_before) << "extern"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2283 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc, |
| 2284 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2285 | break; |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2286 | case tok::kw___private_extern__: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2287 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern, |
| 2288 | Loc, PrevSpec, DiagID); |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 2289 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2290 | case tok::kw_static: |
| 2291 | if (DS.isThreadSpecified()) |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 2292 | Diag(Tok, diag::ext_thread_before) << "static"; |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2293 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc, |
| 2294 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2295 | break; |
| 2296 | case tok::kw_auto: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2297 | if (getLangOpts().CPlusPlus0x) { |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2298 | if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) { |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2299 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2300 | PrevSpec, DiagID); |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2301 | if (!isInvalid) |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2302 | Diag(Tok, diag::ext_auto_storage_class) |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2303 | << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc()); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2304 | } else |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2305 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec, |
| 2306 | DiagID); |
Richard Smith | 8f4fb19 | 2011-09-04 19:54:14 +0000 | [diff] [blame] | 2307 | } else |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2308 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc, |
| 2309 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2310 | break; |
| 2311 | case tok::kw_register: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2312 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc, |
| 2313 | PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2314 | break; |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2315 | case tok::kw_mutable: |
Peter Collingbourne | b8b0e75 | 2011-10-06 03:01:00 +0000 | [diff] [blame] | 2316 | isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc, |
| 2317 | PrevSpec, DiagID); |
Sebastian Redl | 669d5d7 | 2008-11-14 23:42:31 +0000 | [diff] [blame] | 2318 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2319 | case tok::kw___thread: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2320 | isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2321 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2322 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2323 | // function-specifier |
| 2324 | case tok::kw_inline: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2325 | isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2326 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2327 | case tok::kw_virtual: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2328 | isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2329 | break; |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2330 | case tok::kw_explicit: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2331 | isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID); |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 2332 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2333 | |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2334 | // alignment-specifier |
| 2335 | case tok::kw__Alignas: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2336 | if (!getLangOpts().C11) |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 2337 | Diag(Tok, diag::ext_c11_alignas); |
Peter Collingbourne | 82d0b0a | 2011-09-29 18:04:28 +0000 | [diff] [blame] | 2338 | ParseAlignmentSpecifier(DS.getAttributes()); |
| 2339 | continue; |
| 2340 | |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2341 | // friend |
| 2342 | case tok::kw_friend: |
John McCall | 67d1a67 | 2009-08-06 02:15:43 +0000 | [diff] [blame] | 2343 | if (DSContext == DSC_class) |
| 2344 | isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID); |
| 2345 | else { |
| 2346 | PrevSpec = ""; // not actually used by the diagnostic |
| 2347 | DiagID = diag::err_friend_invalid_in_context; |
| 2348 | isInvalid = true; |
| 2349 | } |
Anders Carlsson | f47f7a1 | 2009-05-06 04:46:28 +0000 | [diff] [blame] | 2350 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2351 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 2352 | // Modules |
| 2353 | case tok::kw___module_private__: |
| 2354 | isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID); |
| 2355 | break; |
| 2356 | |
Sebastian Redl | 2ac6723 | 2009-11-05 15:47:02 +0000 | [diff] [blame] | 2357 | // constexpr |
| 2358 | case tok::kw_constexpr: |
| 2359 | isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID); |
| 2360 | break; |
| 2361 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2362 | // type-specifier |
| 2363 | case tok::kw_short: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2364 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec, |
| 2365 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2366 | break; |
| 2367 | case tok::kw_long: |
| 2368 | if (DS.getTypeSpecWidth() != DeclSpec::TSW_long) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2369 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec, |
| 2370 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2371 | else |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2372 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2373 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2374 | break; |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 2375 | case tok::kw___int64: |
| 2376 | isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec, |
| 2377 | DiagID); |
| 2378 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2379 | case tok::kw_signed: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2380 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec, |
| 2381 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2382 | break; |
| 2383 | case tok::kw_unsigned: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2384 | isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec, |
| 2385 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2386 | break; |
| 2387 | case tok::kw__Complex: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2388 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec, |
| 2389 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2390 | break; |
| 2391 | case tok::kw__Imaginary: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2392 | isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec, |
| 2393 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2394 | break; |
| 2395 | case tok::kw_void: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2396 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec, |
| 2397 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2398 | break; |
| 2399 | case tok::kw_char: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2400 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec, |
| 2401 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2402 | break; |
| 2403 | case tok::kw_int: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2404 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec, |
| 2405 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2406 | break; |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 2407 | case tok::kw___int128: |
| 2408 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec, |
| 2409 | DiagID); |
| 2410 | break; |
| 2411 | case tok::kw_half: |
| 2412 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec, |
| 2413 | DiagID); |
| 2414 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2415 | case tok::kw_float: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2416 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec, |
| 2417 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2418 | break; |
| 2419 | case tok::kw_double: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2420 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec, |
| 2421 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2422 | break; |
| 2423 | case tok::kw_wchar_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2424 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec, |
| 2425 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2426 | break; |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2427 | case tok::kw_char16_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2428 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec, |
| 2429 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2430 | break; |
| 2431 | case tok::kw_char32_t: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2432 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec, |
| 2433 | DiagID); |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 2434 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2435 | case tok::kw_bool: |
| 2436 | case tok::kw__Bool: |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2437 | if (Tok.is(tok::kw_bool) && |
| 2438 | DS.getTypeSpecType() != DeclSpec::TST_unspecified && |
| 2439 | DS.getStorageClassSpec() == DeclSpec::SCS_typedef) { |
| 2440 | PrevSpec = ""; // Not used by the diagnostic. |
| 2441 | DiagID = diag::err_bool_redeclaration; |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2442 | // For better error recovery. |
| 2443 | Tok.setKind(tok::identifier); |
Argyrios Kyrtzidis | 4383e18 | 2010-11-16 18:18:13 +0000 | [diff] [blame] | 2444 | isInvalid = true; |
| 2445 | } else { |
| 2446 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec, |
| 2447 | DiagID); |
| 2448 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2449 | break; |
| 2450 | case tok::kw__Decimal32: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2451 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec, |
| 2452 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2453 | break; |
| 2454 | case tok::kw__Decimal64: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2455 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec, |
| 2456 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2457 | break; |
| 2458 | case tok::kw__Decimal128: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2459 | isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec, |
| 2460 | DiagID); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2461 | break; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 2462 | case tok::kw___vector: |
| 2463 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 2464 | break; |
| 2465 | case tok::kw___pixel: |
| 2466 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 2467 | break; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 2468 | case tok::kw___unknown_anytype: |
| 2469 | isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc, |
| 2470 | PrevSpec, DiagID); |
| 2471 | break; |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2472 | |
| 2473 | // class-specifier: |
| 2474 | case tok::kw_class: |
| 2475 | case tok::kw_struct: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2476 | case tok::kw_union: { |
| 2477 | tok::TokenKind Kind = Tok.getKind(); |
| 2478 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2479 | ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS, |
| 2480 | EnteringContext, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2481 | continue; |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2482 | } |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2483 | |
| 2484 | // enum-specifier: |
| 2485 | case tok::kw_enum: |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2486 | ConsumeToken(); |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2487 | ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2488 | continue; |
| 2489 | |
| 2490 | // cv-qualifier: |
| 2491 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2492 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2493 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2494 | break; |
| 2495 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2496 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2497 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2498 | break; |
| 2499 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2500 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2501 | getLangOpts()); |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2502 | break; |
| 2503 | |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2504 | // C++ typename-specifier: |
| 2505 | case tok::kw_typename: |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2506 | if (TryAnnotateTypeOrScopeToken()) { |
| 2507 | DS.SetTypeSpecError(); |
| 2508 | goto DoneWithDeclSpec; |
| 2509 | } |
| 2510 | if (!Tok.is(tok::kw_typename)) |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 2511 | continue; |
| 2512 | break; |
| 2513 | |
Chris Lattner | 80d0c89 | 2009-01-21 19:48:37 +0000 | [diff] [blame] | 2514 | // GNU typeof support. |
| 2515 | case tok::kw_typeof: |
| 2516 | ParseTypeofSpecifier(DS); |
| 2517 | continue; |
| 2518 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 2519 | case tok::annot_decltype: |
Anders Carlsson | 6fd634f | 2009-06-24 17:47:40 +0000 | [diff] [blame] | 2520 | ParseDecltypeSpecifier(DS); |
| 2521 | continue; |
| 2522 | |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2523 | case tok::kw___underlying_type: |
| 2524 | ParseUnderlyingTypeSpecifier(DS); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 2525 | continue; |
| 2526 | |
| 2527 | case tok::kw__Atomic: |
| 2528 | ParseAtomicSpecifier(DS); |
| 2529 | continue; |
Sean Hunt | db5d44b | 2011-05-19 05:37:45 +0000 | [diff] [blame] | 2530 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2531 | // OpenCL qualifiers: |
| 2532 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2533 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 2534 | goto DoneWithDeclSpec; |
| 2535 | case tok::kw___private: |
| 2536 | case tok::kw___global: |
| 2537 | case tok::kw___local: |
| 2538 | case tok::kw___constant: |
| 2539 | case tok::kw___read_only: |
| 2540 | case tok::kw___write_only: |
| 2541 | case tok::kw___read_write: |
| 2542 | ParseOpenCLQualifiers(DS); |
| 2543 | break; |
| 2544 | |
Steve Naroff | d3ded1f | 2008-06-05 00:02:44 +0000 | [diff] [blame] | 2545 | case tok::less: |
Chris Lattner | 3bd934a | 2008-07-26 01:18:38 +0000 | [diff] [blame] | 2546 | // GCC ObjC supports types like "<SomeProtocol>" as a synonym for |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2547 | // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous, |
| 2548 | // but we support it. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2549 | if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1) |
Chris Lattner | bce6135 | 2008-07-26 00:20:22 +0000 | [diff] [blame] | 2550 | goto DoneWithDeclSpec; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2551 | |
Douglas Gregor | 46f936e | 2010-11-19 17:10:50 +0000 | [diff] [blame] | 2552 | if (!ParseObjCProtocolQualifiers(DS)) |
| 2553 | Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id) |
| 2554 | << FixItHint::CreateInsertion(Loc, "id") |
| 2555 | << SourceRange(Loc, DS.getSourceRange().getEnd()); |
Douglas Gregor | 9bd1d8d | 2010-10-21 23:17:00 +0000 | [diff] [blame] | 2556 | |
| 2557 | // Need to support trailing type qualifiers (e.g. "id<p> const"). |
| 2558 | // If a type specifier follows, it will be diagnosed elsewhere. |
| 2559 | continue; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2560 | } |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2561 | // If the specifier wasn't legal, issue a diagnostic. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2562 | if (isInvalid) { |
| 2563 | assert(PrevSpec && "Method did not return previous specifier!"); |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 2564 | assert(DiagID); |
Douglas Gregor | ae2fb14 | 2010-08-23 14:34:43 +0000 | [diff] [blame] | 2565 | |
| 2566 | if (DiagID == diag::ext_duplicate_declspec) |
| 2567 | Diag(Tok, DiagID) |
| 2568 | << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation()); |
| 2569 | else |
| 2570 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2571 | } |
Fariborz Jahanian | 12e3ece | 2011-02-22 23:17:49 +0000 | [diff] [blame] | 2572 | |
Chris Lattner | 81c018d | 2008-03-13 06:29:04 +0000 | [diff] [blame] | 2573 | DS.SetRangeEnd(Tok.getLocation()); |
Fariborz Jahanian | e106a0b | 2011-04-19 21:42:37 +0000 | [diff] [blame] | 2574 | if (DiagID != diag::err_bool_redeclaration) |
| 2575 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2576 | } |
| 2577 | } |
Douglas Gregor | adcac88 | 2008-12-01 23:54:00 +0000 | [diff] [blame] | 2578 | |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2579 | /// ParseStructDeclaration - Parse a struct declaration without the terminating |
| 2580 | /// semicolon. |
| 2581 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2582 | /// struct-declaration: |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2583 | /// specifier-qualifier-list struct-declarator-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2584 | /// [GNU] __extension__ struct-declaration |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2585 | /// [GNU] specifier-qualifier-list |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2586 | /// struct-declarator-list: |
| 2587 | /// struct-declarator |
| 2588 | /// struct-declarator-list ',' struct-declarator |
| 2589 | /// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator |
| 2590 | /// struct-declarator: |
| 2591 | /// declarator |
| 2592 | /// [GNU] declarator attributes[opt] |
| 2593 | /// declarator[opt] ':' constant-expression |
| 2594 | /// [GNU] declarator[opt] ':' constant-expression attributes[opt] |
| 2595 | /// |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2596 | void Parser:: |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2597 | ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) { |
Fariborz Jahanian | a28948f | 2011-08-22 15:54:49 +0000 | [diff] [blame] | 2598 | |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2599 | if (Tok.is(tok::kw___extension__)) { |
| 2600 | // __extension__ silences extension warnings in the subexpression. |
| 2601 | ExtensionRAIIObject O(Diags); // Use RAII to do this. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2602 | ConsumeToken(); |
Chris Lattner | c46d1a1 | 2008-10-20 06:45:43 +0000 | [diff] [blame] | 2603 | return ParseStructDeclaration(DS, Fields); |
| 2604 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2605 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2606 | // Parse the common specifier-qualifiers-list piece. |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2607 | ParseSpecifierQualifierList(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2608 | |
Douglas Gregor | 4920f1f | 2009-01-12 22:49:06 +0000 | [diff] [blame] | 2609 | // If there are no declarators, this is a free-standing declaration |
| 2610 | // specifier. Let the actions module cope with it. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2611 | if (Tok.is(tok::semi)) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2612 | Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2613 | return; |
| 2614 | } |
| 2615 | |
| 2616 | // Read struct-declarators until we find the semicolon. |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2617 | bool FirstDeclarator = true; |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2618 | SourceLocation CommaLoc; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2619 | while (1) { |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 2620 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2621 | FieldDeclarator DeclaratorInfo(DS); |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2622 | DeclaratorInfo.D.setCommaLoc(CommaLoc); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2623 | |
| 2624 | // Attributes are only allowed here on successive declarators. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2625 | if (!FirstDeclarator) |
| 2626 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2627 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2628 | /// struct-declarator: declarator |
| 2629 | /// struct-declarator: declarator[opt] ':' constant-expression |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2630 | if (Tok.isNot(tok::colon)) { |
| 2631 | // Don't parse FOO:BAR as if it were a typo for FOO::BAR. |
| 2632 | ColonProtectionRAIIObject X(*this); |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2633 | ParseDeclarator(DeclaratorInfo.D); |
Chris Lattner | a1efc8c | 2009-12-10 01:59:24 +0000 | [diff] [blame] | 2634 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2635 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2636 | if (Tok.is(tok::colon)) { |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2637 | ConsumeToken(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2638 | ExprResult Res(ParseConstantExpression()); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 2639 | if (Res.isInvalid()) |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2640 | SkipUntil(tok::semi, true, true); |
Chris Lattner | 60b1e3e | 2008-04-10 06:15:14 +0000 | [diff] [blame] | 2641 | else |
Sebastian Redl | effa8d1 | 2008-12-10 00:02:53 +0000 | [diff] [blame] | 2642 | DeclaratorInfo.BitfieldSize = Res.release(); |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2643 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2644 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2645 | // If attributes exist after the declarator, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2646 | MaybeParseGNUAttributes(DeclaratorInfo.D); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2647 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2648 | // We're done with this declarator; invoke the callback. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2649 | Decl *D = Fields.invoke(DeclaratorInfo); |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 2650 | PD.complete(D); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2651 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2652 | // If we don't have a comma, it is either the end of the list (a ';') |
| 2653 | // or an error, bail out. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2654 | if (Tok.isNot(tok::comma)) |
Chris Lattner | cd4b83c | 2007-10-29 04:42:53 +0000 | [diff] [blame] | 2655 | return; |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2656 | |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2657 | // Consume the comma. |
Richard Smith | 7984de3 | 2012-01-12 23:53:29 +0000 | [diff] [blame] | 2658 | CommaLoc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 2659 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2660 | FirstDeclarator = false; |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2661 | } |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
| 2664 | /// ParseStructUnionBody |
| 2665 | /// struct-contents: |
| 2666 | /// struct-declaration-list |
| 2667 | /// [EXT] empty |
| 2668 | /// [GNU] "struct-declaration-list" without terminatoring ';' |
| 2669 | /// struct-declaration-list: |
| 2670 | /// struct-declaration |
| 2671 | /// struct-declaration-list struct-declaration |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2672 | /// [OBC] '@' 'defs' '(' class-name ')' |
Steve Naroff | 28a7ca8 | 2007-08-20 22:28:22 +0000 | [diff] [blame] | 2673 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2674 | void Parser::ParseStructUnionBody(SourceLocation RecordLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2675 | unsigned TagType, Decl *TagDecl) { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2676 | PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc, |
| 2677 | "parsing struct/union body"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2678 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2679 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 2680 | if (T.consumeOpen()) |
| 2681 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2682 | |
Douglas Gregor | 3218c4b | 2009-01-09 22:42:13 +0000 | [diff] [blame] | 2683 | ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2684 | Actions.ActOnTagStartDefinition(getCurScope(), TagDecl); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2685 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2686 | // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in |
| 2687 | // C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2688 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) { |
Richard Smith | d7c56e1 | 2011-12-29 21:57:33 +0000 | [diff] [blame] | 2689 | Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union); |
| 2690 | Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union); |
| 2691 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2692 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2693 | SmallVector<Decl *, 32> FieldDecls; |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2694 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2695 | // 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] | 2696 | while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2697 | // Each iteration of this loop reads one struct-declaration. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2698 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2699 | // Check for extraneous top-level semicolon. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2700 | if (Tok.is(tok::semi)) { |
Richard Trieu | 4b0e6f1 | 2012-05-16 19:04:59 +0000 | [diff] [blame] | 2701 | ConsumeExtraSemi(InsideStruct, |
| 2702 | DeclSpec::getSpecifierName((DeclSpec::TST)TagType)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2703 | continue; |
| 2704 | } |
Chris Lattner | e135942 | 2008-04-10 06:46:29 +0000 | [diff] [blame] | 2705 | |
| 2706 | // Parse all the comma separated declarators. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2707 | DeclSpec DS(AttrFactory); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2708 | |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2709 | if (!Tok.is(tok::at)) { |
| 2710 | struct CFieldCallback : FieldCallback { |
| 2711 | Parser &P; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2712 | Decl *TagDecl; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2713 | SmallVectorImpl<Decl *> &FieldDecls; |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2714 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2715 | CFieldCallback(Parser &P, Decl *TagDecl, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2716 | SmallVectorImpl<Decl *> &FieldDecls) : |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2717 | P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {} |
| 2718 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2719 | virtual Decl *invoke(FieldDeclarator &FD) { |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2720 | // Install the declarator into the current TagDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 2721 | Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl, |
John McCall | 4ba3971 | 2009-11-03 21:13:47 +0000 | [diff] [blame] | 2722 | FD.D.getDeclSpec().getSourceRange().getBegin(), |
| 2723 | FD.D, FD.BitfieldSize); |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2724 | FieldDecls.push_back(Field); |
| 2725 | return Field; |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 2726 | } |
John McCall | bdd563e | 2009-11-03 02:38:08 +0000 | [diff] [blame] | 2727 | } Callback(*this, TagDecl, FieldDecls); |
| 2728 | |
| 2729 | ParseStructDeclaration(DS, Callback); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2730 | } else { // Handle @defs |
| 2731 | ConsumeToken(); |
| 2732 | if (!Tok.isObjCAtKeyword(tok::objc_defs)) { |
| 2733 | Diag(Tok, diag::err_unexpected_at); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2734 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2735 | continue; |
| 2736 | } |
| 2737 | ConsumeToken(); |
| 2738 | ExpectAndConsume(tok::l_paren, diag::err_expected_lparen); |
| 2739 | if (!Tok.is(tok::identifier)) { |
| 2740 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2741 | SkipUntil(tok::semi, true); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2742 | continue; |
| 2743 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2744 | SmallVector<Decl *, 16> Fields; |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2745 | Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(), |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 2746 | Tok.getIdentifierInfo(), Fields); |
Chris Lattner | 5a6ddbf | 2008-06-21 19:39:06 +0000 | [diff] [blame] | 2747 | FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end()); |
| 2748 | ConsumeToken(); |
| 2749 | ExpectAndConsume(tok::r_paren, diag::err_expected_rparen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2750 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2751 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2752 | if (Tok.is(tok::semi)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2753 | ConsumeToken(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 2754 | } else if (Tok.is(tok::r_brace)) { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2755 | ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2756 | break; |
| 2757 | } else { |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2758 | ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list); |
| 2759 | // 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] | 2760 | SkipUntil(tok::r_brace, true, true); |
Chris Lattner | 3e156ad | 2010-02-02 00:37:27 +0000 | [diff] [blame] | 2761 | // If we stopped at a ';', eat it. |
| 2762 | if (Tok.is(tok::semi)) ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2763 | } |
| 2764 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2765 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2766 | T.consumeClose(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2767 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2768 | ParsedAttributes attrs(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2769 | // If attributes exist after struct contents, parse them. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2770 | MaybeParseGNUAttributes(attrs); |
Daniel Dunbar | 1bfe1c2 | 2008-10-03 02:03:53 +0000 | [diff] [blame] | 2771 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2772 | Actions.ActOnFields(getCurScope(), |
David Blaikie | 77b6de0 | 2011-09-22 02:58:26 +0000 | [diff] [blame] | 2773 | RecordLoc, TagDecl, FieldDecls, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2774 | T.getOpenLocation(), T.getCloseLocation(), |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2775 | attrs.getList()); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 2776 | StructScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 2777 | Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, |
| 2778 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2779 | } |
| 2780 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2781 | /// ParseEnumSpecifier |
| 2782 | /// enum-specifier: [C99 6.7.2.2] |
| 2783 | /// 'enum' identifier[opt] '{' enumerator-list '}' |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2784 | ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2785 | /// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 2786 | /// '}' attributes[opt] |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 2787 | /// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt] |
| 2788 | /// '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2789 | /// 'enum' identifier |
| 2790 | /// [GNU] 'enum' attributes[opt] identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2791 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2792 | /// [C++11] enum-head '{' enumerator-list[opt] '}' |
| 2793 | /// [C++11] enum-head '{' enumerator-list ',' '}' |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2794 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2795 | /// enum-head: [C++11] |
| 2796 | /// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt] |
| 2797 | /// enum-key attribute-specifier-seq[opt] nested-name-specifier |
| 2798 | /// identifier enum-base[opt] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2799 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2800 | /// enum-key: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2801 | /// 'enum' |
| 2802 | /// 'enum' 'class' |
| 2803 | /// 'enum' 'struct' |
| 2804 | /// |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2805 | /// enum-base: [C++11] |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2806 | /// ':' type-specifier-seq |
| 2807 | /// |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2808 | /// [C++] elaborated-type-specifier: |
| 2809 | /// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier |
| 2810 | /// |
Chris Lattner | 4c97d76 | 2009-04-12 21:49:30 +0000 | [diff] [blame] | 2811 | void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS, |
Douglas Gregor | 9b9edd6 | 2010-03-02 17:53:14 +0000 | [diff] [blame] | 2812 | const ParsedTemplateInfo &TemplateInfo, |
Richard Smith | 69730c1 | 2012-03-12 07:56:15 +0000 | [diff] [blame] | 2813 | AccessSpecifier AS, DeclSpecContext DSC) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2814 | // Parse the tag portion of this. |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2815 | if (Tok.is(tok::code_completion)) { |
| 2816 | // Code completion for an enum name. |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 2817 | Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 2818 | return cutOffParsing(); |
Douglas Gregor | 374929f | 2009-09-18 15:37:17 +0000 | [diff] [blame] | 2819 | } |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2820 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2821 | SourceLocation ScopedEnumKWLoc; |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2822 | bool IsScopedUsingClassTag = false; |
| 2823 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2824 | if (getLangOpts().CPlusPlus0x && |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2825 | (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2826 | Diag(Tok, diag::warn_cxx98_compat_scoped_enum); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2827 | IsScopedUsingClassTag = Tok.is(tok::kw_class); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2828 | ScopedEnumKWLoc = ConsumeToken(); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2829 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2830 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 2831 | // C++11 [temp.explicit]p12: |
| 2832 | // The usual access controls do not apply to names used to specify |
| 2833 | // explicit instantiations. |
| 2834 | // We extend this to also cover explicit specializations. Note that |
| 2835 | // we don't suppress if this turns out to be an elaborated type |
| 2836 | // specifier. |
| 2837 | bool shouldDelayDiagsInTag = |
| 2838 | (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation || |
| 2839 | TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization); |
| 2840 | SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2841 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2842 | // If attributes exist after tag, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 2843 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 2844 | MaybeParseGNUAttributes(attrs); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2845 | |
Aaron Ballman | 6454a02 | 2012-03-01 04:09:28 +0000 | [diff] [blame] | 2846 | // If declspecs exist after tag, parse them. |
| 2847 | while (Tok.is(tok::kw___declspec)) |
| 2848 | ParseMicrosoftDeclSpec(attrs); |
| 2849 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2850 | // Enum definitions should not be parsed in a trailing-return-type. |
| 2851 | bool AllowDeclaration = DSC != DSC_trailing; |
| 2852 | |
| 2853 | bool AllowFixedUnderlyingType = AllowDeclaration && |
| 2854 | (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt || |
| 2855 | getLangOpts().ObjC2); |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2856 | |
Abramo Bagnara | e4da7a0 | 2010-05-19 21:37:53 +0000 | [diff] [blame] | 2857 | CXXScopeSpec &SS = DS.getTypeSpecScope(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2858 | if (getLangOpts().CPlusPlus) { |
John McCall | 57c1300 | 2011-07-06 05:58:41 +0000 | [diff] [blame] | 2859 | // "enum foo : bar;" is not a potential typo for "enum foo::bar;" |
| 2860 | // if a fixed underlying type is allowed. |
| 2861 | ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType); |
| 2862 | |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 2863 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
| 2864 | /*EnteringContext=*/false)) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 2865 | return; |
| 2866 | |
| 2867 | if (SS.isSet() && Tok.isNot(tok::identifier)) { |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 2868 | Diag(Tok, diag::err_expected_ident); |
| 2869 | if (Tok.isNot(tok::l_brace)) { |
| 2870 | // Has no name and is not a definition. |
| 2871 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2872 | SkipUntil(tok::comma, true); |
| 2873 | return; |
| 2874 | } |
| 2875 | } |
| 2876 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2877 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2878 | // Must have either 'enum name' or 'enum {...}'. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2879 | if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) && |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2880 | !(AllowFixedUnderlyingType && Tok.is(tok::colon))) { |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2881 | Diag(Tok, diag::err_expected_ident_lbrace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2882 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2883 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 2884 | SkipUntil(tok::comma, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2885 | return; |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2886 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2887 | |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2888 | // If an identifier is present, consume and remember it. |
| 2889 | IdentifierInfo *Name = 0; |
| 2890 | SourceLocation NameLoc; |
| 2891 | if (Tok.is(tok::identifier)) { |
| 2892 | Name = Tok.getIdentifierInfo(); |
| 2893 | NameLoc = ConsumeToken(); |
| 2894 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2895 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2896 | if (!Name && ScopedEnumKWLoc.isValid()) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2897 | // C++0x 7.2p2: The optional identifier shall not be omitted in the |
| 2898 | // declaration of a scoped enumeration. |
| 2899 | Diag(Tok, diag::err_scoped_enum_missing_identifier); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2900 | ScopedEnumKWLoc = SourceLocation(); |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 2901 | IsScopedUsingClassTag = false; |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 2904 | // Okay, end the suppression area. We'll decide whether to emit the |
| 2905 | // diagnostics in a second. |
| 2906 | if (shouldDelayDiagsInTag) |
| 2907 | diagsFromTag.done(); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 2908 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2909 | TypeResult BaseType; |
| 2910 | |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2911 | // Parse the fixed underlying type. |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 2912 | if (AllowFixedUnderlyingType && Tok.is(tok::colon)) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2913 | bool PossibleBitfield = false; |
| 2914 | if (getCurScope()->getFlags() & Scope::ClassScope) { |
| 2915 | // If we're in class scope, this can either be an enum declaration with |
| 2916 | // an underlying type, or a declaration of a bitfield member. We try to |
| 2917 | // use a simple disambiguation scheme first to catch the common cases |
| 2918 | // (integer literal, sizeof); if it's still ambiguous, we then consider |
| 2919 | // anything that's a simple-type-specifier followed by '(' as an |
| 2920 | // expression. This suffices because function types are not valid |
| 2921 | // underlying types anyway. |
| 2922 | TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind()); |
| 2923 | // If the next token starts an expression, we know we're parsing a |
| 2924 | // bit-field. This is the common case. |
| 2925 | if (TPR == TPResult::True()) |
| 2926 | PossibleBitfield = true; |
| 2927 | // If the next token starts a type-specifier-seq, it may be either a |
| 2928 | // a fixed underlying type or the start of a function-style cast in C++; |
| 2929 | // lookahead one more token to see if it's obvious that we have a |
| 2930 | // fixed underlying type. |
| 2931 | else if (TPR == TPResult::False() && |
| 2932 | GetLookAheadToken(2).getKind() == tok::semi) { |
| 2933 | // Consume the ':'. |
| 2934 | ConsumeToken(); |
| 2935 | } else { |
| 2936 | // We have the start of a type-specifier-seq, so we have to perform |
| 2937 | // tentative parsing to determine whether we have an expression or a |
| 2938 | // type. |
| 2939 | TentativeParsingAction TPA(*this); |
| 2940 | |
| 2941 | // Consume the ':'. |
| 2942 | ConsumeToken(); |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 2943 | |
| 2944 | // If we see a type specifier followed by an open-brace, we have an |
| 2945 | // ambiguity between an underlying type and a C++11 braced |
| 2946 | // function-style cast. Resolve this by always treating it as an |
| 2947 | // underlying type. |
| 2948 | // FIXME: The standard is not entirely clear on how to disambiguate in |
| 2949 | // this case. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2950 | if ((getLangOpts().CPlusPlus && |
Richard Smith | d81e961 | 2012-02-23 01:36:12 +0000 | [diff] [blame] | 2951 | isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2952 | (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) { |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2953 | // We'll parse this as a bitfield later. |
| 2954 | PossibleBitfield = true; |
| 2955 | TPA.Revert(); |
| 2956 | } else { |
| 2957 | // We have a type-specifier-seq. |
| 2958 | TPA.Commit(); |
| 2959 | } |
| 2960 | } |
| 2961 | } else { |
| 2962 | // Consume the ':'. |
| 2963 | ConsumeToken(); |
| 2964 | } |
| 2965 | |
| 2966 | if (!PossibleBitfield) { |
| 2967 | SourceRange Range; |
| 2968 | BaseType = ParseTypeName(&Range); |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 2969 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2970 | if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2) |
Douglas Gregor | 86f208c | 2011-02-22 20:32:04 +0000 | [diff] [blame] | 2971 | Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type) |
| 2972 | << Range; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2973 | if (getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 2974 | Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type); |
Douglas Gregor | a61b3e7 | 2010-12-01 17:42:47 +0000 | [diff] [blame] | 2975 | } |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 2976 | } |
| 2977 | |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 2978 | // There are four options here. If we have 'friend enum foo;' then this is a |
| 2979 | // friend declaration, and cannot have an accompanying definition. If we have |
| 2980 | // 'enum foo;', then this is a forward declaration. If we have |
| 2981 | // 'enum foo {...' then this is a definition. Otherwise we have something |
| 2982 | // like 'enum foo xyz', a reference. |
Argyrios Kyrtzidis | e281b4c | 2008-09-11 00:21:41 +0000 | [diff] [blame] | 2983 | // |
| 2984 | // This is needed to handle stuff like this right (C99 6.7.2.3p11): |
| 2985 | // enum foo {..}; void bar() { enum foo; } <- new foo in bar. |
| 2986 | // enum foo {..}; void bar() { enum foo x; } <- use of old foo. |
| 2987 | // |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 2988 | Sema::TagUseKind TUK; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 2989 | if (!AllowDeclaration) { |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 2990 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 2991 | } else if (Tok.is(tok::l_brace)) { |
| 2992 | if (DS.isFriendSpecified()) { |
| 2993 | Diag(Tok.getLocation(), diag::err_friend_decl_defines_type) |
| 2994 | << SourceRange(DS.getFriendSpecLoc()); |
| 2995 | ConsumeBrace(); |
| 2996 | SkipUntil(tok::r_brace); |
| 2997 | TUK = Sema::TUK_Friend; |
| 2998 | } else { |
| 2999 | TUK = Sema::TUK_Definition; |
| 3000 | } |
| 3001 | } else if (Tok.is(tok::semi) && DSC != DSC_type_specifier) { |
| 3002 | TUK = (DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration); |
| 3003 | } else { |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3004 | TUK = Sema::TUK_Reference; |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3005 | } |
| 3006 | |
| 3007 | // If this is an elaborated type specifier, and we delayed |
| 3008 | // diagnostics before, just merge them into the current pool. |
| 3009 | if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) { |
| 3010 | diagsFromTag.redelay(); |
| 3011 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3012 | |
| 3013 | MultiTemplateParamsArg TParams; |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3014 | if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate && |
John McCall | f312b1e | 2010-08-26 23:41:50 +0000 | [diff] [blame] | 3015 | TUK != Sema::TUK_Reference) { |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3016 | if (!getLangOpts().CPlusPlus0x || !SS.isSet()) { |
| 3017 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3018 | Diag(Tok, diag::err_enum_template); |
| 3019 | SkipUntil(tok::comma, true); |
| 3020 | return; |
| 3021 | } |
| 3022 | |
| 3023 | if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) { |
| 3024 | // Enumerations can't be explicitly instantiated. |
| 3025 | DS.SetTypeSpecError(); |
| 3026 | Diag(StartLoc, diag::err_explicit_instantiation_enum); |
| 3027 | return; |
| 3028 | } |
| 3029 | |
| 3030 | assert(TemplateInfo.TemplateParams && "no template parameters"); |
| 3031 | TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(), |
| 3032 | TemplateInfo.TemplateParams->size()); |
Douglas Gregor | 8fc6d23 | 2010-05-03 17:48:54 +0000 | [diff] [blame] | 3033 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3034 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3035 | if (!Name && TUK != Sema::TUK_Definition) { |
| 3036 | Diag(Tok, diag::err_enumerator_unnamed_no_def); |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3037 | |
Douglas Gregor | b907560 | 2011-02-22 02:55:24 +0000 | [diff] [blame] | 3038 | // Skip the rest of this declarator, up until the comma or semicolon. |
| 3039 | SkipUntil(tok::comma, true); |
| 3040 | return; |
| 3041 | } |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3042 | |
Douglas Gregor | 402abb5 | 2009-05-28 23:31:59 +0000 | [diff] [blame] | 3043 | bool Owned = false; |
John McCall | c4e7019 | 2009-09-11 04:59:25 +0000 | [diff] [blame] | 3044 | bool IsDependent = false; |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3045 | const char *PrevSpec = 0; |
| 3046 | unsigned DiagID; |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3047 | Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3048 | StartLoc, SS, Name, NameLoc, attrs.getList(), |
Richard Smith | 1af83c4 | 2012-03-23 03:33:32 +0000 | [diff] [blame] | 3049 | AS, DS.getModulePrivateSpecLoc(), TParams, |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3050 | Owned, IsDependent, ScopedEnumKWLoc, |
Abramo Bagnara | a88cefd | 2010-12-03 18:54:17 +0000 | [diff] [blame] | 3051 | IsScopedUsingClassTag, BaseType); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 3052 | |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3053 | if (IsDependent) { |
| 3054 | // This enum has a dependent nested-name-specifier. Handle it as a |
| 3055 | // dependent tag. |
| 3056 | if (!Name) { |
| 3057 | DS.SetTypeSpecError(); |
| 3058 | Diag(Tok, diag::err_expected_type_name_after_typename); |
| 3059 | return; |
| 3060 | } |
| 3061 | |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3062 | TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum, |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3063 | TUK, SS, Name, StartLoc, |
| 3064 | NameLoc); |
| 3065 | if (Type.isInvalid()) { |
| 3066 | DS.SetTypeSpecError(); |
| 3067 | return; |
| 3068 | } |
| 3069 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3070 | if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, |
| 3071 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3072 | PrevSpec, DiagID, Type.get())) |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3073 | Diag(StartLoc, DiagID) << PrevSpec; |
| 3074 | |
| 3075 | return; |
| 3076 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3077 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3078 | if (!TagDecl) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3079 | // The action failed to produce an enumeration tag. If this is a |
| 3080 | // definition, consume the entire definition. |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3081 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
Douglas Gregor | 48c89f4 | 2010-04-24 16:38:41 +0000 | [diff] [blame] | 3082 | ConsumeBrace(); |
| 3083 | SkipUntil(tok::r_brace); |
| 3084 | } |
| 3085 | |
| 3086 | DS.SetTypeSpecError(); |
| 3087 | return; |
| 3088 | } |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3089 | |
Richard Smith | 7796eb5 | 2012-03-12 08:56:40 +0000 | [diff] [blame] | 3090 | if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) { |
John McCall | 1348967 | 2012-05-07 06:16:58 +0000 | [diff] [blame] | 3091 | ParseEnumBody(StartLoc, TagDecl); |
Richard Smith | bdad7a2 | 2012-01-10 01:33:14 +0000 | [diff] [blame] | 3092 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3093 | |
Abramo Bagnara | 0daaf32 | 2011-03-16 20:16:18 +0000 | [diff] [blame] | 3094 | if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc, |
| 3095 | NameLoc.isValid() ? NameLoc : StartLoc, |
| 3096 | PrevSpec, DiagID, TagDecl, Owned)) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3097 | Diag(StartLoc, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3098 | } |
| 3099 | |
| 3100 | /// ParseEnumBody - Parse a {} enclosed enumerator-list. |
| 3101 | /// enumerator-list: |
| 3102 | /// enumerator |
| 3103 | /// enumerator-list ',' enumerator |
| 3104 | /// enumerator: |
| 3105 | /// enumeration-constant |
| 3106 | /// enumeration-constant '=' constant-expression |
| 3107 | /// enumeration-constant: |
| 3108 | /// identifier |
| 3109 | /// |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3110 | void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) { |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3111 | // Enter the scope of the enum body and start the definition. |
| 3112 | ParseScope EnumScope(this, Scope::DeclScope); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3113 | Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl); |
Douglas Gregor | 074149e | 2009-01-05 19:45:36 +0000 | [diff] [blame] | 3114 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3115 | BalancedDelimiterTracker T(*this, tok::l_brace); |
| 3116 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3117 | |
Chris Lattner | 7946dd3 | 2007-08-27 17:24:30 +0000 | [diff] [blame] | 3118 | // C does not allow an empty enumerator-list, C++ does [dcl.enum]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3119 | if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 0511552 | 2010-05-28 22:23:22 +0000 | [diff] [blame] | 3120 | Diag(Tok, diag::error_empty_enum); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3121 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3122 | SmallVector<Decl *, 32> EnumConstantDecls; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3123 | |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3124 | Decl *LastEnumConstDecl = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3125 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3126 | // Parse the enumerator-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3127 | while (Tok.is(tok::identifier)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3128 | IdentifierInfo *Ident = Tok.getIdentifierInfo(); |
| 3129 | SourceLocation IdentLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3130 | |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3131 | // If attributes exist after the enumerator, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3132 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3133 | MaybeParseGNUAttributes(attrs); |
John McCall | 5b629aa | 2010-10-22 23:36:17 +0000 | [diff] [blame] | 3134 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3135 | SourceLocation EqualLoc; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3136 | ExprResult AssignedVal; |
John McCall | 9257664 | 2012-05-07 06:16:41 +0000 | [diff] [blame] | 3137 | ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3138 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3139 | if (Tok.is(tok::equal)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3140 | EqualLoc = ConsumeToken(); |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 3141 | AssignedVal = ParseConstantExpression(); |
| 3142 | if (AssignedVal.isInvalid()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3143 | SkipUntil(tok::comma, tok::r_brace, true, true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3144 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3145 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3146 | // Install the enumerator constant into EnumDecl. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3147 | Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl, |
| 3148 | LastEnumConstDecl, |
| 3149 | IdentLoc, Ident, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3150 | attrs.getList(), EqualLoc, |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3151 | AssignedVal.release()); |
Fariborz Jahanian | 5a477db | 2011-12-09 01:15:54 +0000 | [diff] [blame] | 3152 | PD.complete(EnumConstDecl); |
| 3153 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3154 | EnumConstantDecls.push_back(EnumConstDecl); |
| 3155 | LastEnumConstDecl = EnumConstDecl; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3156 | |
Douglas Gregor | 751f692 | 2010-09-07 14:51:08 +0000 | [diff] [blame] | 3157 | if (Tok.is(tok::identifier)) { |
| 3158 | // We're missing a comma between enumerators. |
| 3159 | SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation); |
| 3160 | Diag(Loc, diag::err_enumerator_list_missing_comma) |
| 3161 | << FixItHint::CreateInsertion(Loc, ", "); |
| 3162 | continue; |
| 3163 | } |
| 3164 | |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 3165 | if (Tok.isNot(tok::comma)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3166 | break; |
| 3167 | SourceLocation CommaLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3168 | |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3169 | if (Tok.isNot(tok::identifier)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3170 | if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3171 | Diag(CommaLoc, diag::ext_enumerator_list_comma) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3172 | << getLangOpts().CPlusPlus |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3173 | << FixItHint::CreateRemoval(CommaLoc); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3174 | else if (getLangOpts().CPlusPlus0x) |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3175 | Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma) |
| 3176 | << FixItHint::CreateRemoval(CommaLoc); |
| 3177 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3178 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3179 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3180 | // Eat the }. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3181 | T.consumeClose(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3182 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3183 | // If attributes exist after the identifier list, parse them. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3184 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3185 | MaybeParseGNUAttributes(attrs); |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3186 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3187 | Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(), |
| 3188 | EnumDecl, EnumConstantDecls.data(), |
| 3189 | EnumConstantDecls.size(), getCurScope(), |
| 3190 | attrs.getList()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3191 | |
Douglas Gregor | 72de667 | 2009-01-08 20:45:30 +0000 | [diff] [blame] | 3192 | EnumScope.Exit(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 3193 | Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, |
| 3194 | T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
| 3197 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3198 | /// start of a type-qualifier-list. |
| 3199 | bool Parser::isTypeQualifier() const { |
| 3200 | switch (Tok.getKind()) { |
| 3201 | default: return false; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3202 | |
| 3203 | // type-qualifier only in OpenCL |
| 3204 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3205 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3206 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3207 | // type-qualifier |
| 3208 | case tok::kw_const: |
| 3209 | case tok::kw_volatile: |
| 3210 | case tok::kw_restrict: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3211 | case tok::kw___private: |
| 3212 | case tok::kw___local: |
| 3213 | case tok::kw___global: |
| 3214 | case tok::kw___constant: |
| 3215 | case tok::kw___read_only: |
| 3216 | case tok::kw___read_write: |
| 3217 | case tok::kw___write_only: |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3218 | return true; |
| 3219 | } |
| 3220 | } |
| 3221 | |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3222 | /// isKnownToBeTypeSpecifier - Return true if we know that the specified token |
| 3223 | /// is definitely a type-specifier. Return false if it isn't part of a type |
| 3224 | /// specifier or if we're not sure. |
| 3225 | bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const { |
| 3226 | switch (Tok.getKind()) { |
| 3227 | default: return false; |
| 3228 | // type-specifiers |
| 3229 | case tok::kw_short: |
| 3230 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3231 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3232 | case tok::kw___int128: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3233 | case tok::kw_signed: |
| 3234 | case tok::kw_unsigned: |
| 3235 | case tok::kw__Complex: |
| 3236 | case tok::kw__Imaginary: |
| 3237 | case tok::kw_void: |
| 3238 | case tok::kw_char: |
| 3239 | case tok::kw_wchar_t: |
| 3240 | case tok::kw_char16_t: |
| 3241 | case tok::kw_char32_t: |
| 3242 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3243 | case tok::kw_half: |
Chris Lattner | b3a4e43 | 2010-02-28 18:18:36 +0000 | [diff] [blame] | 3244 | case tok::kw_float: |
| 3245 | case tok::kw_double: |
| 3246 | case tok::kw_bool: |
| 3247 | case tok::kw__Bool: |
| 3248 | case tok::kw__Decimal32: |
| 3249 | case tok::kw__Decimal64: |
| 3250 | case tok::kw__Decimal128: |
| 3251 | case tok::kw___vector: |
| 3252 | |
| 3253 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3254 | case tok::kw_class: |
| 3255 | case tok::kw_struct: |
| 3256 | case tok::kw_union: |
| 3257 | // enum-specifier |
| 3258 | case tok::kw_enum: |
| 3259 | |
| 3260 | // typedef-name |
| 3261 | case tok::annot_typename: |
| 3262 | return true; |
| 3263 | } |
| 3264 | } |
| 3265 | |
Steve Naroff | 5f8aa69 | 2008-02-11 23:15:56 +0000 | [diff] [blame] | 3266 | /// isTypeSpecifierQualifier - Return true if the current token could be the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3267 | /// start of a specifier-qualifier-list. |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3268 | bool Parser::isTypeSpecifierQualifier() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3269 | switch (Tok.getKind()) { |
| 3270 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3271 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3272 | case tok::identifier: // foo::bar |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3273 | if (TryAltiVecVectorToken()) |
| 3274 | return true; |
| 3275 | // Fall through. |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3276 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3277 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3278 | // recurse to handle whatever we get. |
| 3279 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3280 | return true; |
| 3281 | if (Tok.is(tok::identifier)) |
| 3282 | return false; |
| 3283 | return isTypeSpecifierQualifier(); |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3284 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3285 | case tok::coloncolon: // ::foo::bar |
| 3286 | if (NextToken().is(tok::kw_new) || // ::new |
| 3287 | NextToken().is(tok::kw_delete)) // ::delete |
| 3288 | return false; |
| 3289 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3290 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3291 | return true; |
| 3292 | return isTypeSpecifierQualifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3293 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3294 | // GNU attributes support. |
| 3295 | case tok::kw___attribute: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3296 | // GNU typeof support. |
| 3297 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3298 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3299 | // type-specifiers |
| 3300 | case tok::kw_short: |
| 3301 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3302 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3303 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3304 | case tok::kw_signed: |
| 3305 | case tok::kw_unsigned: |
| 3306 | case tok::kw__Complex: |
| 3307 | case tok::kw__Imaginary: |
| 3308 | case tok::kw_void: |
| 3309 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3310 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3311 | case tok::kw_char16_t: |
| 3312 | case tok::kw_char32_t: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3313 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3314 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3315 | case tok::kw_float: |
| 3316 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3317 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3318 | case tok::kw__Bool: |
| 3319 | case tok::kw__Decimal32: |
| 3320 | case tok::kw__Decimal64: |
| 3321 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3322 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3323 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3324 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3325 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3326 | case tok::kw_struct: |
| 3327 | case tok::kw_union: |
| 3328 | // enum-specifier |
| 3329 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3330 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3331 | // type-qualifier |
| 3332 | case tok::kw_const: |
| 3333 | case tok::kw_volatile: |
| 3334 | case tok::kw_restrict: |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3335 | |
| 3336 | // typedef-name |
Chris Lattner | b31757b | 2009-01-06 05:06:21 +0000 | [diff] [blame] | 3337 | case tok::annot_typename: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3338 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3339 | |
Chris Lattner | 7c186be | 2008-10-20 00:25:30 +0000 | [diff] [blame] | 3340 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3341 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3342 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3343 | |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3344 | case tok::kw___cdecl: |
| 3345 | case tok::kw___stdcall: |
| 3346 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3347 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3348 | case tok::kw___w64: |
| 3349 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3350 | case tok::kw___ptr32: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3351 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3352 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3353 | |
| 3354 | case tok::kw___private: |
| 3355 | case tok::kw___local: |
| 3356 | case tok::kw___global: |
| 3357 | case tok::kw___constant: |
| 3358 | case tok::kw___read_only: |
| 3359 | case tok::kw___read_write: |
| 3360 | case tok::kw___write_only: |
| 3361 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3362 | return true; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3363 | |
| 3364 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3365 | return getLangOpts().OpenCL; |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3366 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3367 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3368 | case tok::kw__Atomic: |
| 3369 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3370 | } |
| 3371 | } |
| 3372 | |
| 3373 | /// isDeclarationSpecifier() - Return true if the current token is part of a |
| 3374 | /// declaration specifier. |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3375 | /// |
| 3376 | /// \param DisambiguatingWithExpression True to indicate that the purpose of |
| 3377 | /// this check is to disambiguate between an expression and a declaration. |
| 3378 | bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3379 | switch (Tok.getKind()) { |
| 3380 | default: return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3381 | |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3382 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3383 | return getLangOpts().OpenCL; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3384 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3385 | case tok::identifier: // foo::bar |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3386 | // Unfortunate hack to support "Class.factoryMethod" notation. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3387 | if (getLangOpts().ObjC1 && NextToken().is(tok::period)) |
Steve Naroff | 61f72cb | 2009-03-09 21:12:44 +0000 | [diff] [blame] | 3388 | return false; |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3389 | if (TryAltiVecVectorToken()) |
| 3390 | return true; |
| 3391 | // Fall through. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3392 | case tok::kw_decltype: // decltype(T())::type |
Douglas Gregor | d57959a | 2009-03-27 23:10:48 +0000 | [diff] [blame] | 3393 | case tok::kw_typename: // typename T::type |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3394 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3395 | // recurse to handle whatever we get. |
| 3396 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3397 | return true; |
| 3398 | if (Tok.is(tok::identifier)) |
| 3399 | return false; |
Douglas Gregor | 9497a73 | 2010-09-16 01:51:54 +0000 | [diff] [blame] | 3400 | |
| 3401 | // If we're in Objective-C and we have an Objective-C class type followed |
| 3402 | // by an identifier and then either ':' or ']', in a place where an |
| 3403 | // expression is permitted, then this is probably a class message send |
| 3404 | // missing the initial '['. In this case, we won't consider this to be |
| 3405 | // the start of a declaration. |
| 3406 | if (DisambiguatingWithExpression && |
| 3407 | isStartOfObjCClassMessageMissingOpenBracket()) |
| 3408 | return false; |
| 3409 | |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3410 | return isDeclarationSpecifier(); |
| 3411 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3412 | case tok::coloncolon: // ::foo::bar |
| 3413 | if (NextToken().is(tok::kw_new) || // ::new |
| 3414 | NextToken().is(tok::kw_delete)) // ::delete |
| 3415 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3416 | |
Chris Lattner | 166a8fc | 2009-01-04 23:41:41 +0000 | [diff] [blame] | 3417 | // Annotate typenames and C++ scope specifiers. If we get one, just |
| 3418 | // recurse to handle whatever we get. |
| 3419 | if (TryAnnotateTypeOrScopeToken()) |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3420 | return true; |
| 3421 | return isDeclarationSpecifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3422 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3423 | // storage-class-specifier |
| 3424 | case tok::kw_typedef: |
| 3425 | case tok::kw_extern: |
Steve Naroff | 8d54bf2 | 2007-12-18 00:16:02 +0000 | [diff] [blame] | 3426 | case tok::kw___private_extern__: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3427 | case tok::kw_static: |
| 3428 | case tok::kw_auto: |
| 3429 | case tok::kw_register: |
| 3430 | case tok::kw___thread: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3431 | |
Douglas Gregor | 8d267c5 | 2011-09-09 02:06:17 +0000 | [diff] [blame] | 3432 | // Modules |
| 3433 | case tok::kw___module_private__: |
| 3434 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3435 | // type-specifiers |
| 3436 | case tok::kw_short: |
| 3437 | case tok::kw_long: |
Francois Pichet | 338d7f7 | 2011-04-28 01:59:37 +0000 | [diff] [blame] | 3438 | case tok::kw___int64: |
Richard Smith | 5a5a971 | 2012-04-04 06:24:32 +0000 | [diff] [blame] | 3439 | case tok::kw___int128: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3440 | case tok::kw_signed: |
| 3441 | case tok::kw_unsigned: |
| 3442 | case tok::kw__Complex: |
| 3443 | case tok::kw__Imaginary: |
| 3444 | case tok::kw_void: |
| 3445 | case tok::kw_char: |
Argyrios Kyrtzidis | 64c438a | 2008-08-09 16:51:54 +0000 | [diff] [blame] | 3446 | case tok::kw_wchar_t: |
Alisdair Meredith | f5c209d | 2009-07-14 06:30:34 +0000 | [diff] [blame] | 3447 | case tok::kw_char16_t: |
| 3448 | case tok::kw_char32_t: |
| 3449 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3450 | case tok::kw_int: |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 3451 | case tok::kw_half: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3452 | case tok::kw_float: |
| 3453 | case tok::kw_double: |
Chris Lattner | 9298d96 | 2007-11-15 05:25:19 +0000 | [diff] [blame] | 3454 | case tok::kw_bool: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3455 | case tok::kw__Bool: |
| 3456 | case tok::kw__Decimal32: |
| 3457 | case tok::kw__Decimal64: |
| 3458 | case tok::kw__Decimal128: |
John Thompson | 82287d1 | 2010-02-05 00:12:22 +0000 | [diff] [blame] | 3459 | case tok::kw___vector: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3460 | |
Chris Lattner | 99dc914 | 2008-04-13 18:59:07 +0000 | [diff] [blame] | 3461 | // struct-or-union-specifier (C99) or class-specifier (C++) |
| 3462 | case tok::kw_class: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3463 | case tok::kw_struct: |
| 3464 | case tok::kw_union: |
| 3465 | // enum-specifier |
| 3466 | case tok::kw_enum: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3467 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3468 | // type-qualifier |
| 3469 | case tok::kw_const: |
| 3470 | case tok::kw_volatile: |
| 3471 | case tok::kw_restrict: |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 3472 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3473 | // function-specifier |
| 3474 | case tok::kw_inline: |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3475 | case tok::kw_virtual: |
| 3476 | case tok::kw_explicit: |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3477 | |
Peter Collingbourne | c6eb44b | 2011-04-15 00:35:57 +0000 | [diff] [blame] | 3478 | // static_assert-declaration |
| 3479 | case tok::kw__Static_assert: |
| 3480 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3481 | // GNU typeof support. |
| 3482 | case tok::kw_typeof: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3483 | |
Chris Lattner | 1ef0876 | 2007-08-09 17:01:07 +0000 | [diff] [blame] | 3484 | // GNU attributes. |
Chris Lattner | d6c7c18 | 2007-08-09 16:40:21 +0000 | [diff] [blame] | 3485 | case tok::kw___attribute: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3486 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3487 | |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3488 | // C++0x decltype. |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 3489 | case tok::annot_decltype: |
Francois Pichet | e3d49b4 | 2011-06-19 08:02:06 +0000 | [diff] [blame] | 3490 | return true; |
| 3491 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 3492 | // C11 _Atomic() |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 3493 | case tok::kw__Atomic: |
| 3494 | return true; |
| 3495 | |
Chris Lattner | f3948c4 | 2008-07-26 03:38:44 +0000 | [diff] [blame] | 3496 | // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'. |
| 3497 | case tok::less: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3498 | return getLangOpts().ObjC1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3499 | |
Douglas Gregor | d9d75e5 | 2011-04-27 05:41:15 +0000 | [diff] [blame] | 3500 | // typedef-name |
| 3501 | case tok::annot_typename: |
| 3502 | return !DisambiguatingWithExpression || |
| 3503 | !isStartOfObjCClassMessageMissingOpenBracket(); |
| 3504 | |
Steve Naroff | 47f5209 | 2009-01-06 19:34:12 +0000 | [diff] [blame] | 3505 | case tok::kw___declspec: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3506 | case tok::kw___cdecl: |
| 3507 | case tok::kw___stdcall: |
| 3508 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3509 | case tok::kw___thiscall: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3510 | case tok::kw___w64: |
| 3511 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3512 | case tok::kw___ptr32: |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3513 | case tok::kw___forceinline: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3514 | case tok::kw___pascal: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3515 | case tok::kw___unaligned: |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3516 | |
| 3517 | case tok::kw___private: |
| 3518 | case tok::kw___local: |
| 3519 | case tok::kw___global: |
| 3520 | case tok::kw___constant: |
| 3521 | case tok::kw___read_only: |
| 3522 | case tok::kw___read_write: |
| 3523 | case tok::kw___write_only: |
| 3524 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3525 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3526 | } |
| 3527 | } |
| 3528 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3529 | bool Parser::isConstructorDeclarator() { |
| 3530 | TentativeParsingAction TPA(*this); |
| 3531 | |
| 3532 | // Parse the C++ scope specifier. |
| 3533 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3534 | if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(), |
| 3535 | /*EnteringContext=*/true)) { |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3536 | TPA.Revert(); |
| 3537 | return false; |
| 3538 | } |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3539 | |
| 3540 | // Parse the constructor name. |
| 3541 | if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) { |
| 3542 | // We already know that we have a constructor name; just consume |
| 3543 | // the token. |
| 3544 | ConsumeToken(); |
| 3545 | } else { |
| 3546 | TPA.Revert(); |
| 3547 | return false; |
| 3548 | } |
| 3549 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 3550 | // Current class name must be followed by a left parenthesis. |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3551 | if (Tok.isNot(tok::l_paren)) { |
| 3552 | TPA.Revert(); |
| 3553 | return false; |
| 3554 | } |
| 3555 | ConsumeParen(); |
| 3556 | |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 3557 | // A right parenthesis, or ellipsis followed by a right parenthesis signals |
| 3558 | // that we have a constructor. |
| 3559 | if (Tok.is(tok::r_paren) || |
| 3560 | (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) { |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3561 | TPA.Revert(); |
| 3562 | return true; |
| 3563 | } |
| 3564 | |
| 3565 | // If we need to, enter the specified scope. |
| 3566 | DeclaratorScopeObj DeclScopeObj(*this, SS); |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3567 | if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS)) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3568 | DeclScopeObj.EnterDeclaratorScope(); |
| 3569 | |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3570 | // Optionally skip Microsoft attributes. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3571 | ParsedAttributes Attrs(AttrFactory); |
Francois Pichet | dfaa5fb | 2011-01-31 04:54:32 +0000 | [diff] [blame] | 3572 | MaybeParseMicrosoftAttributes(Attrs); |
| 3573 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3574 | // Check whether the next token(s) are part of a declaration |
| 3575 | // specifier, in which case we have the start of a parameter and, |
| 3576 | // therefore, we know that this is a constructor. |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 3577 | bool IsConstructor = false; |
| 3578 | if (isDeclarationSpecifier()) |
| 3579 | IsConstructor = true; |
| 3580 | else if (Tok.is(tok::identifier) || |
| 3581 | (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) { |
| 3582 | // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type. |
| 3583 | // This might be a parenthesized member name, but is more likely to |
| 3584 | // be a constructor declaration with an invalid argument type. Keep |
| 3585 | // looking. |
| 3586 | if (Tok.is(tok::annot_cxxscope)) |
| 3587 | ConsumeToken(); |
| 3588 | ConsumeToken(); |
| 3589 | |
| 3590 | // If this is not a constructor, we must be parsing a declarator, |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 3591 | // which must have one of the following syntactic forms (see the |
| 3592 | // grammar extract at the start of ParseDirectDeclarator): |
Richard Smith | 412e0cc | 2012-03-27 00:56:56 +0000 | [diff] [blame] | 3593 | switch (Tok.getKind()) { |
| 3594 | case tok::l_paren: |
| 3595 | // C(X ( int)); |
| 3596 | case tok::l_square: |
| 3597 | // C(X [ 5]); |
| 3598 | // C(X [ [attribute]]); |
| 3599 | case tok::coloncolon: |
| 3600 | // C(X :: Y); |
| 3601 | // C(X :: *p); |
| 3602 | case tok::r_paren: |
| 3603 | // C(X ) |
| 3604 | // Assume this isn't a constructor, rather than assuming it's a |
| 3605 | // constructor with an unnamed parameter of an ill-formed type. |
| 3606 | break; |
| 3607 | |
| 3608 | default: |
| 3609 | IsConstructor = true; |
| 3610 | break; |
| 3611 | } |
| 3612 | } |
| 3613 | |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 3614 | TPA.Revert(); |
| 3615 | return IsConstructor; |
| 3616 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3617 | |
| 3618 | /// ParseTypeQualifierListOpt |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3619 | /// type-qualifier-list: [C99 6.7.5] |
| 3620 | /// type-qualifier |
| 3621 | /// [vendor] attributes |
| 3622 | /// [ only if VendorAttributesAllowed=true ] |
| 3623 | /// type-qualifier-list type-qualifier |
| 3624 | /// [vendor] type-qualifier-list attributes |
| 3625 | /// [ only if VendorAttributesAllowed=true ] |
| 3626 | /// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq |
| 3627 | /// [ only if CXX0XAttributesAllowed=true ] |
| 3628 | /// Note: vendor can be GNU, MS, etc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3629 | /// |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3630 | void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, |
| 3631 | bool VendorAttributesAllowed, |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3632 | bool CXX11AttributesAllowed) { |
| 3633 | if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed && |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3634 | isCXX11AttributeSpecifier()) { |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3635 | ParsedAttributesWithRange attrs(AttrFactory); |
Richard Smith | c56298d | 2012-04-10 03:25:07 +0000 | [diff] [blame] | 3636 | ParseCXX11Attributes(attrs); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3637 | DS.takeAttributesFrom(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 3638 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3639 | |
| 3640 | SourceLocation EndLoc; |
| 3641 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3642 | while (1) { |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3643 | bool isInvalid = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3644 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3645 | unsigned DiagID = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3646 | SourceLocation Loc = Tok.getLocation(); |
| 3647 | |
| 3648 | switch (Tok.getKind()) { |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3649 | case tok::code_completion: |
| 3650 | Actions.CodeCompleteTypeQualifiers(DS); |
Argyrios Kyrtzidis | 7d10087 | 2011-09-04 03:32:15 +0000 | [diff] [blame] | 3651 | return cutOffParsing(); |
Douglas Gregor | 1a480c4 | 2010-08-27 17:35:51 +0000 | [diff] [blame] | 3652 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3653 | case tok::kw_const: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3654 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3655 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3656 | break; |
| 3657 | case tok::kw_volatile: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3658 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3659 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3660 | break; |
| 3661 | case tok::kw_restrict: |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 3662 | isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID, |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3663 | getLangOpts()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3664 | break; |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3665 | |
| 3666 | // OpenCL qualifiers: |
| 3667 | case tok::kw_private: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3668 | if (!getLangOpts().OpenCL) |
Peter Collingbourne | 207f4d8 | 2011-03-18 22:38:29 +0000 | [diff] [blame] | 3669 | goto DoneWithTypeQuals; |
| 3670 | case tok::kw___private: |
| 3671 | case tok::kw___global: |
| 3672 | case tok::kw___local: |
| 3673 | case tok::kw___constant: |
| 3674 | case tok::kw___read_only: |
| 3675 | case tok::kw___write_only: |
| 3676 | case tok::kw___read_write: |
| 3677 | ParseOpenCLQualifiers(DS); |
| 3678 | break; |
| 3679 | |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3680 | case tok::kw___w64: |
Steve Naroff | 86bc6cf | 2008-12-25 14:41:26 +0000 | [diff] [blame] | 3681 | case tok::kw___ptr64: |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 3682 | case tok::kw___ptr32: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3683 | case tok::kw___cdecl: |
| 3684 | case tok::kw___stdcall: |
| 3685 | case tok::kw___fastcall: |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 3686 | case tok::kw___thiscall: |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 3687 | case tok::kw___unaligned: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3688 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3689 | ParseMicrosoftTypeAttributes(DS.getAttributes()); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 3690 | continue; |
| 3691 | } |
| 3692 | goto DoneWithTypeQuals; |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3693 | case tok::kw___pascal: |
| 3694 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3695 | ParseBorlandTypeAttributes(DS.getAttributes()); |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3696 | continue; |
| 3697 | } |
| 3698 | goto DoneWithTypeQuals; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3699 | case tok::kw___attribute: |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 3700 | if (VendorAttributesAllowed) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 3701 | ParseGNUAttributes(DS.getAttributes()); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3702 | continue; // do *not* consume the next token! |
| 3703 | } |
| 3704 | // otherwise, FALL THROUGH! |
| 3705 | default: |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 3706 | DoneWithTypeQuals: |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3707 | // If this is not a type-qualifier token, we're done reading type |
| 3708 | // qualifiers. First verify that DeclSpec's are consistent. |
Douglas Gregor | 9b3064b | 2009-04-01 22:41:11 +0000 | [diff] [blame] | 3709 | DS.Finish(Diags, PP); |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3710 | if (EndLoc.isValid()) |
| 3711 | DS.SetRangeEnd(EndLoc); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 3712 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3713 | } |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 3714 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3715 | // If the specifier combination wasn't legal, issue a diagnostic. |
| 3716 | if (isInvalid) { |
| 3717 | assert(PrevSpec && "Method did not return previous specifier!"); |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3718 | Diag(Tok, DiagID) << PrevSpec; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3719 | } |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 3720 | EndLoc = ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3721 | } |
| 3722 | } |
| 3723 | |
| 3724 | |
| 3725 | /// ParseDeclarator - Parse and verify a newly-initialized declarator. |
| 3726 | /// |
| 3727 | void Parser::ParseDeclarator(Declarator &D) { |
| 3728 | /// This implements the 'declarator' production in the C grammar, then checks |
| 3729 | /// for well-formedness and issues diagnostics. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3730 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3731 | } |
| 3732 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3733 | static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) { |
| 3734 | if (Kind == tok::star || Kind == tok::caret) |
| 3735 | return true; |
| 3736 | |
| 3737 | // We parse rvalue refs in C++03, because otherwise the errors are scary. |
| 3738 | if (!Lang.CPlusPlus) |
| 3739 | return false; |
| 3740 | |
| 3741 | return Kind == tok::amp || Kind == tok::ampamp; |
| 3742 | } |
| 3743 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3744 | /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator |
| 3745 | /// is parsed by the function passed to it. Pass null, and the direct-declarator |
| 3746 | /// isn't parsed at all, making this function effectively parse the C++ |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3747 | /// ptr-operator production. |
| 3748 | /// |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 3749 | /// 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] | 3750 | /// made to TryParseDeclarator and MightBeDeclarator, and possibly to |
| 3751 | /// isConstructorDeclarator. |
Richard Smith | 0706df4 | 2011-10-19 21:33:05 +0000 | [diff] [blame] | 3752 | /// |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3753 | /// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl] |
| 3754 | /// [C] pointer[opt] direct-declarator |
| 3755 | /// [C++] direct-declarator |
| 3756 | /// [C++] ptr-operator declarator |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3757 | /// |
| 3758 | /// pointer: [C99 6.7.5] |
| 3759 | /// '*' type-qualifier-list[opt] |
| 3760 | /// '*' type-qualifier-list[opt] pointer |
| 3761 | /// |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3762 | /// ptr-operator: |
| 3763 | /// '*' cv-qualifier-seq[opt] |
| 3764 | /// '&' |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3765 | /// [C++0x] '&&' |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3766 | /// [GNU] '&' restrict[opt] attributes[opt] |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3767 | /// [GNU?] '&&' restrict[opt] attributes[opt] |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3768 | /// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt] |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3769 | void Parser::ParseDeclaratorInternal(Declarator &D, |
| 3770 | DirectDeclParseFunction DirectDeclParser) { |
Douglas Gregor | 91a2886 | 2009-08-26 14:27:30 +0000 | [diff] [blame] | 3771 | if (Diags.hasAllExtensionsSilenced()) |
| 3772 | D.setExtension(); |
Douglas Gregor | 2ccccb3 | 2010-08-23 18:23:48 +0000 | [diff] [blame] | 3773 | |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3774 | // C++ member pointers start with a '::' or a nested-name. |
| 3775 | // Member pointers get special handling, since there's no place for the |
| 3776 | // scope spec in the generic path below. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3777 | if (getLangOpts().CPlusPlus && |
Chris Lattner | f919bfe | 2009-03-24 17:04:48 +0000 | [diff] [blame] | 3778 | (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) || |
| 3779 | Tok.is(tok::annot_cxxscope))) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3780 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 3781 | D.getContext() == Declarator::MemberContext; |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3782 | CXXScopeSpec SS; |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3783 | ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3784 | |
Jeffrey Yasskin | edc2877 | 2010-04-07 23:29:58 +0000 | [diff] [blame] | 3785 | if (SS.isNotEmpty()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3786 | if (Tok.isNot(tok::star)) { |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3787 | // The scope spec really belongs to the direct-declarator. |
| 3788 | D.getCXXScopeSpec() = SS; |
| 3789 | if (DirectDeclParser) |
| 3790 | (this->*DirectDeclParser)(D); |
| 3791 | return; |
| 3792 | } |
| 3793 | |
| 3794 | SourceLocation Loc = ConsumeToken(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3795 | D.SetRangeEnd(Loc); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3796 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3797 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3798 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3799 | |
| 3800 | // Recurse to parse whatever is left. |
| 3801 | ParseDeclaratorInternal(D, DirectDeclParser); |
| 3802 | |
| 3803 | // Sema will have to catch (syntactically invalid) pointers into global |
| 3804 | // scope. It has to catch pointers into namespace scope anyway. |
| 3805 | D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3806 | Loc), |
| 3807 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3808 | /* Don't replace range end. */SourceLocation()); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3809 | return; |
| 3810 | } |
| 3811 | } |
| 3812 | |
| 3813 | tok::TokenKind Kind = Tok.getKind(); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3814 | // Not a pointer, C++ reference, or block. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3815 | if (!isPtrOperatorToken(Kind, getLangOpts())) { |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3816 | if (DirectDeclParser) |
| 3817 | (this->*DirectDeclParser)(D); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 3818 | return; |
| 3819 | } |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3820 | |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3821 | // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference, |
| 3822 | // '&&' -> rvalue reference |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3823 | SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&. |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3824 | D.SetRangeEnd(Loc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3825 | |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 3826 | if (Kind == tok::star || Kind == tok::caret) { |
Chris Lattner | 7654914 | 2008-02-21 01:32:26 +0000 | [diff] [blame] | 3827 | // Is a pointer. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3828 | DeclSpec DS(AttrFactory); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3829 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3830 | // FIXME: GNU attributes are not allowed here in a new-type-id. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3831 | ParseTypeQualifierListOpt(DS); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3832 | D.ExtendWithDeclSpec(DS); |
Sebastian Redl | f30208a | 2009-01-24 21:16:55 +0000 | [diff] [blame] | 3833 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3834 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3835 | ParseDeclaratorInternal(D, DirectDeclParser); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3836 | if (Kind == tok::star) |
| 3837 | // Remember that we parsed a pointer type, and remember the type-quals. |
| 3838 | D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc, |
Chandler Carruth | d067c07 | 2011-02-23 18:51:59 +0000 | [diff] [blame] | 3839 | DS.getConstSpecLoc(), |
| 3840 | DS.getVolatileSpecLoc(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3841 | DS.getRestrictSpecLoc()), |
| 3842 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3843 | SourceLocation()); |
Steve Naroff | 5618bd4 | 2008-08-27 16:04:49 +0000 | [diff] [blame] | 3844 | else |
| 3845 | // Remember that we parsed a Block type, and remember the type-quals. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3846 | D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3847 | Loc), |
| 3848 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3849 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3850 | } else { |
| 3851 | // Is a reference |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3852 | DeclSpec DS(AttrFactory); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3853 | |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3854 | // Complain about rvalue references in C++03, but then go on and build |
| 3855 | // the declarator. |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3856 | if (Kind == tok::ampamp) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3857 | Diag(Loc, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 3858 | diag::warn_cxx98_compat_rvalue_reference : |
| 3859 | diag::ext_rvalue_reference); |
Sebastian Redl | 743de1f | 2009-03-23 00:00:23 +0000 | [diff] [blame] | 3860 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3861 | // GNU-style and C++11 attributes are allowed here, as is restrict. |
| 3862 | ParseTypeQualifierListOpt(DS); |
| 3863 | D.ExtendWithDeclSpec(DS); |
| 3864 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3865 | // C++ 8.3.2p1: cv-qualified references are ill-formed except when the |
| 3866 | // cv-qualifiers are introduced through the use of a typedef or of a |
| 3867 | // template type argument, in which case the cv-qualifiers are ignored. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3868 | if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) { |
| 3869 | if (DS.getTypeQualifiers() & DeclSpec::TQ_const) |
| 3870 | Diag(DS.getConstSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3871 | diag::err_invalid_reference_qualifier_application) << "const"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3872 | if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile) |
| 3873 | Diag(DS.getVolatileSpecLoc(), |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 3874 | diag::err_invalid_reference_qualifier_application) << "volatile"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3875 | } |
| 3876 | |
| 3877 | // Recursively parse the declarator. |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3878 | ParseDeclaratorInternal(D, DirectDeclParser); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3879 | |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3880 | if (D.getNumTypeObjects() > 0) { |
| 3881 | // C++ [dcl.ref]p4: There shall be no references to references. |
| 3882 | DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1); |
| 3883 | if (InnerChunk.Kind == DeclaratorChunk::Reference) { |
Chris Lattner | da83bac | 2008-11-19 07:37:42 +0000 | [diff] [blame] | 3884 | if (const IdentifierInfo *II = D.getIdentifier()) |
| 3885 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 3886 | << II; |
| 3887 | else |
| 3888 | Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference) |
| 3889 | << "type name"; |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3890 | |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 3891 | // Once we've complained about the reference-to-reference, we |
Douglas Gregor | f1f9b4e | 2008-11-03 15:51:28 +0000 | [diff] [blame] | 3892 | // can go ahead and build the (technically ill-formed) |
| 3893 | // declarator: reference collapsing will take care of it. |
| 3894 | } |
| 3895 | } |
| 3896 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3897 | // 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] | 3898 | D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc, |
Sebastian Redl | 05532f2 | 2009-03-15 22:02:01 +0000 | [diff] [blame] | 3899 | Kind == tok::amp), |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 3900 | DS.getAttributes(), |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 3901 | SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3902 | } |
| 3903 | } |
| 3904 | |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3905 | static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D, |
| 3906 | SourceLocation EllipsisLoc) { |
| 3907 | if (EllipsisLoc.isValid()) { |
| 3908 | FixItHint Insertion; |
| 3909 | if (!D.getEllipsisLoc().isValid()) { |
| 3910 | Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "..."); |
| 3911 | D.setEllipsisLoc(EllipsisLoc); |
| 3912 | } |
| 3913 | P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration) |
| 3914 | << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName(); |
| 3915 | } |
| 3916 | } |
| 3917 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3918 | /// ParseDirectDeclarator |
| 3919 | /// direct-declarator: [C99 6.7.5] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3920 | /// [C99] identifier |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3921 | /// '(' declarator ')' |
| 3922 | /// [GNU] '(' attributes declarator ')' |
| 3923 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 3924 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 3925 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 3926 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 3927 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3928 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 3929 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3930 | /// direct-declarator '(' parameter-type-list ')' |
| 3931 | /// direct-declarator '(' identifier-list[opt] ')' |
| 3932 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 3933 | /// parameter-type-list[opt] ')' |
Argyrios Kyrtzidis | 971c4fa | 2008-10-24 21:46:40 +0000 | [diff] [blame] | 3934 | /// [C++] direct-declarator '(' parameter-declaration-clause ')' |
| 3935 | /// cv-qualifier-seq[opt] exception-specification[opt] |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3936 | /// [C++11] direct-declarator '(' parameter-declaration-clause ')' |
| 3937 | /// attribute-specifier-seq[opt] cv-qualifier-seq[opt] |
| 3938 | /// ref-qualifier[opt] exception-specification[opt] |
Douglas Gregor | b48fe38 | 2008-10-31 09:07:45 +0000 | [diff] [blame] | 3939 | /// [C++] declarator-id |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 3940 | /// [C++11] declarator-id attribute-specifier-seq[opt] |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3941 | /// |
| 3942 | /// declarator-id: [C++ 8] |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3943 | /// '...'[opt] id-expression |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3944 | /// '::'[opt] nested-name-specifier[opt] type-name |
| 3945 | /// |
| 3946 | /// id-expression: [C++ 5.1] |
| 3947 | /// unqualified-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3948 | /// qualified-id |
Douglas Gregor | 42a552f | 2008-11-05 20:51:48 +0000 | [diff] [blame] | 3949 | /// |
| 3950 | /// unqualified-id: [C++ 5.1] |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3951 | /// identifier |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3952 | /// operator-function-id |
Douglas Gregor | db422df | 2009-09-25 21:45:23 +0000 | [diff] [blame] | 3953 | /// conversion-function-id |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3954 | /// '~' class-name |
Douglas Gregor | 39a8de1 | 2009-02-25 19:37:18 +0000 | [diff] [blame] | 3955 | /// template-id |
Argyrios Kyrtzidis | c7ed9c6 | 2008-11-07 22:02:30 +0000 | [diff] [blame] | 3956 | /// |
Richard Smith | 5d8388c | 2012-03-27 01:42:32 +0000 | [diff] [blame] | 3957 | /// Note, any additional constructs added here may need corresponding changes |
| 3958 | /// in isConstructorDeclarator. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3959 | void Parser::ParseDirectDeclarator(Declarator &D) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 3960 | DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec()); |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 3961 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3962 | if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) { |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 3963 | // ParseDeclaratorInternal might already have parsed the scope. |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3964 | if (D.getCXXScopeSpec().isEmpty()) { |
Douglas Gregor | efaa93a | 2011-11-07 17:33:42 +0000 | [diff] [blame] | 3965 | bool EnteringContext = D.getContext() == Declarator::FileContext || |
| 3966 | D.getContext() == Declarator::MemberContext; |
| 3967 | ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(), |
| 3968 | EnteringContext); |
John McCall | 9ba6166 | 2010-02-26 08:45:28 +0000 | [diff] [blame] | 3969 | } |
| 3970 | |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3971 | if (D.getCXXScopeSpec().isValid()) { |
Douglas Gregor | 23c94db | 2010-07-02 17:43:08 +0000 | [diff] [blame] | 3972 | if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
John McCall | e7e278b | 2009-12-11 20:04:54 +0000 | [diff] [blame] | 3973 | // Change the declaration context for name lookup, until this function |
| 3974 | // is exited (and the declarator has been parsed). |
| 3975 | DeclScopeObj.EnterDeclaratorScope(); |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3976 | } |
| 3977 | |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3978 | // C++0x [dcl.fct]p14: |
| 3979 | // There is a syntactic ambiguity when an ellipsis occurs at the end |
| 3980 | // of a parameter-declaration-clause without a preceding comma. In |
| 3981 | // this case, the ellipsis is parsed as part of the |
| 3982 | // abstract-declarator if the type of the parameter names a template |
| 3983 | // parameter pack that has not been expanded; otherwise, it is parsed |
| 3984 | // as part of the parameter-declaration-clause. |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3985 | if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3986 | !((D.getContext() == Declarator::PrototypeContext || |
| 3987 | D.getContext() == Declarator::BlockLiteralContext) && |
Douglas Gregor | a8bc8c9 | 2010-12-23 22:44:42 +0000 | [diff] [blame] | 3988 | NextToken().is(tok::r_paren) && |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 3989 | !Actions.containsUnexpandedParameterPacks(D))) { |
| 3990 | SourceLocation EllipsisLoc = ConsumeToken(); |
| 3991 | if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) { |
| 3992 | // The ellipsis was put in the wrong place. Recover, and explain to |
| 3993 | // the user what they should have done. |
| 3994 | ParseDeclarator(D); |
| 3995 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 3996 | return; |
| 3997 | } else |
| 3998 | D.setEllipsisLoc(EllipsisLoc); |
| 3999 | |
| 4000 | // The ellipsis can't be followed by a parenthesized declarator. We |
| 4001 | // check for that in ParseParenDeclarator, after we have disambiguated |
| 4002 | // the l_paren token. |
| 4003 | } |
| 4004 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4005 | if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) || |
| 4006 | Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) { |
| 4007 | // We found something that indicates the start of an unqualified-id. |
| 4008 | // Parse that unqualified-id. |
John McCall | ba9d853 | 2010-04-13 06:39:49 +0000 | [diff] [blame] | 4009 | bool AllowConstructorName; |
| 4010 | if (D.getDeclSpec().hasTypeSpecifier()) |
| 4011 | AllowConstructorName = false; |
| 4012 | else if (D.getCXXScopeSpec().isSet()) |
| 4013 | AllowConstructorName = |
| 4014 | (D.getContext() == Declarator::FileContext || |
| 4015 | (D.getContext() == Declarator::MemberContext && |
| 4016 | D.getDeclSpec().isFriendSpecified())); |
| 4017 | else |
| 4018 | AllowConstructorName = (D.getContext() == Declarator::MemberContext); |
| 4019 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4020 | SourceLocation TemplateKWLoc; |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4021 | if (ParseUnqualifiedId(D.getCXXScopeSpec(), |
| 4022 | /*EnteringContext=*/true, |
| 4023 | /*AllowDestructorName=*/true, |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4024 | AllowConstructorName, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4025 | ParsedType(), |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 4026 | TemplateKWLoc, |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4027 | D.getName()) || |
| 4028 | // Once we're past the identifier, if the scope was bad, mark the |
| 4029 | // whole declarator bad. |
| 4030 | D.getCXXScopeSpec().isInvalid()) { |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4031 | D.SetIdentifier(0, Tok.getLocation()); |
| 4032 | D.setInvalidType(true); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4033 | } else { |
| 4034 | // Parsed the unqualified-id; update range information and move along. |
| 4035 | if (D.getSourceRange().getBegin().isInvalid()) |
| 4036 | D.SetRangeBegin(D.getName().getSourceRange().getBegin()); |
| 4037 | D.SetRangeEnd(D.getName().getSourceRange().getEnd()); |
Douglas Gregor | 2f1bc52 | 2008-11-07 20:08:42 +0000 | [diff] [blame] | 4038 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4039 | goto PastIdentifier; |
Douglas Gregor | 1cd1b1e | 2008-11-06 22:13:31 +0000 | [diff] [blame] | 4040 | } |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4041 | } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4042 | assert(!getLangOpts().CPlusPlus && |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4043 | "There's a C++-specific check for tok::identifier above"); |
| 4044 | assert(Tok.getIdentifierInfo() && "Not an identifier?"); |
| 4045 | D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation()); |
| 4046 | ConsumeToken(); |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4047 | goto PastIdentifier; |
| 4048 | } |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4049 | |
Douglas Gregor | 3f9a056 | 2009-11-03 01:35:08 +0000 | [diff] [blame] | 4050 | if (Tok.is(tok::l_paren)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4051 | // direct-declarator: '(' declarator ')' |
| 4052 | // direct-declarator: '(' attributes declarator ')' |
| 4053 | // Example: 'char (*X)' or 'int (*XX)(void)' |
| 4054 | ParseParenDeclarator(D); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4055 | |
| 4056 | // If the declarator was parenthesized, we entered the declarator |
| 4057 | // scope when parsing the parenthesized declarator, then exited |
| 4058 | // the scope already. Re-enter the scope, if we need to. |
| 4059 | if (D.getCXXScopeSpec().isSet()) { |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4060 | // If there was an error parsing parenthesized declarator, declarator |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4061 | // scope may have been entered before. Don't do it again. |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4062 | if (!D.isInvalidType() && |
| 4063 | Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec())) |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4064 | // Change the declaration context for name lookup, until this function |
| 4065 | // is exited (and the declarator has been parsed). |
Fariborz Jahanian | 46877cd | 2010-08-17 23:50:37 +0000 | [diff] [blame] | 4066 | DeclScopeObj.EnterDeclaratorScope(); |
Douglas Gregor | 0efc2c1 | 2010-01-13 17:31:36 +0000 | [diff] [blame] | 4067 | } |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4068 | } else if (D.mayOmitIdentifier()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4069 | // This could be something simple like "int" (in which case the declarator |
| 4070 | // portion is empty), if an abstract-declarator is allowed. |
| 4071 | D.SetIdentifier(0, Tok.getLocation()); |
| 4072 | } else { |
Douglas Gregor | e950d4b | 2009-03-06 23:28:18 +0000 | [diff] [blame] | 4073 | if (D.getContext() == Declarator::MemberContext) |
| 4074 | Diag(Tok, diag::err_expected_member_name_or_semi) |
| 4075 | << D.getDeclSpec().getSourceRange(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4076 | else if (getLangOpts().CPlusPlus) |
| 4077 | Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus; |
Argyrios Kyrtzidis | eb83ecd | 2008-11-08 16:45:02 +0000 | [diff] [blame] | 4078 | else |
Chris Lattner | 1ab3b96 | 2008-11-18 07:48:38 +0000 | [diff] [blame] | 4079 | Diag(Tok, diag::err_expected_ident_lparen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4080 | D.SetIdentifier(0, Tok.getLocation()); |
Chris Lattner | 1f6f54b | 2008-11-11 06:13:16 +0000 | [diff] [blame] | 4081 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4082 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4083 | |
Argyrios Kyrtzidis | 314fe78 | 2008-11-26 22:40:03 +0000 | [diff] [blame] | 4084 | PastIdentifier: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4085 | assert(D.isPastIdentifier() && |
| 4086 | "Haven't past the location of the identifier yet?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4087 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4088 | // Don't parse attributes unless we have parsed an unparenthesized name. |
| 4089 | if (D.hasName() && !D.getNumTypeObjects()) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4090 | MaybeParseCXX0XAttributes(D); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4091 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4092 | while (1) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4093 | if (Tok.is(tok::l_paren)) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4094 | // Enter function-declaration scope, limiting any declarators to the |
| 4095 | // function prototype scope, including parameter declarators. |
| 4096 | ParseScope PrototypeScope(this, |
| 4097 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4098 | // The paren may be part of a C++ direct initializer, eg. "int x(1);". |
| 4099 | // In such a case, check if we actually have a function declarator; if it |
| 4100 | // is not, the declarator has been fully parsed. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4101 | if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) { |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4102 | // When not in file scope, warn for ambiguous function declarators, just |
| 4103 | // in case the author intended it as a variable definition. |
| 4104 | bool warnIfAmbiguous = D.getContext() != Declarator::FileContext; |
| 4105 | if (!isCXXFunctionDeclarator(warnIfAmbiguous)) |
| 4106 | break; |
| 4107 | } |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4108 | ParsedAttributes attrs(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4109 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4110 | T.consumeOpen(); |
| 4111 | ParseFunctionDeclarator(D, attrs, T); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4112 | PrototypeScope.Exit(); |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4113 | } else if (Tok.is(tok::l_square)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4114 | ParseBracketDeclarator(D); |
| 4115 | } else { |
| 4116 | break; |
| 4117 | } |
| 4118 | } |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4119 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4120 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4121 | /// ParseParenDeclarator - We parsed the declarator D up to a paren. This is |
| 4122 | /// only called before the identifier, so these are most likely just grouping |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4123 | /// parens for precedence. If we find that these are actually function |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4124 | /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator. |
| 4125 | /// |
| 4126 | /// direct-declarator: |
| 4127 | /// '(' declarator ')' |
| 4128 | /// [GNU] '(' attributes declarator ')' |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4129 | /// direct-declarator '(' parameter-type-list ')' |
| 4130 | /// direct-declarator '(' identifier-list[opt] ')' |
| 4131 | /// [GNU] direct-declarator '(' parameter-forward-declarations |
| 4132 | /// parameter-type-list[opt] ')' |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4133 | /// |
| 4134 | void Parser::ParseParenDeclarator(Declarator &D) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4135 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4136 | T.consumeOpen(); |
| 4137 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4138 | assert(!D.isPastIdentifier() && "Should be called before passing identifier"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4139 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4140 | // Eat any attributes before we look at whether this is a grouping or function |
| 4141 | // declarator paren. If this is a grouping paren, the attribute applies to |
| 4142 | // the type being built up, for example: |
| 4143 | // int (__attribute__(()) *x)(long y) |
| 4144 | // If this ends up not being a grouping paren, the attribute applies to the |
| 4145 | // first argument, for example: |
| 4146 | // int (__attribute__(()) int x) |
| 4147 | // In either case, we need to eat any attributes to be able to determine what |
| 4148 | // sort of paren this is. |
| 4149 | // |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4150 | ParsedAttributes attrs(AttrFactory); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4151 | bool RequiresArg = false; |
| 4152 | if (Tok.is(tok::kw___attribute)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4153 | ParseGNUAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4154 | |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4155 | // We require that the argument list (if this is a non-grouping paren) be |
| 4156 | // present even if the attribute list was empty. |
| 4157 | RequiresArg = true; |
| 4158 | } |
Steve Naroff | 239f073 | 2008-12-25 14:16:32 +0000 | [diff] [blame] | 4159 | // Eat any Microsoft extensions. |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4160 | if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) || |
Douglas Gregor | f813a2c | 2010-05-18 16:57:00 +0000 | [diff] [blame] | 4161 | Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) || |
Francois Pichet | 3bd9aa4 | 2011-08-18 09:59:55 +0000 | [diff] [blame] | 4162 | Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) || |
Francois Pichet | 58fd97a | 2011-08-25 00:36:46 +0000 | [diff] [blame] | 4163 | Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) { |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4164 | ParseMicrosoftTypeAttributes(attrs); |
Eli Friedman | 290eeb0 | 2009-06-08 23:27:34 +0000 | [diff] [blame] | 4165 | } |
Dawn Perchik | 52fc314 | 2010-09-03 01:29:35 +0000 | [diff] [blame] | 4166 | // Eat any Borland extensions. |
Ted Kremenek | 8113ecf | 2010-11-10 05:59:39 +0000 | [diff] [blame] | 4167 | if (Tok.is(tok::kw___pascal)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4168 | ParseBorlandTypeAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4169 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4170 | // If we haven't past the identifier yet (or where the identifier would be |
| 4171 | // stored, if this is an abstract declarator), then this is probably just |
| 4172 | // grouping parens. However, if this could be an abstract-declarator, then |
| 4173 | // this could also be the start of function arguments (consider 'void()'). |
| 4174 | bool isGrouping; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4175 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4176 | if (!D.mayOmitIdentifier()) { |
| 4177 | // If this can't be an abstract-declarator, this *must* be a grouping |
| 4178 | // paren, because we haven't seen the identifier yet. |
| 4179 | isGrouping = true; |
| 4180 | } else if (Tok.is(tok::r_paren) || // 'int()' is a function. |
Richard Smith | 2259286 | 2012-03-27 23:05:05 +0000 | [diff] [blame] | 4181 | (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) && |
| 4182 | NextToken().is(tok::r_paren)) || // C++ int(...) |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4183 | isDeclarationSpecifier() || // 'int(int)' is a function. |
| 4184 | isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4185 | // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is |
| 4186 | // considered to be a type, not a K&R identifier-list. |
| 4187 | isGrouping = false; |
| 4188 | } else { |
| 4189 | // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'. |
| 4190 | isGrouping = true; |
| 4191 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4192 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4193 | // If this is a grouping paren, handle: |
| 4194 | // direct-declarator: '(' declarator ')' |
| 4195 | // direct-declarator: '(' attributes declarator ')' |
| 4196 | if (isGrouping) { |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4197 | SourceLocation EllipsisLoc = D.getEllipsisLoc(); |
| 4198 | D.setEllipsisLoc(SourceLocation()); |
| 4199 | |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4200 | bool hadGroupingParens = D.hasGroupingParens(); |
Argyrios Kyrtzidis | 73a0d88 | 2008-10-06 17:10:33 +0000 | [diff] [blame] | 4201 | D.setGroupingParens(true); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 4202 | ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4203 | // Match the ')'. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4204 | T.consumeClose(); |
| 4205 | D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(), |
| 4206 | T.getCloseLocation()), |
| 4207 | attrs, T.getCloseLocation()); |
Argyrios Kyrtzidis | 3f2a8a0 | 2008-10-07 10:21:57 +0000 | [diff] [blame] | 4208 | |
| 4209 | D.setGroupingParens(hadGroupingParens); |
Richard Smith | 9988f28 | 2012-03-29 01:16:42 +0000 | [diff] [blame] | 4210 | |
| 4211 | // An ellipsis cannot be placed outside parentheses. |
| 4212 | if (EllipsisLoc.isValid()) |
| 4213 | diagnoseMisplacedEllipsis(*this, D, EllipsisLoc); |
| 4214 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4215 | return; |
| 4216 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4217 | |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4218 | // Okay, if this wasn't a grouping paren, it must be the start of a function |
| 4219 | // argument list. Recognize that this declarator will never have an |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4220 | // identifier (and remember where it would have been), then call into |
| 4221 | // ParseFunctionDeclarator to handle of argument list. |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4222 | D.SetIdentifier(0, Tok.getLocation()); |
| 4223 | |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4224 | // Enter function-declaration scope, limiting any declarators to the |
| 4225 | // function prototype scope, including parameter declarators. |
| 4226 | ParseScope PrototypeScope(this, |
| 4227 | Scope::FunctionPrototypeScope|Scope::DeclScope); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4228 | ParseFunctionDeclarator(D, attrs, T, RequiresArg); |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4229 | PrototypeScope.Exit(); |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4230 | } |
| 4231 | |
| 4232 | /// ParseFunctionDeclarator - We are after the identifier and have parsed the |
| 4233 | /// declarator D up to a paren, which indicates that we are parsing function |
| 4234 | /// arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4235 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4236 | /// If FirstArgAttrs is non-null, then the caller parsed those arguments |
| 4237 | /// immediately after the open paren - they should be considered to be the |
| 4238 | /// first argument of a parameter. |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4239 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4240 | /// If RequiresArg is true, then the first argument of the function is required |
| 4241 | /// to be present and required to not be an identifier list. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4242 | /// |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4243 | /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt], |
| 4244 | /// (C++11) ref-qualifier[opt], exception-specification[opt], |
| 4245 | /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt]. |
| 4246 | /// |
| 4247 | /// [C++11] exception-specification: |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4248 | /// dynamic-exception-specification |
| 4249 | /// noexcept-specification |
| 4250 | /// |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4251 | void Parser::ParseFunctionDeclarator(Declarator &D, |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4252 | ParsedAttributes &FirstArgAttrs, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4253 | BalancedDelimiterTracker &Tracker, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4254 | bool RequiresArg) { |
David Blaikie | 42d6d0c | 2011-12-04 05:04:18 +0000 | [diff] [blame] | 4255 | assert(getCurScope()->isFunctionPrototypeScope() && |
| 4256 | "Should call from a Function scope"); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4257 | // lparen is already consumed! |
| 4258 | assert(D.isPastIdentifier() && "Should not call before identifier!"); |
| 4259 | |
| 4260 | // This should be true when the function has typed arguments. |
| 4261 | // Otherwise, it is treated as a K&R-style function. |
| 4262 | bool HasProto = false; |
| 4263 | // Build up an array of information about the parsed arguments. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4264 | SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4265 | // Remember where we see an ellipsis, if any. |
| 4266 | SourceLocation EllipsisLoc; |
| 4267 | |
| 4268 | DeclSpec DS(AttrFactory); |
| 4269 | bool RefQualifierIsLValueRef = true; |
| 4270 | SourceLocation RefQualifierLoc; |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4271 | SourceLocation ConstQualifierLoc; |
| 4272 | SourceLocation VolatileQualifierLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4273 | ExceptionSpecificationType ESpecType = EST_None; |
| 4274 | SourceRange ESpecRange; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4275 | SmallVector<ParsedType, 2> DynamicExceptions; |
| 4276 | SmallVector<SourceRange, 2> DynamicExceptionRanges; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4277 | ExprResult NoexceptExpr; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4278 | ParsedAttributes FnAttrs(AttrFactory); |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame^] | 4279 | TypeResult TrailingReturnType; |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4280 | |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4281 | Actions.ActOnStartFunctionDeclarator(); |
| 4282 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4283 | SourceLocation EndLoc; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4284 | if (isFunctionDeclaratorIdentifierList()) { |
| 4285 | if (RequiresArg) |
| 4286 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4287 | |
| 4288 | ParseFunctionDeclaratorIdentifierList(D, ParamInfo); |
| 4289 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4290 | Tracker.consumeClose(); |
| 4291 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4292 | } else { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4293 | if (Tok.isNot(tok::r_paren)) |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4294 | ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4295 | else if (RequiresArg) |
| 4296 | Diag(Tok, diag::err_argument_required_after_attribute); |
| 4297 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4298 | HasProto = ParamInfo.size() || getLangOpts().CPlusPlus; |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4299 | |
| 4300 | // If we have the closing ')', eat it. |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4301 | Tracker.consumeClose(); |
| 4302 | EndLoc = Tracker.getCloseLocation(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4303 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4304 | if (getLangOpts().CPlusPlus) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4305 | // FIXME: Accept these components in any order, and produce fixits to |
| 4306 | // correct the order if the user gets it wrong. Ideally we should deal |
| 4307 | // with the virt-specifier-seq and pure-specifier in the same way. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4308 | |
| 4309 | // Parse cv-qualifier-seq[opt]. |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4310 | ParseTypeQualifierListOpt(DS, false /*no attributes*/, false); |
| 4311 | if (!DS.getSourceRange().getEnd().isInvalid()) { |
| 4312 | EndLoc = DS.getSourceRange().getEnd(); |
| 4313 | ConstQualifierLoc = DS.getConstSpecLoc(); |
| 4314 | VolatileQualifierLoc = DS.getVolatileSpecLoc(); |
| 4315 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4316 | |
| 4317 | // Parse ref-qualifier[opt]. |
| 4318 | if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4319 | Diag(Tok, getLangOpts().CPlusPlus0x ? |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4320 | diag::warn_cxx98_compat_ref_qualifier : |
| 4321 | diag::ext_ref_qualifier); |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4322 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4323 | RefQualifierIsLValueRef = Tok.is(tok::amp); |
| 4324 | RefQualifierLoc = ConsumeToken(); |
| 4325 | EndLoc = RefQualifierLoc; |
| 4326 | } |
| 4327 | |
Douglas Gregor | cefc3af | 2012-04-16 07:05:22 +0000 | [diff] [blame] | 4328 | // C++11 [expr.prim.general]p3: |
| 4329 | // If a declaration declares a member function or member function |
| 4330 | // template of a class X, the expression this is a prvalue of type |
| 4331 | // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq |
| 4332 | // and the end of the function-definition, member-declarator, or |
| 4333 | // declarator. |
| 4334 | bool IsCXX11MemberFunction = |
| 4335 | getLangOpts().CPlusPlus0x && |
| 4336 | (D.getContext() == Declarator::MemberContext || |
| 4337 | (D.getContext() == Declarator::FileContext && |
| 4338 | D.getCXXScopeSpec().isValid() && |
| 4339 | Actions.CurContext->isRecord())); |
| 4340 | Sema::CXXThisScopeRAII ThisScope(Actions, |
| 4341 | dyn_cast<CXXRecordDecl>(Actions.CurContext), |
| 4342 | DS.getTypeQualifiers(), |
| 4343 | IsCXX11MemberFunction); |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 4344 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4345 | // Parse exception-specification[opt]. |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 4346 | ESpecType = tryParseExceptionSpecification(ESpecRange, |
Douglas Gregor | 74e2fc3 | 2012-04-16 18:27:27 +0000 | [diff] [blame] | 4347 | DynamicExceptions, |
| 4348 | DynamicExceptionRanges, |
Richard Smith | a058fd4 | 2012-05-02 22:22:32 +0000 | [diff] [blame] | 4349 | NoexceptExpr); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4350 | if (ESpecType != EST_None) |
| 4351 | EndLoc = ESpecRange.getEnd(); |
| 4352 | |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4353 | // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes |
| 4354 | // after the exception-specification. |
| 4355 | MaybeParseCXX0XAttributes(FnAttrs); |
| 4356 | |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4357 | // Parse trailing-return-type[opt]. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4358 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) { |
Richard Smith | 7fe6208 | 2011-10-15 05:09:34 +0000 | [diff] [blame] | 4359 | Diag(Tok, diag::warn_cxx98_compat_trailing_return_type); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 4360 | SourceRange Range; |
Richard Smith | 54655be | 2012-06-12 01:51:59 +0000 | [diff] [blame^] | 4361 | TrailingReturnType = ParseTrailingReturnType(Range); |
Douglas Gregor | ae7902c | 2011-08-04 15:30:47 +0000 | [diff] [blame] | 4362 | if (Range.getEnd().isValid()) |
| 4363 | EndLoc = Range.getEnd(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4364 | } |
| 4365 | } |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4366 | } |
| 4367 | |
| 4368 | // Remember that we parsed a function type, and remember the attributes. |
| 4369 | D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto, |
| 4370 | /*isVariadic=*/EllipsisLoc.isValid(), |
| 4371 | EllipsisLoc, |
| 4372 | ParamInfo.data(), ParamInfo.size(), |
| 4373 | DS.getTypeQualifiers(), |
| 4374 | RefQualifierIsLValueRef, |
Douglas Gregor | 43f5103 | 2011-10-19 06:04:55 +0000 | [diff] [blame] | 4375 | RefQualifierLoc, ConstQualifierLoc, |
| 4376 | VolatileQualifierLoc, |
Douglas Gregor | 90ebed0 | 2011-07-13 21:47:47 +0000 | [diff] [blame] | 4377 | /*MutableLoc=*/SourceLocation(), |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4378 | ESpecType, ESpecRange.getBegin(), |
| 4379 | DynamicExceptions.data(), |
| 4380 | DynamicExceptionRanges.data(), |
| 4381 | DynamicExceptions.size(), |
| 4382 | NoexceptExpr.isUsable() ? |
| 4383 | NoexceptExpr.get() : 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4384 | Tracker.getOpenLocation(), |
| 4385 | EndLoc, D, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4386 | TrailingReturnType), |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4387 | FnAttrs, EndLoc); |
James Molloy | 16f1f71 | 2012-02-29 10:24:19 +0000 | [diff] [blame] | 4388 | |
| 4389 | Actions.ActOnEndFunctionDeclarator(); |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4390 | } |
| 4391 | |
| 4392 | /// isFunctionDeclaratorIdentifierList - This parameter list may have an |
| 4393 | /// identifier list form for a K&R-style function: void foo(a,b,c) |
| 4394 | /// |
| 4395 | /// Note that identifier-lists are only allowed for normal declarators, not for |
| 4396 | /// abstract-declarators. |
| 4397 | bool Parser::isFunctionDeclaratorIdentifierList() { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4398 | return !getLangOpts().CPlusPlus |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4399 | && Tok.is(tok::identifier) |
| 4400 | && !TryAltiVecVectorToken() |
| 4401 | // K&R identifier lists can't have typedefs as identifiers, per C99 |
| 4402 | // 6.7.5.3p11. |
| 4403 | && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename)) |
| 4404 | // Identifier lists follow a really simple grammar: the identifiers can |
| 4405 | // be followed *only* by a ", identifier" or ")". However, K&R |
| 4406 | // identifier lists are really rare in the brave new modern world, and |
| 4407 | // it is very common for someone to typo a type in a non-K&R style |
| 4408 | // list. If we are presented with something like: "void foo(intptr x, |
| 4409 | // float y)", we don't want to start parsing the function declarator as |
| 4410 | // though it is a K&R style declarator just because intptr is an |
| 4411 | // invalid type. |
| 4412 | // |
| 4413 | // To handle this, we check to see if the token after the first |
| 4414 | // identifier is a "," or ")". Only then do we parse it as an |
| 4415 | // identifier list. |
| 4416 | && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)); |
| 4417 | } |
| 4418 | |
| 4419 | /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator |
| 4420 | /// we found a K&R-style identifier list instead of a typed parameter list. |
| 4421 | /// |
| 4422 | /// After returning, ParamInfo will hold the parsed parameters. |
| 4423 | /// |
| 4424 | /// identifier-list: [C99 6.7.5] |
| 4425 | /// identifier |
| 4426 | /// identifier-list ',' identifier |
| 4427 | /// |
| 4428 | void Parser::ParseFunctionDeclaratorIdentifierList( |
| 4429 | Declarator &D, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4430 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) { |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4431 | // If there was no identifier specified for the declarator, either we are in |
| 4432 | // an abstract-declarator, or we are in a parameter declarator which was found |
| 4433 | // to be abstract. In abstract-declarators, identifier lists are not valid: |
| 4434 | // diagnose this. |
| 4435 | if (!D.getIdentifier()) |
| 4436 | Diag(Tok, diag::ext_ident_list_in_param); |
| 4437 | |
| 4438 | // Maintain an efficient lookup of params we have seen so far. |
| 4439 | llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar; |
| 4440 | |
| 4441 | while (1) { |
| 4442 | // If this isn't an identifier, report the error and skip until ')'. |
| 4443 | if (Tok.isNot(tok::identifier)) { |
| 4444 | Diag(Tok, diag::err_expected_ident); |
| 4445 | SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true); |
| 4446 | // Forget we parsed anything. |
| 4447 | ParamInfo.clear(); |
| 4448 | return; |
| 4449 | } |
| 4450 | |
| 4451 | IdentifierInfo *ParmII = Tok.getIdentifierInfo(); |
| 4452 | |
| 4453 | // Reject 'typedef int y; int test(x, y)', but continue parsing. |
| 4454 | if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope())) |
| 4455 | Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII; |
| 4456 | |
| 4457 | // Verify that the argument identifier has not already been mentioned. |
| 4458 | if (!ParamsSoFar.insert(ParmII)) { |
| 4459 | Diag(Tok, diag::err_param_redefinition) << ParmII; |
| 4460 | } else { |
| 4461 | // Remember this identifier in ParamInfo. |
| 4462 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4463 | Tok.getLocation(), |
| 4464 | 0)); |
| 4465 | } |
| 4466 | |
| 4467 | // Eat the identifier. |
| 4468 | ConsumeToken(); |
| 4469 | |
| 4470 | // The list continues if we see a comma. |
| 4471 | if (Tok.isNot(tok::comma)) |
| 4472 | break; |
| 4473 | ConsumeToken(); |
| 4474 | } |
| 4475 | } |
| 4476 | |
| 4477 | /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list |
| 4478 | /// after the opening parenthesis. This function will not parse a K&R-style |
| 4479 | /// identifier list. |
| 4480 | /// |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4481 | /// D is the declarator being parsed. If FirstArgAttrs is non-null, then the |
| 4482 | /// caller parsed those arguments immediately after the open paren - they should |
| 4483 | /// be considered to be part of the first parameter. |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4484 | /// |
| 4485 | /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will |
| 4486 | /// be the location of the ellipsis, if any was parsed. |
| 4487 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4488 | /// parameter-type-list: [C99 6.7.5] |
| 4489 | /// parameter-list |
| 4490 | /// parameter-list ',' '...' |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4491 | /// [C++] parameter-list '...' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4492 | /// |
| 4493 | /// parameter-list: [C99 6.7.5] |
| 4494 | /// parameter-declaration |
| 4495 | /// parameter-list ',' parameter-declaration |
| 4496 | /// |
| 4497 | /// parameter-declaration: [C99 6.7.5] |
| 4498 | /// declaration-specifiers declarator |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4499 | /// [C++] declaration-specifiers declarator '=' assignment-expression |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4500 | /// [C++11] initializer-clause |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4501 | /// [GNU] declaration-specifiers declarator attributes |
Sebastian Redl | 50de12f | 2009-03-24 22:27:57 +0000 | [diff] [blame] | 4502 | /// declaration-specifiers abstract-declarator[opt] |
| 4503 | /// [C++] declaration-specifiers abstract-declarator[opt] |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 4504 | /// '=' assignment-expression |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4505 | /// [GNU] declaration-specifiers abstract-declarator[opt] attributes |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4506 | /// [C++11] attribute-specifier-seq parameter-declaration |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4507 | /// |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4508 | void Parser::ParseParameterDeclarationClause( |
| 4509 | Declarator &D, |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4510 | ParsedAttributes &FirstArgAttrs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4511 | SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo, |
Douglas Gregor | 3fd1ba0 | 2011-07-05 16:44:18 +0000 | [diff] [blame] | 4512 | SourceLocation &EllipsisLoc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4513 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4514 | while (1) { |
| 4515 | if (Tok.is(tok::ellipsis)) { |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4516 | // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq |
| 4517 | // before deciding this was a parameter-declaration-clause. |
Douglas Gregor | 965acbb | 2009-02-18 07:07:28 +0000 | [diff] [blame] | 4518 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4519 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4520 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4521 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4522 | // Parse the declaration-specifiers. |
John McCall | 54abf7d | 2009-11-04 02:18:39 +0000 | [diff] [blame] | 4523 | // Just use the ParsingDeclaration "scope" of the declarator. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4524 | DeclSpec DS(AttrFactory); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4525 | |
Richard Smith | 6ce48a7 | 2012-04-11 04:01:28 +0000 | [diff] [blame] | 4526 | // Parse any C++11 attributes. |
| 4527 | MaybeParseCXX0XAttributes(DS.getAttributes()); |
| 4528 | |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4529 | // Skip any Microsoft attributes before a param. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4530 | if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square)) |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4531 | ParseMicrosoftAttributes(DS.getAttributes()); |
| 4532 | |
| 4533 | SourceLocation DSStart = Tok.getLocation(); |
Chris Lattner | 7399ee0 | 2008-10-20 02:05:46 +0000 | [diff] [blame] | 4534 | |
| 4535 | // If the caller parsed attributes for the first argument, add them now. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4536 | // 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] | 4537 | // 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] | 4538 | // get rid of a parameter (FirstArgAttrs) and this statement. It might be |
| 4539 | // too much hassle. |
| 4540 | DS.takeAttributesFrom(FirstArgAttrs); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4541 | |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4542 | ParseDeclarationSpecifiers(DS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4543 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4544 | // Parse the declarator. This is "PrototypeContext", because we must |
| 4545 | // accept either 'declarator' or 'abstract-declarator' here. |
| 4546 | Declarator ParmDecl(DS, Declarator::PrototypeContext); |
| 4547 | ParseDeclarator(ParmDecl); |
| 4548 | |
| 4549 | // Parse GNU attributes, if present. |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4550 | MaybeParseGNUAttributes(ParmDecl); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4551 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4552 | // Remember this parsed parameter in ParamInfo. |
| 4553 | IdentifierInfo *ParmII = ParmDecl.getIdentifier(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4554 | |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4555 | // DefArgToks is used when the parsing of default arguments needs |
| 4556 | // to be delayed. |
| 4557 | CachedTokens *DefArgToks = 0; |
| 4558 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4559 | // If no parameter was specified, verify that *something* was specified, |
| 4560 | // otherwise we have a missing type and identifier. |
Chris Lattner | e64c549 | 2009-02-27 18:38:20 +0000 | [diff] [blame] | 4561 | if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 && |
| 4562 | ParmDecl.getNumTypeObjects() == 0) { |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4563 | // Completely missing, emit error. |
| 4564 | Diag(DSStart, diag::err_missing_param); |
| 4565 | } else { |
| 4566 | // Otherwise, we have something. Add it and let semantic analysis try |
| 4567 | // 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] | 4568 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4569 | // Inform the actions module about the parameter declarator, so it gets |
| 4570 | // added to the current scope. |
John McCall | d226f65 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4571 | Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4572 | |
| 4573 | // Parse the default argument, if any. We parse the default |
| 4574 | // arguments in all dialects; the semantic analysis in |
| 4575 | // ActOnParamDefaultArgument will reject the default argument in |
| 4576 | // C. |
| 4577 | if (Tok.is(tok::equal)) { |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4578 | SourceLocation EqualLoc = Tok.getLocation(); |
| 4579 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4580 | // Parse the default argument |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4581 | if (D.getContext() == Declarator::MemberContext) { |
| 4582 | // If we're inside a class definition, cache the tokens |
| 4583 | // corresponding to the default argument. We'll actually parse |
| 4584 | // them when we see the end of the class definition. |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4585 | // FIXME: Can we use a smart pointer for Toks? |
| 4586 | DefArgToks = new CachedTokens; |
| 4587 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4588 | if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks, |
Argyrios Kyrtzidis | 14b9162 | 2010-04-23 21:20:12 +0000 | [diff] [blame] | 4589 | /*StopAtSemi=*/true, |
| 4590 | /*ConsumeFinalToken=*/false)) { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4591 | delete DefArgToks; |
| 4592 | DefArgToks = 0; |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4593 | Actions.ActOnParamDefaultArgumentError(Param); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4594 | } else { |
| 4595 | // Mark the end of the default argument so that we know when to |
| 4596 | // stop when we parse it later on. |
| 4597 | Token DefArgEnd; |
| 4598 | DefArgEnd.startToken(); |
| 4599 | DefArgEnd.setKind(tok::cxx_defaultarg_end); |
| 4600 | DefArgEnd.setLocation(Tok.getLocation()); |
| 4601 | DefArgToks->push_back(DefArgEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4602 | Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc, |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4603 | (*DefArgToks)[1].getLocation()); |
Argyrios Kyrtzidis | 2b602ad | 2010-08-06 09:47:24 +0000 | [diff] [blame] | 4604 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4605 | } else { |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4606 | // Consume the '='. |
Douglas Gregor | 61366e9 | 2008-12-24 00:01:03 +0000 | [diff] [blame] | 4607 | ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4608 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4609 | // The argument isn't actually potentially evaluated unless it is |
| 4610 | // used. |
| 4611 | EnterExpressionEvaluationContext Eval(Actions, |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 4612 | Sema::PotentiallyEvaluatedIfUsed, |
| 4613 | Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4614 | |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4615 | ExprResult DefArgResult; |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 4616 | if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) { |
| 4617 | Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists); |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4618 | DefArgResult = ParseBraceInitializer(); |
Sebastian Redl | 3e280b5 | 2012-03-18 22:25:45 +0000 | [diff] [blame] | 4619 | } else |
Sebastian Redl | 84407ba | 2012-03-14 15:54:00 +0000 | [diff] [blame] | 4620 | DefArgResult = ParseAssignmentExpression(); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4621 | if (DefArgResult.isInvalid()) { |
| 4622 | Actions.ActOnParamDefaultArgumentError(Param); |
| 4623 | SkipUntil(tok::comma, tok::r_paren, true, true); |
| 4624 | } else { |
| 4625 | // Inform the actions module about the default argument |
| 4626 | Actions.ActOnParamDefaultArgument(Param, EqualLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4627 | DefArgResult.take()); |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4628 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 4629 | } |
| 4630 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4631 | |
| 4632 | ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII, |
| 4633 | ParmDecl.getIdentifierLoc(), Param, |
Douglas Gregor | 72b505b | 2008-12-16 21:30:33 +0000 | [diff] [blame] | 4634 | DefArgToks)); |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4635 | } |
| 4636 | |
| 4637 | // 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] | 4638 | if (Tok.isNot(tok::comma)) { |
| 4639 | if (Tok.is(tok::ellipsis)) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4640 | EllipsisLoc = ConsumeToken(); // Consume the ellipsis. |
| 4641 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4642 | if (!getLangOpts().CPlusPlus) { |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4643 | // We have ellipsis without a preceding ',', which is ill-formed |
| 4644 | // in C. Complain and provide the fix. |
| 4645 | Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4646 | << FixItHint::CreateInsertion(EllipsisLoc, ", "); |
Douglas Gregor | ed5d651 | 2009-09-22 21:41:40 +0000 | [diff] [blame] | 4647 | } |
| 4648 | } |
| 4649 | |
| 4650 | break; |
| 4651 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4652 | |
Chris Lattner | f97409f | 2008-04-06 06:57:35 +0000 | [diff] [blame] | 4653 | // Consume the comma. |
| 4654 | ConsumeToken(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4655 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4656 | |
Chris Lattner | 66d2865 | 2008-04-06 06:34:08 +0000 | [diff] [blame] | 4657 | } |
Chris Lattner | ef4715c | 2008-04-06 05:45:57 +0000 | [diff] [blame] | 4658 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4659 | /// [C90] direct-declarator '[' constant-expression[opt] ']' |
| 4660 | /// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']' |
| 4661 | /// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']' |
| 4662 | /// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']' |
| 4663 | /// [C99] direct-declarator '[' type-qual-list[opt] '*' ']' |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4664 | /// [C++11] direct-declarator '[' constant-expression[opt] ']' |
| 4665 | /// attribute-specifier-seq[opt] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4666 | void Parser::ParseBracketDeclarator(Declarator &D) { |
Richard Smith | 6ee326a | 2012-04-10 01:32:12 +0000 | [diff] [blame] | 4667 | if (CheckProhibitedCXX11Attribute()) |
| 4668 | return; |
| 4669 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4670 | BalancedDelimiterTracker T(*this, tok::l_square); |
| 4671 | T.consumeOpen(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4672 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4673 | // C array syntax has many features, but by-far the most common is [] and [4]. |
| 4674 | // This code does a fast path to handle some of the most obvious cases. |
| 4675 | if (Tok.getKind() == tok::r_square) { |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4676 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4677 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4678 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4679 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4680 | // Remember that we parsed the empty array type. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4681 | ExprResult NumElements; |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4682 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0, |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4683 | T.getOpenLocation(), |
| 4684 | T.getCloseLocation()), |
| 4685 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4686 | return; |
| 4687 | } else if (Tok.getKind() == tok::numeric_constant && |
| 4688 | GetLookAheadToken(1).is(tok::r_square)) { |
| 4689 | // [4] is very common. Parse the numeric constant expression. |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 4690 | ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope())); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4691 | ConsumeToken(); |
| 4692 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4693 | T.consumeClose(); |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4694 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4695 | MaybeParseCXX0XAttributes(attrs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4696 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4697 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4698 | D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0, |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4699 | ExprRes.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4700 | T.getOpenLocation(), |
| 4701 | T.getCloseLocation()), |
| 4702 | attrs, T.getCloseLocation()); |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4703 | return; |
| 4704 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4705 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4706 | // If valid, this location is the position where we read the 'static' keyword. |
| 4707 | SourceLocation StaticLoc; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4708 | if (Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4709 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4710 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4711 | // If there is a type-qualifier-list, read it now. |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4712 | // Type qualifiers in an array subscript are a C99 feature. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4713 | DeclSpec DS(AttrFactory); |
Chris Lattner | 5a69d1c | 2008-12-18 07:02:59 +0000 | [diff] [blame] | 4714 | ParseTypeQualifierListOpt(DS, false /*no attributes*/); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4715 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4716 | // If we haven't already read 'static', check to see if there is one after the |
| 4717 | // type-qualifier-list. |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4718 | if (!StaticLoc.isValid() && Tok.is(tok::kw_static)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4719 | StaticLoc = ConsumeToken(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4720 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4721 | // Handle "direct-declarator [ type-qual-list[opt] * ]". |
| 4722 | bool isStar = false; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4723 | ExprResult NumElements; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4724 | |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4725 | // Handle the case where we have '[*]' as the array size. However, a leading |
| 4726 | // star could be the start of an expression, for example 'X[*p + 4]'. Verify |
| 4727 | // the the token after the star is a ']'. Since stars in arrays are |
| 4728 | // infrequent, use of lookahead is not costly here. |
| 4729 | if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) { |
Chris Lattner | a711dd0 | 2008-04-06 05:27:21 +0000 | [diff] [blame] | 4730 | ConsumeToken(); // Eat the '*'. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4731 | |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4732 | if (StaticLoc.isValid()) { |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4733 | Diag(StaticLoc, diag::err_unspecified_vla_size_with_static); |
Chris Lattner | a1fcbad | 2008-12-18 06:50:14 +0000 | [diff] [blame] | 4734 | StaticLoc = SourceLocation(); // Drop the static. |
| 4735 | } |
Chris Lattner | 5dcc6ce | 2008-04-06 05:26:30 +0000 | [diff] [blame] | 4736 | isStar = true; |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4737 | } else if (Tok.isNot(tok::r_square)) { |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4738 | // Note, in C89, this production uses the constant-expr production instead |
| 4739 | // of assignment-expr. The only difference is that assignment-expr allows |
| 4740 | // things like '=' and '*='. Sema rejects these in C89 mode because they |
| 4741 | // 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] | 4742 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4743 | // Parse the constant-expression or assignment-expression now (depending |
| 4744 | // on dialect). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4745 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4746 | NumElements = ParseConstantExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4747 | } else { |
| 4748 | EnterExpressionEvaluationContext Unevaluated(Actions, |
| 4749 | Sema::ConstantEvaluated); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 4750 | NumElements = ParseAssignmentExpression(); |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4751 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4752 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4753 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4754 | // If there was an error parsing the assignment-expression, recover. |
Sebastian Redl | 0e9eabc | 2008-12-09 13:15:23 +0000 | [diff] [blame] | 4755 | if (NumElements.isInvalid()) { |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 4756 | D.setInvalidType(true); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4757 | // If the expression was invalid, skip it. |
| 4758 | SkipUntil(tok::r_square); |
| 4759 | return; |
| 4760 | } |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4761 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4762 | T.consumeClose(); |
Sebastian Redl | ab197ba | 2009-02-09 18:23:29 +0000 | [diff] [blame] | 4763 | |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4764 | ParsedAttributes attrs(AttrFactory); |
John McCall | 7f040a9 | 2010-12-24 02:08:15 +0000 | [diff] [blame] | 4765 | MaybeParseCXX0XAttributes(attrs); |
Sean Hunt | bbd37c6 | 2009-11-21 08:43:09 +0000 | [diff] [blame] | 4766 | |
Chris Lattner | 378c7e4 | 2008-12-18 07:27:21 +0000 | [diff] [blame] | 4767 | // Remember that we parsed a array type, and remember its features. |
John McCall | 0b7e678 | 2011-03-24 11:26:52 +0000 | [diff] [blame] | 4768 | D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(), |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4769 | StaticLoc.isValid(), isStar, |
Douglas Gregor | 7e7eb3d | 2009-07-06 15:59:29 +0000 | [diff] [blame] | 4770 | NumElements.release(), |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4771 | T.getOpenLocation(), |
| 4772 | T.getCloseLocation()), |
| 4773 | attrs, T.getCloseLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4774 | } |
| 4775 | |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4776 | /// [GNU] typeof-specifier: |
| 4777 | /// typeof ( expressions ) |
| 4778 | /// typeof ( type-name ) |
| 4779 | /// [GNU/C++] typeof unary-expression |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4780 | /// |
| 4781 | void Parser::ParseTypeofSpecifier(DeclSpec &DS) { |
Chris Lattner | 04d6666 | 2007-10-09 17:33:22 +0000 | [diff] [blame] | 4782 | assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier"); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4783 | Token OpTok = Tok; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4784 | SourceLocation StartLoc = ConsumeToken(); |
| 4785 | |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 4786 | const bool hasParens = Tok.is(tok::l_paren); |
| 4787 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4788 | EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated); |
| 4789 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4790 | bool isCastExpr; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4791 | ParsedType CastTy; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4792 | SourceRange CastRange; |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 4793 | ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, |
| 4794 | CastTy, CastRange); |
John McCall | cfb708c | 2010-01-13 20:03:27 +0000 | [diff] [blame] | 4795 | if (hasParens) |
| 4796 | DS.setTypeofParensRange(CastRange); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4797 | |
| 4798 | if (CastRange.getEnd().isInvalid()) |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4799 | // FIXME: Not accurate, the range gets one token more than it should. |
| 4800 | DS.SetRangeEnd(Tok.getLocation()); |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4801 | else |
| 4802 | DS.SetRangeEnd(CastRange.getEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4803 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4804 | if (isCastExpr) { |
| 4805 | if (!CastTy) { |
| 4806 | DS.SetTypeSpecError(); |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4807 | return; |
Douglas Gregor | 809070a | 2009-02-18 17:45:20 +0000 | [diff] [blame] | 4808 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4809 | |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4810 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4811 | unsigned DiagID; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4812 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 4813 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec, |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4814 | DiagID, CastTy)) |
| 4815 | Diag(StartLoc, DiagID) << PrevSpec; |
Argyrios Kyrtzidis | 5ab0640 | 2009-05-22 10:22:50 +0000 | [diff] [blame] | 4816 | return; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4817 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4818 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4819 | // If we get here, the operand to the typeof was an expresion. |
| 4820 | if (Operand.isInvalid()) { |
| 4821 | DS.SetTypeSpecError(); |
Steve Naroff | 9dfa7b4 | 2007-08-02 02:53:48 +0000 | [diff] [blame] | 4822 | return; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4823 | } |
Argyrios Kyrtzidis | 0f07203 | 2008-09-05 11:26:19 +0000 | [diff] [blame] | 4824 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 4825 | // We might need to transform the operand if it is potentially evaluated. |
| 4826 | Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get()); |
| 4827 | if (Operand.isInvalid()) { |
| 4828 | DS.SetTypeSpecError(); |
| 4829 | return; |
| 4830 | } |
| 4831 | |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4832 | const char *PrevSpec = 0; |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4833 | unsigned DiagID; |
Argyrios Kyrtzidis | 6409625 | 2009-05-22 10:22:18 +0000 | [diff] [blame] | 4834 | // Check for duplicate type specifiers (e.g. "int typeof(int)"). |
| 4835 | if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4836 | DiagID, Operand.get())) |
John McCall | fec5401 | 2009-08-03 20:12:06 +0000 | [diff] [blame] | 4837 | Diag(StartLoc, DiagID) << PrevSpec; |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 4838 | } |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4839 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 4840 | /// [C11] atomic-specifier: |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4841 | /// _Atomic ( type-name ) |
| 4842 | /// |
| 4843 | void Parser::ParseAtomicSpecifier(DeclSpec &DS) { |
| 4844 | assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier"); |
| 4845 | |
| 4846 | SourceLocation StartLoc = ConsumeToken(); |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4847 | BalancedDelimiterTracker T(*this, tok::l_paren); |
| 4848 | if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) { |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4849 | SkipUntil(tok::r_paren); |
| 4850 | return; |
| 4851 | } |
| 4852 | |
| 4853 | TypeResult Result = ParseTypeName(); |
| 4854 | if (Result.isInvalid()) { |
| 4855 | SkipUntil(tok::r_paren); |
| 4856 | return; |
| 4857 | } |
| 4858 | |
| 4859 | // Match the ')' |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4860 | T.consumeClose(); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4861 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4862 | if (T.getCloseLocation().isInvalid()) |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4863 | return; |
| 4864 | |
Douglas Gregor | 4a8dfb5 | 2011-10-12 16:37:45 +0000 | [diff] [blame] | 4865 | DS.setTypeofParensRange(T.getRange()); |
| 4866 | DS.SetRangeEnd(T.getCloseLocation()); |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 4867 | |
| 4868 | const char *PrevSpec = 0; |
| 4869 | unsigned DiagID; |
| 4870 | if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec, |
| 4871 | DiagID, Result.release())) |
| 4872 | Diag(StartLoc, DiagID) << PrevSpec; |
| 4873 | } |
| 4874 | |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4875 | |
| 4876 | /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called |
| 4877 | /// from TryAltiVecVectorToken. |
| 4878 | bool Parser::TryAltiVecVectorTokenOutOfLine() { |
| 4879 | Token Next = NextToken(); |
| 4880 | switch (Next.getKind()) { |
| 4881 | default: return false; |
| 4882 | case tok::kw_short: |
| 4883 | case tok::kw_long: |
| 4884 | case tok::kw_signed: |
| 4885 | case tok::kw_unsigned: |
| 4886 | case tok::kw_void: |
| 4887 | case tok::kw_char: |
| 4888 | case tok::kw_int: |
| 4889 | case tok::kw_float: |
| 4890 | case tok::kw_double: |
| 4891 | case tok::kw_bool: |
| 4892 | case tok::kw___pixel: |
| 4893 | Tok.setKind(tok::kw___vector); |
| 4894 | return true; |
| 4895 | case tok::identifier: |
| 4896 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 4897 | Tok.setKind(tok::kw___vector); |
| 4898 | return true; |
| 4899 | } |
| 4900 | return false; |
| 4901 | } |
| 4902 | } |
| 4903 | |
| 4904 | bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc, |
| 4905 | const char *&PrevSpec, unsigned &DiagID, |
| 4906 | bool &isInvalid) { |
| 4907 | if (Tok.getIdentifierInfo() == Ident_vector) { |
| 4908 | Token Next = NextToken(); |
| 4909 | switch (Next.getKind()) { |
| 4910 | case tok::kw_short: |
| 4911 | case tok::kw_long: |
| 4912 | case tok::kw_signed: |
| 4913 | case tok::kw_unsigned: |
| 4914 | case tok::kw_void: |
| 4915 | case tok::kw_char: |
| 4916 | case tok::kw_int: |
| 4917 | case tok::kw_float: |
| 4918 | case tok::kw_double: |
| 4919 | case tok::kw_bool: |
| 4920 | case tok::kw___pixel: |
| 4921 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 4922 | return true; |
| 4923 | case tok::identifier: |
| 4924 | if (Next.getIdentifierInfo() == Ident_pixel) { |
| 4925 | isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID); |
| 4926 | return true; |
| 4927 | } |
| 4928 | break; |
| 4929 | default: |
| 4930 | break; |
| 4931 | } |
Douglas Gregor | a8f031f | 2010-06-16 15:28:57 +0000 | [diff] [blame] | 4932 | } else if ((Tok.getIdentifierInfo() == Ident_pixel) && |
Chris Lattner | 1b49242 | 2010-02-28 18:33:55 +0000 | [diff] [blame] | 4933 | DS.isTypeAltiVecVector()) { |
| 4934 | isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID); |
| 4935 | return true; |
| 4936 | } |
| 4937 | return false; |
| 4938 | } |